Improve RedisCacheSource::set()
authorMaximilian Mader <max@bastelstu.be>
Wed, 31 Dec 2014 16:01:30 +0000 (17:01 +0100)
committerMaximilian Mader <max@bastelstu.be>
Fri, 29 May 2015 01:49:01 +0000 (03:49 +0200)
wcfsetup/install/files/lib/system/cache/source/RedisCacheSource.class.php

index b12c5785037be6f5f9e3e67366bf62ed58f6a366..e9ec4c7b99f2bfa66ac527bb3d2452e90ee7ad16 100644 (file)
@@ -124,12 +124,23 @@ class RedisCacheSource implements ICacheSource {
                
                // check if entry is parameterized
                if (isset($parts[1])) {
+                       $key = $this->getCacheName($parts[0]);
+                       
                        // save parameterized cache entries as field in a hashset
-                       $this->redis->hset($parts[0], $parts[1], serialize($value));
+                       // saving in a hashset is safe as the smallest lifetime of its fields is set as TTL for the whole hashset
+                       $this->redis->hset($key, $parts[1], serialize($value));
+                       
+                       $keyTTL = $this->redis->ttl($key);
+                       $newTTL = $this->getTTL($maxLifetime);
+                       
+                       // set a new TTL if no TTL is set or if the current TTL is longer than the new one.
+                       if ($keyTTL < 0 || $keyTTL > $newTTL) {
+                               $this->redis->expire($key, $newTTL);
+                       }
                }
                else {
                        // save normal cache entries as simple key
-                       $this->redis->setex($cacheName, $this->getTTL($maxLifetime), serialize($value));
+                       $this->redis->setex($this->getCacheName($cacheName), $this->getTTL($maxLifetime), serialize($value));
                }
        }