Skip to content

Commit

Permalink
Remove _expires cache keys
Browse files Browse the repository at this point in the history
Apcu and wincache implement ttl - it shouldn't be necessary to set two
cache keys and double-confirm that the cache entry has not expired
before returning the cache hit.

XCache also implements ttl, but uses the request time for all
timestamps, that means cache usage like this doesn't work:

    Cache::write('something', 'value');
    sleep(longer than ttl);
    $result = Cache::read('something'); <- still 'value'

Since XCache support ends soon, this engine is not modified.
  • Loading branch information
AD7six committed Nov 17, 2017
1 parent ee50968 commit 9dbcc04
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 50 deletions.
37 changes: 2 additions & 35 deletions src/Cache/Engine/ApcuEngine.php
Expand Up @@ -59,29 +59,9 @@ public function init(array $config = [])
public function write($key, $value)
{
$key = $this->_key($key);

$expires = 0;
$duration = $this->_config['duration'];
if ($duration) {
$expires = time() + $duration;
}

$values = [
$key . '_expires' => $expires,
$key => $value,
];

$errors = apcu_store($values, null, $duration);

if ($errors !== []) {
$this->warning(
sprintf('Failed to store %s into APCu cache.', json_encode($errors))
);

return false;
}

return true;
return apcu_store($key, $value, $duration);
}

/**
Expand All @@ -96,13 +76,6 @@ public function read($key)
{
$key = $this->_key($key);

$time = time();
$success = false;
$cachetime = (int)apcu_fetch($key . '_expires', $success);
if ($success && $cachetime !== 0 && ($cachetime < $time || ($time + $this->_config['duration']) < $cachetime)) {
return false;
}

return apcu_fetch($key);
}

Expand Down Expand Up @@ -196,15 +169,9 @@ public function clear($check)
public function add($key, $value)
{
$key = $this->_key($key);

$expires = 0;
$duration = $this->_config['duration'];
if ($duration) {
$expires = time() + $duration;
}

return apcu_add($key, $value, $duration) === true
&& apcu_add($key . '_expires', $expires, $duration) === true;
return apcu_add($key, $value, $duration);
}

/**
Expand Down
16 changes: 1 addition & 15 deletions src/Cache/Engine/WincacheEngine.php
Expand Up @@ -61,17 +61,9 @@ public function init(array $config = [])
public function write($key, $value)
{
$key = $this->_key($key);

$duration = $this->_config['duration'];
$expires = time() + $duration;

$data = [
$key . '_expires' => $expires,
$key => $value
];
$result = wincache_ucache_set($data, null, $duration);

return empty($result);
return wincache_ucache_set($key, $value, $duration);
}

/**
Expand All @@ -85,12 +77,6 @@ public function read($key)
{
$key = $this->_key($key);

$time = time();
$cachetime = (int)wincache_ucache_get($key . '_expires');
if ($cachetime < $time || ($time + $this->_config['duration']) < $cachetime) {
return false;
}

return wincache_ucache_get($key);
}

Expand Down

0 comments on commit 9dbcc04

Please sign in to comment.