Skip to content

Commit

Permalink
added cleanUp() function
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-graute committed Jan 19, 2021
1 parent f956628 commit c4a32d9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Pressmind/Cache/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public function add($pKey, $pValue, $info = null);
public function remove($pKey);
public function exists($pKey);
public function get($pKey);
public function cleanUp();
}
48 changes: 45 additions & 3 deletions src/Pressmind/Cache/Adapter/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
namespace Pressmind\Cache\Adapter;


use DateTime;
use Pressmind\HelperFunctions;
use Pressmind\Log\Writer;
use Pressmind\Registry;
use Pressmind\REST\Server;

class Redis implements AdapterInterface
{
Expand All @@ -20,12 +23,17 @@ class Redis implements AdapterInterface
*/
private $_prefix;

/**
* @var array
*/
private $_config;

public function __construct()
{
$config = Registry::getInstance()->get('config');
$this->_config = Registry::getInstance()->get('config')['cache'];
$this->_server = new \Redis();
$this->_server->connect($config['cache']['adapter']['config']['host'], $config['cache']['adapter']['config']['port']);
$this->_prefix = HelperFunctions::replaceConstantsFromConfig($config['cache']['key_prefix']);
$this->_server->connect($this->_config['adapter']['config']['host'], $this->_config['adapter']['config']['port']);
$this->_prefix = HelperFunctions::replaceConstantsFromConfig($this->_config['key_prefix']);
}

public function isEnabled()
Expand Down Expand Up @@ -61,4 +69,38 @@ public function remove($pKey)
{
return $this->_server->del('pmt2core-' . $this->_prefix . '-' . $pKey);
}

public function cleanUp()
{
$keys = $this->_server->keys('pmt2core-' . $this->_prefix . '-*');
Writer::write('Found ' . count($keys) . ' keys in cache', WRITER::OUTPUT_BOTH, 'redis', WRITER::TYPE_INFO);
foreach ($this->_server->keys('pmt2core-' . $this->_prefix . '-*') as $key) {
Writer::write('Checking key ' . $key, WRITER::OUTPUT_BOTH, 'redis', WRITER::TYPE_INFO);
$idle_time = $this->_server->object('idletime', $key);
$info = json_decode($this->_server->hGet('pmt2corecacheinfo-' . $this->_prefix, $key));
$now = new DateTime();
$cache_date = $this->_server->hGet('pmt2corecachetime-' . $this->_prefix, $key);
$date = DateTime::createFromFormat(DateTime::ISO8601, $cache_date);
$age = $now->getTimestamp() - $date->getTimestamp();
Writer::write('Idle time: ' . $idle_time . ' sec.', WRITER::OUTPUT_BOTH, 'redis', WRITER::TYPE_INFO);
Writer::write('Age: ' . $age . ' sec.', WRITER::OUTPUT_BOTH, 'redis', WRITER::TYPE_INFO);
if($idle_time >= $this->_config['max_idle_time'] || $age >= $this->_config['update_frequency']) {
Writer::write('Deleting key ' . $key, WRITER::OUTPUT_BOTH, 'redis', WRITER::TYPE_INFO);
$this->_server->del($key);
$this->_server->hDel('pmt2corecacheinfo-' . $this->_prefix, $key);
$this->_server->hDel('pmt2corecachetime-' . $this->_prefix, $key);
}
if($age >= $this->_config['update_frequency'] && $idle_time < $this->_config['max_idle_time']) {
Writer::write('Updating key ' . $key . ' due to update frequency', WRITER::OUTPUT_BOTH, 'redis', WRITER::TYPE_INFO);
Writer::write('Type: ' . $info->type, WRITER::OUTPUT_BOTH, 'redis', WRITER::TYPE_INFO);
Writer::write('Class: ' . $info->classname, WRITER::OUTPUT_BOTH, 'redis', WRITER::TYPE_INFO);
Writer::write('Method: ' . $info->method, WRITER::OUTPUT_BOTH, 'redis', WRITER::TYPE_INFO);
Writer::write('Params: ' . print_r($info->parameters, true), WRITER::OUTPUT_BOTH, 'redis', WRITER::TYPE_INFO);
if(strtolower($info->type) == 'rest') {
$rest_server = new Server();
$rest_server->directCall($info->classname, $info->method, json_decode(json_encode($info->parameters), true));
}
}
}
}
}

0 comments on commit c4a32d9

Please sign in to comment.