先安装redis,我的系统是ubuntu 13.04,安装比较easy。
apt-get install redis-server
redis的默认配置文件放在/etc/redis/redis.conf,全部都采用默认参数,对仅仅做测试而言,端口6379是最重要的
activerehashing yesappendfsync everysecappendonly noauto-aof-rewrite-min-size 64mbauto-aof-rewrite-percentage 100bind 127.0.0.1client-output-buffer-limit normal 0 0 0client-output-buffer-limit pubsub 32mb 8mb 60client-output-buffer-limit slave 256mb 64mb 60daemonize yesdatabases 16dbfilename dump.rdbdir /var/lib/redishash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-entries 512list-max-ziplist-value 64logfile /var/log/redis/redis-server.logloglevel noticelua-time-limit 5000no-appendfsync-on-rewrite nopidfile /var/run/redis/redis-server.pidport 6379rdbchecksum yesrdbcompression yessave 300 10save 60 10000save 900 1set-max-intset-entries 512slave-priority 100slave-read-only yesslave-serve-stale-data yesslowlog-log-slower-than 10000slowlog-max-len 128stop-writes-on-bgsave-error yestimeout 0zset-max-ziplist-entries 128zset-max-ziplist-value 64
启动redis,
redis-server /etc/redis/redis.conf
然后是ruby用到的gem
sudo gem install redis
最后是写ruby代码
# 20140117, redis_test.rbrequire 'rubygems'require 'redis'redis=Redis.new(:host => "localhost", :port => 6379)redis2=Redis.new(:host => "localhost", :port => 6379)puts "TEST REDIS LIST"redis.lpush:"list1",3redis.lpush:"list1",2redis.lpush:"list1",1redis.rpush:"list1",4redis2.rpush:"list1",5p redis.lrange:"list1",0,-1p redis.llen:"list1"redis.ltrim:"list1",0,2p redis.lrange:"list1",0,-1p redis.lindex:"list1",1puts "TEST " + (redis.type:"list1") + " END"redis.del:"list1"puts "TEST REDIS STRING"redis.set:"str1","1234567890"p redis.get:"str1"puts "TEST " + (redis.type:"str1") + " END"redis.del:"str1"puts "TEST REDIS SET"redis.sadd:"set1","222"redis.sadd:"set1","111"redis.sadd:"set1","333"p redis.scard:"set1"p redis.smembers:"set1"redis.sadd:"set2","444"redis.sadd:"set2","333"redis.sadd:"set2","555"p redis.sunion:"set1","set2"p redis.sinter:"set1","set2"puts "TEST " + (redis.type:"set1") + " END"redis.del:"set1"redis.del:"set2"puts "TEST REDIS ZSET"redis.zadd:"sort1",2,"222"redis.zadd:"sort1",1,"111"redis.zadd:"sort1",3,"333"p redis.zrange:"sort1",0,-1p redis.zrangebyscore:"sort1",2,3p redis.zrangebyscore:"sort1",2,-1puts "TEST " + (redis.type:"sort1") + " END"redis.del:"sort1"puts "TEST REDIS HASH TABLE"redis.hset:"hash1","key1",1redis.hset:"hash1","key2",2redis.hset:"hash1","key3",3p redis.hget:"hash1","key2"p redis.hkeys:"hash1"p redis.hexists:"hash1","key4"redis.hdel:"hash1","key2"p redis.hget:"hash1","key2"p redis.hkeys:"hash1"puts "TEST " + (redis.type:"hash1") + " END"redis.del:"hash1"p redis.keys
这里测试了redis常用的一些数据结构,输出如下
TEST REDIS LIST["1", "2", "3", "4", "5"]5["1", "2", "3"]"2"TEST list ENDTEST REDIS STRING"1234567890"TEST string ENDTEST REDIS SET3["111", "222", "333"]["111", "222", "333", "444", "555"]["333"]TEST set ENDTEST REDIS ZSET["111", "222", "333"]["222", "333"][]TEST zset ENDTEST REDIS HASH TABLE"2"["key1", "key2", "key3"]falsenil["key1", "key3"]TEST hash END[]
老一些的代码中可能说会需要安装system_timer,ruby 1.9开始,system_timer就已经不需要了。