Implement RedisCacheSource::set()
authorMaximilian Mader <max@bastelstu.be>
Sun, 21 Dec 2014 01:54:15 +0000 (02:54 +0100)
committerMaximilian Mader <max@bastelstu.be>
Fri, 29 May 2015 01:49:00 +0000 (03:49 +0200)
wcfsetup/install/files/lib/system/cache/source/RedisCacheSource.class.php

index a87216b77a7aae96de6c17751fd86a26a987511c..a1ee065cb02506aedaae40672634e23244efe198 100644 (file)
@@ -83,10 +83,34 @@ class RedisCacheSource implements ICacheSource {
                
        }
        
+       /**
+        * Returns time to live in seconds, defaults to 3 days.
+        * 
+        * @param       integer         $maxLifetime
+        * @return      integer
+        */
+       protected function getTTL($maxLifetime = 0) {
+               if ($maxLifetime) return $maxLifetime;
+               
+               // default to 3 days
+               return 60 * 60 * 24 * 3;
+       }
+       
        /**
         * @see \wcf\system\cache\source\ICacheSource::set()
         */
        public function set($cacheName, $value, $maxLifetime) {
+               // split parameterized cache entry names into cache name and cache index
+               $parts = explode('-', $cacheName, 2);
                
+               // check if entry is parameterized
+               if (isset($parts[1])) {
+                       // save parameterized cache entries as field in a hashset
+                       $this->redis->hset($parts[0], $parts[1], serialize($value));
+               }
+               else {
+                       // save normal cache entries as simple key
+                       $this->redis->setex($cacheName, $this->getTTL($maxLifetime), serialize($value));
+               }
        }
 }