Skip to content

Commit

Permalink
Schedule a WP Cron event to perform regular pool maintenance. Fixes #12
Browse files Browse the repository at this point in the history
… and hopefully helps a lot with #8
  • Loading branch information
Biont committed Jul 10, 2019
1 parent feabe0d commit 4abfc72
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Cli/WpCliCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Inpsyde\WpStash\Cli;

use Inpsyde\WpStash\WpStash;
use WP_CLI;

// phpcs:disable Inpsyde.CodeQuality.LineLength.TooLong
Expand Down Expand Up @@ -107,4 +108,25 @@ public function flush($args, $assocArgs)
}
}
}

/**
* Some drivers require that maintenance action be performed regular.
* The FileSystem and SQLite drivers, as an example, need to remove old data as they can't do it automatically.
*
* @see http://www.stashphp.com/Integration.html#maintenance-actions
* @throws WP_CLI\ExitException
*/
public function purge()
{

$result = WpStash::instance()
->purge();
if ($result) {
WP_CLI::success('Cache purged successfully');

return;
}
WP_CLI::error('Something unexpected happened during cache purging. Maybe try to run this command with --skip-packages to prevent plugins/themes from interfering');

}
}
11 changes: 11 additions & 0 deletions src/ObjectCacheProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,17 @@ public function flush(): bool
return true;
}

/**
* Perform Cache Pool Maintenance
*
* @return bool
*/
public function purge(): bool
{

return $this->persistent->purge() && $this->non_persistent->purge();
}

/**
* Increment numeric cache item's value
*
Expand Down
12 changes: 12 additions & 0 deletions src/StashAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,18 @@ public function replace(string $key, $data, int $expire = 0): bool
return $this->set($key, $data, $expire);
}

/**
* Perform Cache Pool maintenance
*
* @return bool
*/
public function purge(): bool
{

return $this->pool->purge();

}

public function __destruct()
{
$this->pool->commit();
Expand Down
28 changes: 28 additions & 0 deletions src/WpStash.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,34 @@ public function init()
if ($this->isWpCli()) {
\WP_CLI::add_command('stash', Cli\WpCliCommand::class);
}

add_action('init', function () {

$scheduledPurgeHook = 'inpsyde.wp-stash.scheduled-purge';

add_action($scheduledPurgeHook, [$this, 'purge']);

if (! wp_next_scheduled($scheduledPurgeHook)) {
wp_schedule_single_event(time() + HOUR_IN_SECONDS * 12, $scheduledPurgeHook);
}
});

}
/**
* Some drivers require that maintenance action be performed regular.
* The FileSystem and SQLite drivers, as an example, need to remove old data as they can't do it automatically.
*
* @see http://www.stashphp.com/Integration.html#maintenance-actions
*/
public function purge(): bool
{

global $wp_object_cache;
if (! $wp_object_cache instanceof ObjectCacheProxy) {
return false;
}

return $wp_object_cache->purge();
}

private function ensureDropin(): bool
Expand Down

0 comments on commit 4abfc72

Please sign in to comment.