控制器使用-memcache

需要开启 php_memcache 扩展。memcache 相关知识:https://blog.csdn.net/qq_31432773/article/details/125011612


设置缓存配置

配置文件位置:config/config.php

				
<?php
return [
	// 缓存设置
	'cache'             => [
		'type'          => 'memcache',//支持file, memcache, redis缓存类型
		'host'          => '127.0.0.1', // 主机地址 [ 'memcache', 'redis' 需要设置 ]
		'port'          => '11211', // 端口 memcache 一般端口为 11211, redis 一般为 6379
		'pre'           => 'ue_'// 缓存变量前缀
	]
];
				
			

演示示例

缓存数据会保存为控制器的成员变量,规则 $this->缓存名称;

				
<?php
class indexController extends Mb{
	
    //根据缓存情况设置、读取缓存数据
    public function index(){
        $this->cache('test', '__getData', 12);
        p($this->test);
    }
    
    public function __getData(){
        echo '无缓存,进行查询...<br />';
        $db = db('test');
        $test = $db->fetchAll();
        return $test;
    }
    
    //删除指定的缓存
    public function del(){
		$this->delCache('test','12');
    }
}