Geen omschrijving

CacheRedis.class.php 5.3KB

    <?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK IT ] // +---------------------------------------------------------------------- // | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: 尘缘 <130775@qq.com> // +---------------------------------------------------------------------- // $Id: CacheRedis.class.php 2787 2012-02-28 08:50:32Z shuhai.sir@gmail.com $ /** +------------------------------------- * CacheRedis缓存驱动类 * 要求安装phpredis扩展:https://github.com/owlient/phpredis +------------------------------------- */ class CacheRedis extends Cache { /** +---------------------------------------------------------- * 架构函数 +---------------------------------------------------------- * @access public +---------------------------------------------------------- */ public function __construct($options='') { if ( !extension_loaded('redis') ) { throw_exception(L('_NOT_SUPPERT_').':redis'); } if(empty($options)) { $options = array ( 'host' => C('REDIS_HOST') ? C('REDIS_HOST') : '127.0.0.1', 'port' => C('REDIS_PORT') ? C('REDIS_PORT') : 6379, 'timeout' => C('DATA_CACHE_TIMEOUT') ? C('DATA_CACHE_TIMEOUT') : false, 'persistent' => false, 'expire' => C('DATA_CACHE_TIME'), 'length' => 0, ); } $this->options = $options; $func = $options['persistent'] ? 'pconnect' : 'connect'; $this->handler = new Redis; $this->connected = $options['timeout'] === false ? $this->handler->$func($options['host'], $options['port']) : $this->handler->$func($options['host'], $options['port'], $options['timeout']); } /** +---------------------------------------------------------- * 是否连接 +---------------------------------------------------------- * @access private +---------------------------------------------------------- * @return boolen +---------------------------------------------------------- */ private function isConnected() { return $this->connected; } /** +---------------------------------------------------------- * 读取缓存 +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param string $name 缓存变量名 +---------------------------------------------------------- * @return mixed +---------------------------------------------------------- */ public function get($name) { N('cache_read',1); return $this->handler->get($name); } /** +---------------------------------------------------------- * 写入缓存 +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param string $name 缓存变量名 * @param mixed $value 存储数据 * @param integer $expire 有效时间(秒) +---------------------------------------------------------- * @return boolen +---------------------------------------------------------- */ public function set($name, $value, $expire = null) { N('cache_write',1); if(is_null($expire)) { $expire = $this->options['expire']; } if(is_int($expire)) { $result = $this->handler->setex($name, $expire, $value); }else{ $result = $this->handler->set($name, $value); } if($result && $this->options['length']>0) { // 记录缓存队列 $this->queue($name); } return $result; } /** +---------------------------------------------------------- * 删除缓存 * +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param string $name 缓存变量名 +---------------------------------------------------------- * @return boolen +---------------------------------------------------------- */ public function rm($name) { return $this->handler->delete($name); } /** +---------------------------------------------------------- * 清除缓存 +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @return boolen +---------------------------------------------------------- */ public function clear() { return $this->handler->flushDB(); } }