• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ruby连接redis

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

先安装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 100
bind 127.0.0.1
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit pubsub 32mb 8mb 60
client-output-buffer-limit slave 256mb 64mb 60
daemonize yes
databases 16
dbfilename dump.rdb
dir /var/lib/redis
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
logfile /var/log/redis/redis-server.log
loglevel notice
lua-time-limit 5000
no-appendfsync-on-rewrite no
pidfile /var/run/redis/redis-server.pid
port 6379
rdbchecksum yes
rdbcompression yes
save 300 10
save 60 10000
save 900 1
set-max-intset-entries 512
slave-priority 100
slave-read-only yes
slave-serve-stale-data yes
slowlog-log-slower-than 10000
slowlog-max-len 128
stop-writes-on-bgsave-error yes
timeout 0
zset-max-ziplist-entries 128
zset-max-ziplist-value 64

启动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","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,-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
["1", "2", "3", "4", "5"]
5
["1", "2", "3"]
"2"
TEST list END
TEST REDIS STRING
"1234567890"
TEST string END
TEST REDIS SET
3
["111", "222", "333"]
["111", "222", "333", "444", "555"]
["333"]
TEST set END
TEST REDIS ZSET
["111", "222", "333"]
["222", "333"]
[]
TEST zset END
TEST REDIS HASH TABLE
"2"
["key1", "key2", "key3"]
false
nil
["key1", "key3"]
TEST hash END
[]

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

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
RubyonRails-细节中的精彩发布时间:2022-07-14
下一篇:
Ruby 特性发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap