From 72016d7e7c17f62d3623cefe08f518e2b6ba2c6d Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Mon, 18 Feb 2019 12:59:47 -0700 Subject: [PATCH] Fixed purgeJob() and added clearJob() --- CHANGELOG.md | 1 + system/blueprints/config/system.yaml | 15 +++++++++++ system/config/system.yaml | 4 ++- system/src/Grav/Common/Cache.php | 37 ++++++++++++++++++++++++++-- 4 files changed, 54 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71774f42c..59101ab42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/system/blueprints/config/system.yaml b/system/blueprints/config/system.yaml index 8f9769086..73785d62d 100644 --- a/system/blueprints/config/system.yaml +++ b/system/blueprints/config/system.yaml @@ -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 diff --git a/system/config/system.yaml b/system/config/system.yaml index e41135d3b..c07e32efe 100644 --- a/system/config/system.yaml +++ b/system/config/system.yaml @@ -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) diff --git a/system/src/Grav/Common/Cache.php b/system/src/Grav/Common/Cache.php index 2167f13a5..34b516a2d 100644 --- a/system/src/Grav/Common/Cache.php +++ b/system/src/Grav/Common/Cache.php @@ -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);