Browse Source

Remove _expires cache keys

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.
Andy Dawson 8 years ago
parent
commit
9dbcc0470d
2 changed files with 3 additions and 50 deletions
  1. 2 35
      src/Cache/Engine/ApcuEngine.php
  2. 1 15
      src/Cache/Engine/WincacheEngine.php

+ 2 - 35
src/Cache/Engine/ApcuEngine.php

@@ -59,29 +59,9 @@ class ApcuEngine extends CacheEngine
     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);
     }
 
     /**
@@ -96,13 +76,6 @@ class ApcuEngine extends CacheEngine
     {
         $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);
     }
 
@@ -196,15 +169,9 @@ class ApcuEngine extends CacheEngine
     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);
     }
 
     /**

+ 1 - 15
src/Cache/Engine/WincacheEngine.php

@@ -61,17 +61,9 @@ class WincacheEngine extends CacheEngine
     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);
     }
 
     /**
@@ -85,12 +77,6 @@ class WincacheEngine extends CacheEngine
     {
         $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);
     }