Fixed purgeJob() and added clearJob()

This commit is contained in:
Andy Miller
2019-02-18 12:59:47 -07:00
parent 6c1701ea5a
commit 72016d7e7c
4 changed files with 54 additions and 3 deletions

View File

@@ -5,6 +5,7 @@
* Implemented `Grav\Framework\Psr7` classes as `Nyholm/psr7` decorators
* Renamed `blueprints/user/accounts.yaml` to `blueprints/user/users.yaml`
* Moved FlexUser index into `user-data://flex/indexes/users.yaml` [#2378](https://github.com/getgrav/grav/issues/2378)
* Added a new `cache-clear` scheduled job to go along with `cache-purge`
1. [](#improved)
* More code cleanup
* Fixed `FlexUser` caching

View File

@@ -568,8 +568,23 @@ form:
type: cron
label: PLUGIN_ADMIN.CACHE_PURGE_JOB
help: PLUGIN_ADMIN.CACHE_PURGE_JOB_HELP
default: '* 4 * * *'
cache.clear_at:
type: cron
label: PLUGIN_ADMIN.CACHE_CLEAR_JOB
help: PLUGIN_ADMIN.CACHE_CLEAR_JOB_HELP
default: '* 3 * * *'
cache.clear_job_type:
type: select
size: medium
label: PLUGIN_ADMIN.CACHE_JOB_TYPE
help: PLUGIN_ADMIN.CACHE_JOB_TYPE_HELP
options:
standard: Standard Cache Folders
all: All Cache Folders
cache.clear_images_by_default:
type: toggle
label: PLUGIN_ADMIN.CLEAR_IMAGES_BY_DEFAULT

View File

@@ -78,7 +78,9 @@ cache:
method: file # Method to check for updates in pages: file|folder|hash|none
driver: auto # One of: auto|file|apc|xcache|memcache|wincache
prefix: 'g' # Cache prefix string (prevents cache conflicts)
purge_at: '0 4 * * *' # How often to purge old cache (using new scheduler)
purge_at: '0 4 * * *' # How often to purge old file cache (using new scheduler)
clear_at: '0 3 * * *' # How often to clear cache (using new scheduler)
clear_job_type: 'standard' # Type to clear when processing the scheduled clear job `standard`|`all`
clear_images_by_default: true # By default grav will include processed images in cache clear, this can be disabled
cli_compatibility: false # Ensures only non-volatile drivers are used (file, redis, memcache, etc.)
lifetime: 604800 # Lifetime of cached data in seconds (0 = infinite)

View File

@@ -142,6 +142,11 @@ class Cache extends Getters
$dispatcher->addListener('onSchedulerInitialized', [$this, 'onSchedulerInitialized']);
}
/**
* Deletes the old out of date file-based caches
*
* @return int
*/
public function purgeOldCache()
{
$cache_dir = dirname($this->cache_dir);
@@ -558,12 +563,29 @@ class Cache extends Getters
return false;
}
/**
* Static function to call as a scheduled Job to purge old Doctrine files
*/
public static function purgeJob()
{
/** @var Cache $cache */
$cache = Grav::instance()['cache'];
$deleted_folders = $cache->purgeOldCache();
return 'Purged ' . $deleted_folders . ' old cache folders...';
echo 'Purged ' . $deleted_folders . ' old cache folders...';
}
/**
* Static function to call as a scheduled Job to clear Grav cache
*
* @param $type
*/
public static function clearJob($type)
{
$result = static::clearCache($type);
static::invalidateCache();
echo strip_tags(implode("\n", $result));
}
public function onSchedulerInitialized(Event $event)
@@ -572,11 +594,22 @@ class Cache extends Getters
$scheduler = $event['scheduler'];
$config = Grav::instance()['config'];
// File Cache Purge
$at = $config->get('system.cache.purge_at');
$name = 'cache-purge';
$logs = 'logs/' . $name . '.out';
$job = $scheduler->addFunction('Grav\Common\Cache::purgeJob', null, $name );
$job = $scheduler->addFunction('Grav\Common\Cache::purgeJob', [], $name );
$job->at($at);
$job->output($logs);
// Cache Clear
$at = $config->get('system.cache.clear_at');
$clear_type = $config->get('system.cache.clear_job_type');
$name = 'cache-clear';
$logs = 'logs/' . $name . '.out';
$job = $scheduler->addFunction('Grav\Common\Cache::clearJob', [$clear_type], $name );
$job->at($at);
$job->output($logs);