52 lines
1.4 KiB
PHP
52 lines
1.4 KiB
PHP
<?php
|
|
|
|
|
|
class Base_model extends HD_Model
|
|
{
|
|
protected $_cache_key_prefix = 'LiChe_bobing';
|
|
public $mc_cache_expire = 600, $table_name;
|
|
|
|
function __construct()
|
|
{
|
|
parent::__construct($this->table_name, 'default');
|
|
}
|
|
|
|
protected function cache_key($params)
|
|
{
|
|
return $this->_cache_key_prefix . md5($this->table_name . $params);
|
|
}
|
|
|
|
/**
|
|
* 对方法的某个结果集做缓存
|
|
* @param $func
|
|
* @param array $param
|
|
* @param string $ttl
|
|
* @param string $type
|
|
* @return mixed
|
|
*/
|
|
public function cache($func, $param = array(), $ttl = '', $type = 'mc')
|
|
{
|
|
$cache = &load_cache($type);
|
|
$cache_key = $this->cache_key($type . $func . json_encode($param));
|
|
$ttl > 0 && $this->mc_cache_expire = intval($ttl);
|
|
if (!$rt = $cache->get($cache_key)) {
|
|
$rt = call_user_func_array(array($this, $func), $param);
|
|
$cache->save($cache_key, $rt, $this->mc_cache_expire);
|
|
}
|
|
return $rt;
|
|
}
|
|
|
|
/**
|
|
* 删除某方法某个结果集的缓存
|
|
* @param $func
|
|
* @param array $param
|
|
* @param string $type
|
|
* @return mixed
|
|
*/
|
|
public function un_cache($func, $param = array(), $type = 'mc')
|
|
{
|
|
$cache = &load_cache($type);
|
|
$cache_key = $this->cache_key($type . $func . json_encode($param));
|
|
return $cache->delete($cache_key);
|
|
}
|
|
} |