123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- namespace Qii\Cache;
- \Qii\Autoloader\Import::requires(array(dirname(__FILE__) . DS . 'Redis/Client.php', dirname(__FILE__) . DS . 'Redis/Cluster.php'));
- /**
- * PHP 操作 redis
- * @author Jinhui.Zhu
- *
- */
- class Redis implements Intf
- {
- const VERSION = '1.2';
- public $redis;
- protected $policy = array(
- /**
- * 缓存服务器配置,参看$_default_server
- * 允许多个缓存服务器
- */
- 'servers' => array(['host' => '127.0.0.1', 'port' => '6379']),
- /**
- * 缓存有效时间
- *
- * 如果设置为 0 表示缓存永不过期
- */
- 'life_time' => 900
- );
- public function __construct(array $policy = null)
- {
- if (!extension_loaded('redis')) {
- throw new \Qii\Exceptions\MethodNotFound(\Qii::i(1006), __LINE__);
- }
- if (!empty($policy)) {
- $this->policy = array_merge($this->policy, $policy);
- }
- $redisServer = array();
- foreach ($this->policy['servers'] AS $value) {
- $redisServer[] = array('host' => $value['host'], 'port' => $value['port']);
- }
- $this->redis = new \Qii\Cache\Redis\Cluster($redisServer, 128);
- }
- /**
- * 保存指定key的数据
- */
- public function set($id, $data, array $policy = null)
- {
- if (!isset($policy['life_time'])) $policy['life_time'] = $this->policy['life_time'];
- try {
- $this->redis->hMset($id, $data);
- if (isset($policy['life_time']) && $policy['life_time'] > 0) {
- $this->redis->setTimeout($id, $policy['life_time']);
- }
- } catch (\CredisException $e) {
- throw new \Qii\Exceptions\Errors(\Qii::i(-1, $e->getMessage()), __LINE__);
- }
- }
- /**
- * 获取指定key的数据
- */
- public function hGet($id)
- {
- if ($this->redis->exists($id)) {
- return $this->redis->hGetAll($id);
- }
- return null;
- }
- /**
- * 获取指定key的数据
- */
- public function get($id)
- {
- if ($this->redis->exists($id)) {
- return $this->redis->get($id);
- }
- return null;
- }
- /**
- * 删除指定key的数据
- */
- public function remove($id)
- {
- if ($this->redis->exists($id)) {
- return $this->redis->delete($id);
- }
- }
- /**
- * 清除当前db的所有数据
- */
- public function clean()
- {
- $this->redis->flushdb();
- }
-
- public function __call($method, $args)
- {
- return call_user_func_array(array($this->redis, $method), $args);
- }
- }
|