nciaer 发表于 2019-6-26 11:40:41

PHPCMS中缓存函数的使用

phpcms中有两个缓存函数,setcache和getcache。原型如下:
setcache($name, $data, $filepath = '', $type = 'file', $config = '', $timeout = 0)
getcache($name, $filepath = '', $type = 'file', $config = '')

setcache基本上只是使用前两个参数,name是缓存名,data是缓存的数组数据,getcache只需传入缓存名就会返回缓存数据。

type表示是缓存类型,默认是file文件缓存,我也只用过这个,但是这个没有有效期的设置,需要自己判断缓存有效期,memcache缓存下,timeout参数才会有效。

缓存文件一般在caches\chace_MODULE_M\caches_data\NAME.cache.php。MODULE_M是当前模块名。NAME是缓存名。

直接上个例子吧:
$cachefile = CACHE_PATH.'caches_'.ROUTE_M.'/caches_data/students.cache.php';
if(file_exists($cachefile) && SYS_TIME - filemtime($cachefile) < 1800) { // 这里我加上了缓存有效期设置
      $students = getcache('students');
} else {
      $students = array(1, 2, 3);
      setcache('students', $students);
}


页: [1]
查看完整版本: PHPCMS中缓存函数的使用