Guest
Public paste!

Untitled

By: a guest | Feb 20th, 2010 | Syntax: PHP | Size: 2.61 KB | Hits: 54 | Expires: Never
Copy text to clipboard
  1. <?php
  2. /*
  3.  * Limb PHP Framework
  4.  *
  5.  * @link http://limb-project.com
  6.  * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
  7.  * @license    LGPL http://www.gnu.org/copyleft/lesser.html
  8.  */
  9.  
  10. lmb_require_class('limb/cache2/src/lmbTaggableCache.class.php', 'lmbTaggableCache');
  11.  
  12. class lmbCacheHelper
  13. {
  14.   protected $_cache;
  15.   protected $_tcache;
  16.   protected $_mcache;
  17.   protected $_config;
  18.   protected $_prefix;
  19.  
  20.   function __construct(lmbCacheAbstractConnection $cache_connection, $config, $prefix = '')
  21.   {
  22.     $this->_cache = $cache_connection;
  23.     $this->_tcahce = new lmbTaggableCache($this->_cache);
  24.     $this->_config = $config;
  25.     $this->_prefix = $prefix;
  26.   }
  27.  
  28.   function getCacheInfo($name, $vars)
  29.   {
  30.     if(!isset($this->_config[$name]))
  31.       throw new lmbException('Not found info for cache "'.$name.'"!');
  32.     $raw_info = $this->_config[$name];
  33.     $key = 'k'.$name;
  34.     if(isset($raw_info['vars']))
  35.     {
  36.       ksort($vars);
  37.       foreach($raw_info['vars'] as $vn)
  38.         if(isset($vars[$vn]))
  39.           $key .= $vn.'='.$vars[$vn].';';
  40.         else
  41.           throw new lmbException('Not set value for var '.$vn);
  42.     }
  43.     $info = array();
  44.     $info['ttl'] = $raw_info['ttl'];
  45.     $info['tags'] = isset($raw_info['tags']) ? $raw_info['tags'] : array();
  46.     if(isset($raw_info['tag_ids']))
  47.       foreach($raw_info['tag_ids'] as $t => $v)
  48.         if(isset($vars[$v]))
  49.           $info['tags'][] = $t.'-id:'.$vars[$v];
  50.         else
  51.           throw new lmbException('Not set value for var '.$v);
  52.     $info['key'] = $key.'@t:'.implode(',', $info['tags']);
  53.  
  54.     if($this->_prefix)
  55.     {
  56.       $info['key'] = $this->_prefix.$info['key'];
  57.       foreach($info['tags'] as $n => $v)
  58.         $info['tags'][$n] = $this->_prefix.$v;
  59.     }
  60.  
  61.     return $info;
  62.   }
  63.  
  64.   function deleteByTagsFor($obj)
  65.   {
  66.     if(!is_string($obj))
  67.     {
  68.       $table = $obj->getTableName();
  69.       $this->_tcahce->deleteByTag($this->_prefix.$table);
  70.       if($obj->has('id'))
  71.         $this->_tcahce->deleteByTag($this->_prefix.$table.'-id:'.$obj->id);
  72.     }
  73.     else
  74.       $this->_tcahce->deleteByTag($this->_prefix.$obj);
  75.   }
  76.  
  77.   function flushCache()
  78.   {
  79.     $this->_cache->flush();
  80.   }
  81.  
  82.   function callCache($name, $function, $params = array(), $vars = array())
  83.   {
  84.     $info = $this->getCacheInfo($name, $vars);
  85.     if(null !== ($value = $this->_tcahce->get($info['key'])))
  86.       return $value;
  87.     $value = call_user_func_array($function, $params);
  88.     if(!$value)
  89.       $value = false;
  90.     $this->_tcahce->set($info['key'], $value, $info['ttl'], $info['tags']);
  91.     return $value;
  92.   }
  93. }