thinkphp使用文件缓存的实例

本文介绍了一种基于PHP的缓存实现方式,通过MD5算法为不同请求生成唯一缓存前缀,有效避免缓存污染。当接收到HTTP请求时,系统会检查是否存在对应的缓存数据,如果存在则直接返回缓存数据;若不存在,则执行业务逻辑并存储结果到缓存中以备后续使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


中间省略了设置  

缓存的前缀是md5的传入参数

$input = I('get.');

// cache instance
$prefix = 'matching_index' . md5(__METHOD__ . serialize($input));
$Cache = Cache::getInstance('File', array('expire' => '600', 'prefix' => $prefix));

// check cache
if ($user_show = $Cache->get('user_show')) {
    var_dump('有缓存的情况');
    $total_data = $Cache->get('total_data');
    $user_list = $Cache->get('user_list');
    $input = $Cache->get('input');
    $Page = $Cache->get('page');

} else {

    // there is no cache
    var_dump('没有缓存的情况');
    $stat_show = $this->listShowWithNoCache($Cache);
    $total_data = $stat_show['total_data'];
    $user_show = $stat_show['user_show'];
    $user_list = $stat_show['user_list'];
    $input = $stat_show['input'];
    $Page = $stat_show['page'];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值