Memcached 学习笔记(二)——ruby调用
上一节我们讲述了怎样安装memcached及memcached常用命令。这一节我们将通过ruby来调用memcached相关操作。
第一步,安装ruby.此操作非常简单,直接yum install ruby即可。
第二步,安装rubygems,同样,yum install rubygems.
第三步,安装程序库memcache-client,运行命令:
gem install memcache-client
第四步,运行ruby测试程序:
$KCODE='u' require "rubygems" require "memcache" server=['localhost:11211'] option={} cache=MemCache.new(server,option) cache['key1']=123 cache['key2']="ABCDE" cache['key3']=%w(hoge fuga) cache['key4']={:foo=>1,:bar=>"a"} p cache['key1'] p cache['key2'] p cache['key3'] p cache['key4']
结果如下图:
第五步,通过telnet查看是否已经存在内存中:
第六步,在ruby程序中设置过期时间
[root@localhost ~]# ruby require "rubygems" require "memcache" cache=MemCache.new(['localhost:11211 ']) cache.set('key','value',10) p cache['key'] sleep 11 p cache['key'] "value" nil