Untitled
By: a guest | Feb 20th, 2010 | Syntax:
PHP | Size: 2.61 KB | Hits: 54 | Expires: Never
<?php
/*
* Limb PHP Framework
*
* @link http://limb-project.com
* @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
* @license LGPL http://www.gnu.org/copyleft/lesser.html
*/
lmb_require_class('limb/cache2/src/lmbTaggableCache.class.php', 'lmbTaggableCache');
class lmbCacheHelper
{
protected $_cache;
protected $_tcache;
protected $_mcache;
protected $_config;
protected $_prefix;
function __construct(lmbCacheAbstractConnection $cache_connection, $config, $prefix = '')
{
$this->_cache = $cache_connection;
$this->_tcahce = new lmbTaggableCache($this->_cache);
$this->_config = $config;
$this->_prefix = $prefix;
}
function getCacheInfo($name, $vars)
{
if(!isset($this->_config
[$name]))
throw new lmbException('Not found info for cache "'.$name.'"!');
$raw_info = $this->_config[$name];
$key = 'k'.$name;
if(isset($raw_info['vars']))
{
foreach($raw_info['vars'] as $vn)
$key .= $vn.'='.$vars[$vn].';';
else
throw new lmbException('Not set value for var '.$vn);
}
$info['ttl'] = $raw_info['ttl'];
$info['tags'] = isset($raw_info['tags']) ?
$raw_info['tags'] : array();
if(isset($raw_info['tag_ids']))
foreach($raw_info['tag_ids'] as $t => $v)
$info['tags'][] = $t.'-id:'.$vars[$v];
else
throw new lmbException('Not set value for var '.$v);
$info['key'] = $key.'@t:'.implode(',', $info['tags']);
if($this->_prefix)
{
$info['key'] = $this->_prefix.$info['key'];
foreach($info['tags'] as $n => $v)
$info['tags'][$n] = $this->_prefix.$v;
}
return $info;
}
function deleteByTagsFor($obj)
{
{
$table = $obj->getTableName();
$this->_tcahce->deleteByTag($this->_prefix.$table);
if($obj->has('id'))
$this->_tcahce->deleteByTag($this->_prefix.$table.'-id:'.$obj->id);
}
else
$this->_tcahce->deleteByTag($this->_prefix.$obj);
}
function flushCache()
{
$this->_cache->flush();
}
function callCache
($name, $function, $params = array(), $vars = array())
{
$info = $this->getCacheInfo($name, $vars);
if(null !== ($value = $this->_tcahce->get($info['key'])))
return $value;
if(!$value)
$value = false;
$this->_tcahce->set($info['key'], $value, $info['ttl'], $info['tags']);
return $value;
}
}