quarta-feira, 7 de janeiro de 2015

2 Level Cache - Redis/Memcached and APC

So, you have a cache all over your code and now need some more. Just add a second level cache service, works great in multi-server environment.

Assuming you have a cache interface similar to this:

 interface CacheInterface  
 {  
   public function get($key)  
   public function set($key, $value, $ttl = null)  
   public function delete($key)  
   public function exists($key)  
 }  

Our standard cache implementation should look something like


 class APCCache implements CacheInterface  
 {  
   public function get($key)  
   {  
     return apc_fetch($key);  
   }  
   public function set($key, $value, $ttl = null)  
   {  
     apc_store($key,$value,$ttl);  
   }  
   public function delete($key)  
   {  
     return apc_delete($key);  
   }  
   public function exists($key)  
   {  
     return apc_exists($key);  
   }  
 }  

Now adding a second cache implementation, like Redis:

 class RedisCache implements CacheInterface  
 {  
      private $redis;  
      public function __contruct(Redis $redis)   
      {  
           $this->redis = $redis;  
      }  
   public function get($key)  
   {  
           return $this->redis->get($key);  
   }  
   public function set($key, $value, $ttl = null)  
   {  
           $this->redis->set($key, $value, $ttl);            
   }  
   public function delete($key)  
   {  
           $this->redis->del($key);            
   }  
   public function exists($key)  
   {  
           return $this->redis->exists($key);  
   }  
 }   

Now the real code that create a 2 level cache using 2 cache implementations:

 class MultiCache implements CacheInterface  
 {  
      private $cache1;  
      private $cache2;  
      public function __contruct(CacheInterface $level1, CacheInterface $level2)   
      {  
           $this->cache1 = $level1;  
           $this->cache2 = $level2;  
      }  
      public function get($key)  
      {  
           if ($value = $this->cache1->get($key)) {  
                return $value;  
           }  
           if ($value = $this->cache2->get($key)) {       
                // copy to "local" cache first  
                // $ttl = need to somehow get from cache get  
                $this->cache1->set($key, $value, $ttl);  
                return $value;  
           }  
           return false;  
      }  
      public function set($key, $value, $ttl = null)  
      {  
           $this->cache1->set($key, $value, $ttl);  
           $this->cache2->set($key, $value, $ttl);  
      }  
      public function delete($key)  
      {  
           $this->cache1->delete($key);  
           $this->cache2->delete($key);  
      }  
      public function exists($key)  
      {  
           return $this->cache1->exists($key) || $this->cache2->exists($key);  
      }  
 }   

On your current code you replace the APC cache with the multi-level like this:

 // Multi level cache usage sample  
 $cache = new MultiCache(new APCCache(), new RedisCache(new Redis('127.0.0.1')));  
 // try to fetch object from "local" and "remote" cache  
 $key = 'myname';  
 if (!($aValue = $cache->get($key))) {  
      // our content to cache  
      $aValue = 'jose';  
      // not found on either caches   
      // this will cache on "local" and "remote"  
      $cache->set($key, $aValue);  
 }