August 20th, 2008 by 张磊
前段时间我写了在Rails中使用Ultrasphinx代替acts_as_ferret插件来做全文检索,相应地也会用sphinx取代ferret。具体的做法可以看这篇文章。 先明确下概念,sphinx是类似lucene的一个全文检索实现,而ultrasphinx是rails的插件,有了ultrasphinx,我们可以方便地使用sphinx进行索引、检索。 但我在使用的过程中发现,每次进行检索之后,ultrasphinx都会调用一次或多次find_all_by_id来获得符合条件的ActiveRecords。但如果在项目中用了cache_fu之类的插件,使用memcached做对象缓存后,ultrasphinx依然没法使用缓存。为了避免这成为性能的瓶颈,我开始探索,是否可以让ultrasphinx和cache_fu合作,让搜索的结果也能使用memcached的缓存。 查了一下,ultrasphinx的作者还真的提供了这个口子,虽然他也不确定是否会有人用。打开/vendor/plugin/ultrasphinx/lib/ultrasphinx/search/internals.rb,找到一个叫reify_results的函数,里面有这样一段: finder = ( Ultrasphinx::Search.client_options['finder_methods'].detect do |method_name| klass.respond_to? method_name end or # XXX This default is kind of buried, but I’m not sure why you would need it to be configurable, since you can use ['finder_methods']. “find_all_by_#{klass.primary_key}” ) 也就是说,只要设置Ultrasphinx::Search.client_options['finder_methods']的值,就可以自定义查找ActiveRecord所使用的方法。正巧,在使用cache_fu插件后,有一个get_caches可以达到我们的目标。 于是在config/enviroment.rb中增加一行: Ultrasphinx::Search.client_options['finder_methods'] = ['get_caches'] 然后重新启动应用。试着搜索一下,不出意外会看到一个出错的提示。这里是因为get_caches函数返回数据的格式和find_all_by_id有点小区别。find_all_by_id返回的是一个ActiveRecord的数组,而get_caches返回的是一个数组的数组。只消在上面的internal.rb中的reify_results函数里,找到下面这个地方: records.each do |record| record = record[1] if record.is_a? Array [...]
May 2nd, 2008 by 张磊
At the very beginning I have to firgue out my mistake in this post.The “total_items” tells the total amount of the key=>value pairs that ever in the memory, but the “curr_items” shows the number of current pairs. I’m using memcached on a windows server, so I’ve installed memcached as a service. Every time windows starts, [...]
April 27th, 2008 by 张磊
很多时候需要监控服务器上的Memcached运行情况,比如缓存的查询次数,命中率之类的。但找到的那个memcached-tool是linux下用perl写的,我也没试过windows能不能用。后来发现个简单的办法可以做到,就是使用Telnet。 首先登录到服务器,然后在cmd命令行中键入 telnet 127.0.0.1 11211 其中127.0.0.1是服务器的地址(这里是本机) ,11211是memcached绑定的端口号。 之后命令行窗口全黑只有光标提示,摸黑输入stats,即可得到描述Memcached服务器运行情况的参数。如下图: 其中,uptime 是memcached运行的秒数,cmd_get是查询缓存的次数。这两个数据相除一下就能得到平均每秒请求缓存的次数——最近niupu的流量很低,所以平均也就一秒请求一次多,这么点大的压力,用文件系统缓存一样没问题,根本不会体现出使用memcached的优越。 下面的cmd_set 就是设置key=>value的次数。整个memcached是个大hash,用cmd_get没有找到的内容,就会调用一下cmd_set写进缓存里。紧跟着是get_hits,就是缓存命中的次数。缓存命中率 = get_hits/cmd_get * 100%。 下面的get_misses的数字加上get_hits应该等于cmd_get。而total_itemscurr_items表示现在在缓存中的键值对个数,在图上total_items == cmd_set == get_misses,不过当可用最大内存用光时,memcached就会删掉一些内容,上面的等式就不成立了。 话说回来,memcached要是能有一套完整的监测工具就太好了。memcached的安装和php相应配置请看这里。
April 24th, 2008 by 张磊
在Windows上有个烦恼就是装类似于memcached、lighttpd这些好东西都得一番好找,最后像是做贼一样,终于在互联网的某个角落里找到了。 其实memcached Server我早就找到了,可是配套的PHP的memcached Client一直没找着。windows里不能像在linux那样config/make/make install就搞定,只能满世界地找dll。 坦白说现在做这些都是尝试。niupu的微薄流量对比起强悍的双核服务器,哪怕不用缓存也没啥问题,现在用文件系统缓存自然也可以。想把memcached拿来尝尝鲜,终于被我找到php_memcached.dll 。 把php_memcached.dll放在php的扩展文件夹,然后在C:\windows下打开php.ini,加入一行对php_memcached.dll的引用。重启apache,看看phpinfo()中有没有memcache,如果有,那就安装成功了。 我用Zend Framework中的Zend_Cache,使用默认配置,就可以连接memcached Server了。试试看,速度也没提升多少嘛。