控制器使用-redis

需要开启 php_redis 扩展。redis 相关知识:https://blog.csdn.net/qq_39497281/article/details/126123138


设置缓存配置

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

				
<?php
return [
	// 缓存设置
	'cache'             => [
		'type'          => 'redis',//支持file, memcache, redis缓存类型
		'host'          => '127.0.0.1', // 主机地址 [ 'memcache', 'redis' 需要设置 ]
		'password'      => '', // 对应各类服务的密码, 为空代表不需要密码
		'port'          => '6379', // 端口 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');
    }
    
    //删除全部缓存
    public function delall(){
	$this->delCache('*');
    }
}