ruby连接redis

寻技术 Redis / Ruby编程 2023年07月11日 132

先安装redis,我的系统是ubuntu 13.04,安装比较easy。

apt-get install redis-server

redis的默认配置文件放在/etc/redis/redis.conf,全部都采用默认参数,对仅仅做测试而言,端口6379是最重要的

activerehashing yes
appendfsync everysec
appendonly no
auto-aof-rewrite-min-size 64mb
auto-aof-rewrite-percentage 
bind 127.0.0.1
client-output-buffer-limit normal   
client-output-buffer-limit pubsub 32mb 8mb 
client-output-buffer-limit slave 256mb 64mb 
daemonize yes
databases 
dbfilename dump.rdb
dir /var/lib/redis
hash-max-ziplist-entries 
hash-max-ziplist-value 
list-max-ziplist-entries 
list-max-ziplist-value 
logfile /var/log/redis/redis-server.log
loglevel notice
lua-time-limit 
no-appendfsync-on-rewrite no
pidfile /var/run/redis/redis-server.pid
port 
rdbchecksum yes
rdbcompression yes
save  
save  
save  
set-max-intset-entries 
slave-priority 
slave-read-only yes
slave-serve-stale-data yes
slowlog-log-slower-than 
slowlog-max-len 
stop-writes-on-bgsave-error yes
timeout 
zset-max-ziplist-entries 
zset-max-ziplist-value 

启动redis,

redis-server /etc/redis/redis.conf

然后是ruby用到的gem

sudo gem install redis

 最后是写ruby代码

# 20140117, redis_test.rb

require 'rubygems'
require 'redis'

redis=Redis.new(:host => "localhost", :port => 6379)
redis2=Redis.new(:host => "localhost", :port => 6379)

puts "TEST REDIS LIST"
redis.lpush:"list1",3
redis.lpush:"list1",2
redis.lpush:"list1",1
redis.rpush:"list1",4
redis2.rpush:"list1",5
p redis.lrange:"list1",0,-1
p redis.llen:"list1"
redis.ltrim:"list1",0,2
p redis.lrange:"list1",0,-1
p redis.lindex:"list1",1
puts "TEST " + (redis.type:"list1") + " END"
redis.del:"list1"

puts "TEST REDIS STRING"
redis.set:"str1",""
p redis.get:"str1"
puts "TEST " + (redis.type:"str1") + " END"
redis.del:"str1"

puts "TEST REDIS SET"
redis.sadd:"set1",""
redis.sadd:"set1",""
redis.sadd:"set1",""
p redis.scard:"set1"
p redis.smembers:"set1"
redis.sadd:"set2",""
redis.sadd:"set2",""
redis.sadd:"set2",""
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,""
redis.zadd:"sort1",1,""
redis.zadd:"sort1",3,""
p redis.zrange:"sort1",0,-1
p redis.zrangebyscore:"sort1",2,3
p redis.zrangebyscore:"sort1",2,-1
puts "TEST " + (redis.type:"sort1") + " END"
redis.del:"sort1"

puts "TEST REDIS HASH TABLE"
redis.hset:"hash1","key1",1
redis.hset:"hash1","key2",2
redis.hset:"hash1","key3",3
p 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
["", "", "", "", ""]

["", "", ""]
""
TEST list END
TEST REDIS STRING
""
TEST string END
TEST REDIS SET

["", "", ""]
["", "", "", "", ""]
[""]
TEST set END
TEST REDIS ZSET
["", "", ""]
["", ""]
[]
TEST zset END
TEST REDIS HASH TABLE
""
["key1", "key2", "key3"]
false
nil
["key1", "key3"]
TEST hash END
[]

 老一些的代码中可能说会需要安装system_timer,ruby 1.9开始,system_timer就已经不需要了。

 

关闭

用微信“扫一扫”