账户中心 我的话题 我的评论 退出系统

phpGrace redis 形式的缓存

redis 形式的缓存

php 扩展需求

需要开启 php_memcache 扩展

redis 相关知识:http://www.hcoder.net/books/read_10105.html

修改或添加全局配置 phpGrace/config.php

//缓存类型
'allowCacheType'     => array('file', 'memcache', 'redis'),
//缓存设置
'cache'             => array(
	'type'          => 'redis', // 缓存类型
	'host'          => '127.0.0.1', // redis 主机地址
	'password'      => '',// 密码, 为空代表不需要密码
	'port'          => '6379', // 端口 
	'pre'           => 'grace_' // 缓存名称前缀
)

演示代码

<?php
class indexController extends grace{
    public function index(){
        $this->cache('test', 12, '__getData');
        p($this->test);
    }
    
    public function __getData(){
        echo '无缓存,进行查询...<br />';
        $db = db('persons');
        $persons = $db->fetchAll();
        return $persons;
    }
    
    public function t(){
        $this->removeCache('test', '12');
    }
    
    public function t2(){
        $this->clearCache();
    }
}