微擎缓存

xiaoxiao2021-02-28  20

数据缓存是指将一些 PHP 变量存储到缓存中,使用时再从缓存中取回,避免过多的操作直接从数据库中存取,减轻数据库压力。

微擎系统提供一系列的操作缓存的函数,不需要开发者手动加载引入。前段时间用了下微擎封装的缓存函数,十分简单好用。在此和大家简单的分享下其用法 微擎默认的缓存类型为mysql存储。 微擎系统支持三种缓存(mysql, memcache, file),默认是 mysql 缓存,同一时间只能启用其中一种。 配置缓存在系统 “data/config.php” 文件中,具体配置如下:

$config['setting']['cache'] = 'mysql';// 还可以是memcache, file //如果开启了memcache后,还需要指定memcache的服务器及端口配置

Memcache 缓存设置

缓存到表内需要了解下缓存表:

贴出我的代码:

defined('IN_IA') or exit('Access Denied'); global $_W,$_GPC; load()->func('tpl'); $dos = array('cache','list'); //做一个判断 是否已经缓存 $cache_load = cache_load('catch_sum'); if(empty($cache_load)){ $do = 'cache'; //缓存为空 先跳到缓存 } else { $do = in_array($do, $dos) ? $do : 'list'; } if ($do == 'list') { /**由于代码过长,在此省略了一些代码****/ //下面是调用缓存 $sum = cache_load('catch_sum'); $total = cache_load('catch_total'); $new = cache_load('catch_new'); $jing_num = cache_load('catch_jing_num'); } if ($do == 'cache') { $yesterday = date('Ymd', strtotime('-1 days')); $sum = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('account_wechats')); cache_write('catch_sum', $sum); //缓存列表数据总数 $total = pdo_fetch('SELECT SUM(`cumulate`) FROM ' . tablename('stat_fans')." where date = ".$yesterday); cache_write('catch_total', $total["SUM(`cumulate`)"]); //缓存列表数据总和 $new = pdo_fetch('SELECT SUM(`new`) FROM ' . tablename('stat_fans')." where date = ".$yesterday); cache_write('catch_new', $new["SUM(`new`)"]); //缓存新增 $cancel = pdo_fetch('SELECT SUM(`cancel`) FROM ' . tablename('stat_fans')." where date = ".$yesterday); $jing_num = $new["SUM(`new`)"] - $cancel["SUM(`cancel`)"]; cache_write('catch_jing_num', $jing_num); //缓存净增 message('数据更新成功!',url('community/weixincount/list'),'success'); }

cache_write() - 按照指定的键名存储缓存数据

cache_write($key, $data) $key 参数指定要存储缓存数据的键名,键名必须保证是唯一 $data 参数指定要存储数据的内容,可以为字符串,数组等

cache_load() - 读取指定键名的缓存数据

cache_load(string $key) $key 参数指定要读取缓存数据的键名

cache_delete() - 删除指定的缓存

cache_delete(string $key) $key 参数指定要删除缓存数据的键名

cache_clean() - 清空所有缓存

cache_clean();
转载请注明原文地址: https://www.6miu.com/read-431093.html

最新回复(0)