手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆

ssdb的Yii cache扩展

首页 > PHP Framework >

google的leveldb越来越被很多人接受。国内的ideawu基于leveldb还写了一个ssdb的前置扩展用来实现了很多功能,比如标准的getset和hget,hset还有zset,zget,也实现了队列。当然pub/sub就没有办法实现了。毕竟它和redis还是有点区别。

基于标准的ssdb的类,写了个小扩展,扩展了Yii的Cache类:

PHP代码
  1. class CSsdbCache extends CCache  
  2. {  
  3.     /** 
  4.      * @var string hostname to use for connecting to the redis server. Defaults to '127.0.0.1'. 
  5.      */  
  6.     public $hostname = '127.0.0.1';  
  7.     /** 
  8.      * @var int the port to use for connecting to the ssdb server. Default port is 8888. 
  9.      */  
  10.     public $port = 8888;  
  11.     /** 
  12.      * @var float 
  13.      */  
  14.     public $timeout = 2000;  
  15.     public $serializer = false;  
  16.     public $_cache;  
  17.     protected $_cachekeys = 'ssdb_cachekey';  
  18.       
  19.     public function init() {  
  20.         parent::init();  
  21.     }  
  22.     /** 
  23.      * @return SSDB 
  24.      */  
  25.     public function getSsdbCache() {  
  26.         if ($this->_cache !== null)  
  27.             return $this->_cache;  
  28.         else {  
  29.             return $this->_cache = new SimpleSSDB($this->hostname, $this->port, $this->timeout);  
  30.         }  
  31.     }  
  32.     public function getkeys() {  
  33.         return $this->getSsdbCache()->hkeys($this->_cachekeys, """"$this->getSsdbCache()->hsize($this->_cachekeys));  
  34.     }  
  35.     /** 
  36.      * Retrieves a value from cache with a specified key. 
  37.      * This is the implementation of the method declared in the parent class. 
  38.      * @param string $key a unique key identifying the cached value 
  39.      * @return string|boolean the value stored in cache, false if the value is not in the cache or expired. 
  40.      */  
  41.     protected function getValue($key) {  
  42.         return unserialize($this->getSsdbCache()->get($key));  
  43.     }  
  44.   
  45.     /** 
  46.      * Stores a value identified by a key in cache. 
  47.      * This is the implementation of the method declared in the parent class. 
  48.      * @param string  $key    the key identifying the value to be cached 
  49.      * @param string  $value  the value to be cached 
  50.      * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire. 
  51.      * @return boolean true if the value is successfully stored into cache, false otherwise 
  52.      */  
  53.     protected function setValue($key$value$expire) {  
  54.         $this->getSsdbCache()->hset($this->_cachekeys, $key, 1);  
  55.         if ($expire > 0) {  
  56.             //$expire += time();  
  57.             return $this->getSsdbCache()->setx($key, serialize($value), (int) $expire);  
  58.         }  
  59.         else {  
  60.             return $this->getSsdbCache()->set($key, serialize($value));  
  61.         }  
  62.     }  
  63.     /** 
  64.      * Stores a value identified by a key into cache if the cache does not contain this key. 
  65.      * This is the implementation of the method declared in the parent class. 
  66.      * @param string  $key    the key identifying the value to be cached 
  67.      * @param string  $value  the value to be cached 
  68.      * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire. 
  69.      * @return boolean true if the value is successfully stored into cache, false otherwise 
  70.      */  
  71.     protected function addValue($key$value$expire) {  
  72.         return $this->setValue($key$value$expire);  
  73.     }  
  74.     /** 
  75.      * Deletes a value with the specified key from cache 
  76.      * This is the implementation of the method declared in the parent class. 
  77.      * @param string $key the key of the value to be deleted 
  78.      * @return boolean if no error happens during deletion 
  79.      */  
  80.     protected function deleteValue($key) {  
  81.         $this->getSsdbCache()->hdel($this->_cachekeys, $key);  
  82.         return $this->getSsdbCache()->del($key);  
  83.     }  
  84.     /** 
  85.      * @return boolean whether the flush operation was successful. 
  86.      */  
  87.     protected function flushValues() {  
  88.         $this->getSsdbCache()->multi_del($this->getkeys());  
  89.         return $this->getSsdbCache()->hclear($this->_cachekeys);  
  90.     }  
  91. }  

其实代码很简单,不过由于ssdb默认没有serialize功能,所以在存储之前,得先主动的serialize,然后get的时候unserialize。不然就没有办法存储数组了。

由于ssdb没有flush功能。所以利用hget/hset将所有的key存储下来。flush的时候把hget获取的key读出来删除。然后再清掉这个hget的key

最后还有expire。ssdb里的setx第三个参数。。。居然不是expire,而是ttl。开始的时候,一直都当成expire。结果浪费了很长时间




本站采用创作共享版权协议, 要求署名、非商业和保持一致. 本站欢迎任何非商业应用的转载, 但须注明出自"易栈网-膘叔", 保留原始链接, 此外还必须标注原文标题和链接.

Tags: yii

« 上一篇 | 下一篇 »

只显示10条记录相关文章

常用网站的反向代理页[2013-09-28] (浏览: 66069, 评论: 10)
Yii CDbCriteria的常用方法 (浏览: 56348, 评论: 5)
将Yiiframework与JQuery easyUI整合使用 (浏览: 38204, 评论: 2)
Yii:relations update(self::STAT) (浏览: 33679, 评论: 0)
值得收藏的yii2的doc中关于db Query的说明 (浏览: 29242, 评论: 0)
Yii Demos 随想 (浏览: 28550, 评论: 3)
在Yii框架中使用Hprose或PHPRPC (浏览: 27561, 评论: 0)
Yii ClinkPager 郁闷 (浏览: 27281, 评论: 2)
Yiiframework(Yii框架)开发笔记:续四 (浏览: 26752, 评论: 3)
Yii 一行代码,为模块绑定子域名 (浏览: 26088, 评论: 0)

1条记录访客评论

其实 Redis 的 setx(setex) 的参数也不是 expire, 而是 ttl. 所以 SSDB 也做了同样的选择. ^_^

Post by ideawu on 2014, July 26, 8:07 PM 引用此文发表评论 #1


发表评论

评论内容 (必填):