手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表Tag:include

ssdb的Yii cache扩展

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

不会编程的程序员

【内容全是转载,觉得挺有意思。。。】
我想这让人难以置信,但是通过Twitter和电子邮件渠道蜂拥而来的报道表 明:许多参加程序员面试的求职者根本就不会编程,看看迈克·林最近的邮件就知道了:

为啥程序员 都不会编程呢?》 这篇文章让我改变了面试的方式。以前面试我开始都以营造融洽气氛为主。正如你提到的,事实证明这种方式太耗费时间了,大多数应聘者根本就 不会技术。因此我以技术问题作为开头来面试应聘者,刚开始的时候,问题的难度是从容易逐渐变难。后来我发现反过来的话,甄别应聘者的速度会快些,即难的问 题先问(前提是难的问题必须属于“工作必备技能”一类的问题)。多数面试仍然会花上20分钟作用,因为回答和评估仍然需要花上一些时间。但是这对于之前” 先营造融洽气氛“的面试方式来说一个相当大的改进,而且还可以通过电话方式来完成。

在读了你的文章之 后,我开始通过电话,网络会议方式进行编程面试,甄别不会编程的应聘者(也就是大多数人)所需时间降到了15分钟左右。

我写那篇文章的时候时间还是2007年,三年后当我再次听人说应聘编程工作的绝大多数所谓的程序员竟 然不能写一小段程序时,我震惊了,但是也并没有完全出乎我的意料。需要清楚的是,难是一个相对的词汇——我们不是指那种复杂的有如Google计算机专业 研究生难度的面试题。我们给应聘者做的都是一些极其简单的题目,但是他们不会。这就相当于你想雇一位卡车司机,但是却发现90% 的应聘者竟然找不到油门和变速挡

我也同意,这有些疯狂,但是这样的事情每天都在发生,而且这是我们这个行业在招人时常见的问题。

你需要通过这种简单的技术面试题来筛选掉这些伪程序员的大军。 我在前面提到过的,通过电话筛选是个明智的选择。不过通过网络方式来筛选效果要更好,并且更自然些。

XML/HTML代码
  1. 我还是非常不习惯通过远程共享对方电脑桌面的方式与应聘者进行网络面试。我在网上搜索 了一下类似“纸笔”方式的编程面试的工具,但是没有收获。所以我做了一件所有有自尊心的程序员都会做的事情。我自己写了一个。伙计,花在这个程序上面 的精力很值!对每个应聘者的初期技术筛选所花的时间,我安排了15分钟。但是遗憾的时,我一般只需5~10分钟就能结束。如果他们能写10行简单的 代码,我就会给他们安排一次真正的面试。但是这种事情并不经常有,不过也好,至少我不会再浪费大量的时间了。
迈克加了一个声明,称他这个自制的编程测验工具并不是想秀一下他的编程能力如何。他需要一个这样的工具,所以他就写了一个——并且非常贴心的与大家分享了。 或许除了这个还有其他的工具;你们大多数人都使用什么在线工具来筛选程序员?

三年后,我仍然想知道:为什么一点都不会写程序的人都会冒出他们可以胜任程序员工作的念头来呢? 的确,他们中有一些人会如愿以偿。但这也意味这个程序员这个行业的面试标准非常不合适。这是一种耻辱。它让每个在职的程序员蒙羞

水平差的程序还至少还可以接受教育来提高,而伪程序员不仅无药可就,而且还会让他周围 的同事自掉身价。这类人需要铲除,要做到这点就需要我们从最简单的技术编程测试做起,而这类测试也应该成为每程序员面试的一个组成部分。

本文来自东西 dongxi.net
来源: codinghorror | 作者:Jeff Atwood | 译者:neodreamer
声明:译者neodreamer拥有本文版权并授权东西网发布,非商业转载请参考东西网版权声明中的非商业用途转载版权说明;商业转载请联系东西网