memcache in rails

前提:已架设memcache服务器 参考http://blog.csdn.net/pwlazy/archive/2007/03/27/1542916.aspx

step1)
本地安装rails的memchache客户端
gem install memcache-client

step2)
安装好之后:
在environment.rb中加入:
CACHE = MemCache.new '192.168.100.231:11211'
上面的192.168.100.231就是服务器
MemCache.new的详细使用参看http://dev.robotcoop.com/Libraries/memcache-client/index.html

step3)
写测试:
require File.dirname(__FILE__)  +   ' /../test_helper '

class  CacheTest  <  Test::Unit::TestCase
  
  def test_get
    admin 
=  Admin. new
    admin.id
= 5
    
    #CACHE.delete(
" test " )
    CACHE.
set ( " test " ,admin, - 1
    data 
=  CACHE. get ( " test " )
    assert_equal nil,data
    
    CACHE.
set ( " test " ,admin)    
    data 
=  CACHE. get ( " test " )
    assert_equal 
5 ,data.id
   
  end
  
end

注意:清除某个key,可使用
CACHE.delete("key") 或者CACHE.set("key",value,-1)

真正应用的时候最好写个类或者模块将

memcache-client的API封装一下
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值