<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
Class Cache_util
{
function __construct($params=array())
{
$this->ci = & get_instance();
if(empty($params)||!is_array($params))
$params=array('adapter'=>$this->ci->config->item('cache_adapter'));
$this->ci->load->driver('cache',$params);
}
public function save($top_folder,$idtype,$objectid,$data)
{
$cacheInfo = $this->getCachInfo($top_folder,$idtype,$objectid);
if(!is_dir($cacheInfo['cache_path']))
{
mkdirs($cacheInfo['cache_path']);
}
$oldPath = $this->ci->config->item('cache_path');
$this->ci->cache->set_cache_path($cacheInfo['cache_path']);
$this->ci->cache->save($cacheInfo['cache_id'],$data,$this->ci->config->item('biz_cache_time'));
$this->ci->cache->set_cache_path($oldPath);
}
public function get($top_folder,$idtype,$objectid)
{
$cacheInfo = $this->getCachInfo($top_folder,$idtype,$objectid);
$oldPath = $this->ci->config->item('cache_path');
$this->ci->cache->set_cache_path($cacheInfo['cache_path']);
$stats = $this->ci->cache->get($cacheInfo['cache_id']);
$this->ci->cache->set_cache_path($oldPath);
return $stats;
}
public function delete($top_folder,$idtype,$objectid)
{
$cacheInfo = $this->getCachInfo($top_folder,$idtype,$objectid);
$oldPath = $this->ci->config->item('cache_path');
$this->ci->cache->set_cache_path($cacheInfo['cache_path']);
@$this->ci->cache->delete($cacheInfo['cache_id']);
$this->ci->cache->set_cache_path($oldPath);
}
// @return array: cache_path, cache_id
private function getCachInfo($top_folder,$idtype,$objectid)
{
$longid = str_pad($objectid,9, "0", STR_PAD_LEFT);
$info['cache_path'] = APPPATH.'cache/'.$top_folder.'/'.substr($longid,0,3).'/'.substr($longid,3,3).'/';
$info['cache_id'] = $top_folder.'_'.$idtype.'_'.$objectid;
return $info;
}
}
?>