当后台配置了Memcache服务器信息时,Dedecms会优先选择使用Memcache进行缓存操作。如果未配置Memcache服务器,则系统将自动切换到文件缓存模式。 ### 1. 缓存操作函数 在Dedecms的`cache.helper.php`文件中,定义了三个核心函数用于管理缓存:`SetCache()`、`GetCache()` 和 `DelCache()`。 #### 1.1 设置缓存 (`SetCache()`)
function SetCache($prefix, $key, $value, $timeout = 3600, $is_memcache = TRUE)
if (!empty($cache_helper_config['memcache']) &&
$cache_helper_config['memcache']['is_mc_enable'] === 'Y' &&
$is_memcache === TRUE) {
$result = $GLOBALS['mc_' . $mc_path['host']]->set($key, $value, MEMCACHE_COMPRESSED, $timeout);
}
$key = substr($key, 0, 2) . '/' . substr($key, 2, 2) . '/' . substr($key, 4, 2) . '/' . $key;
$tmp['data'] = $value;
$tmp['timeout'] = time() + (int)$timeout;
$cache_data = "\\n\\r" . @serialize($tmp);
return @PutFile(DEDEDATA . "/cache/$prefix/$key.php", $cache_data);
首先检查Memcache中是否存在对应的缓存数据。如果不存在,则尝试从文件缓存中读取。
function GetCache($prefix, $key, $is_memcache = TRUE)
function DelCache($prefix, $key, $is_memcache = TRUE)