博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ruby连接redis
阅读量:6081 次
发布时间:2019-06-20

本文共 3119 字,大约阅读时间需要 10 分钟。

先安装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就已经不需要了。

 

转载于:https://www.cnblogs.com/valleylord/p/3524568.html

你可能感兴趣的文章
linux下查看各硬件型号
查看>>
epoll的lt和et模式的实验
查看>>
Flux OOM实例
查看>>
07-k8s-dns
查看>>
Android 中 ListView 分页加载数据
查看>>
oracle启动报错:ORA-00845: MEMORY_TARGET not supported on this system
查看>>
Go方法
查看>>
Dapper丶DapperExtention,以及AbpDapper之间的关系,
查看>>
搞IT的同学们,你们在哪个等级__那些年发过的帖子
查看>>
且谈语音搜索
查看>>
MySQL数据库导入导出常用命令
查看>>
低版本Samba无法挂载
查看>>
Telegraf+Influxdb+Grafana构建监控平台
查看>>
使用excel 展现数据库内容
查看>>
C#方法拓展
查看>>
MySql.Data.dll的版本
查看>>
Linux系统磁盘管理
查看>>
hdu 2191 (多重背包+二进制优化)
查看>>
home.php
查看>>
neo4j---删除关系和节点
查看>>