redis 的图标挺有意思的

 

redis 的使用

 

1.安装redis官网的步骤安装redis (http://redis.io/download)

 

 

$ wget http://download.redis.io/releases/redis-2.8.15.tar.gz
$ tar xzf redis-2.8.15.tar.gz
$ cd redis-2.8.15
$ make

 

 

 

 

 

2.启动redis 

 

 

$ src/redis-server

 

 

 

or

 

 

后台启动

 

 

nohup src/redis-server &


进程 

 

 

[1] 16461

 

有意思的提示

 

root@5254004e45d0:/srv/rorapps/redis/redis-2.8.15# src/redis-server 
[16445] 16 Sep 09:54:32.684 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf
[16445] 16 Sep 09:54:32.686 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 2.8.15 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in stand alone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 16445
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

[16445] 16 Sep 09:54:32.688 # Server started, Redis version 2.8.15
[16445] 16 Sep 09:54:32.688 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
[16445] 16 Sep 09:54:32.688 * The server is now ready to accept connections on port 6379

 

 

 

 

 

Ok, 以上redis的服务端完成了

 

 

3.客户端使用

这里介绍Rails的使用

Gem

redis-rb (https://github.com/redis/redis-rb)

 

 

require "redis"

redis = Redis.new

redis.set("mykey", "hello world")
# => "OK"

redis.get("mykey")
# => "hello world"

 

 

 

 

 

 

Ok,客户端简单版
 

 

4.错误解决

提示

 

Redis::ProtocolError ( Got 'H' as initial reply byte. If you're in a forking environment, such as Unicorn, you need to connect to Redis after forking. ):


是指你的Redis 服务没有启动

 

 

 

 

5. 做ActiveRecord对象缓存

http://robbinfan.com/blog/33/activerecord-object-cache

 

6.  dalli/redis

 

  require 'active_support'
  require 'dalli'
  require 'active_support/cache/dalli_store'
  Dalli.logger = $common_logger
  dalli_opts = {
    :namespace => "namespace_name",
    :compress => true,
    :expires_in => 60.minute
  }
  CACHE = ActiveSupport::Cache::DalliStore.new(APP_CONFIG['redis_url'], dalli_opts)
  DALLI = Dalli::Client.new(APP_CONFIG['redis_url'], dalli_opts)
  RedisCache = Redis.new(url: APP_CONFIG['redis_url'], :driver => :hiredis)
  CACHE = ActiveSupport::Cache::RedisStore.new([{:host => APP_CONFIG['redis_url'], :port => 6379, :driver => :hiredis}])

记录平时使用的


setnx 将key设置值为value,如果key不存在,这种情况下等同SET命令。 当key存在时,什么也不做
incr 对存储在指定key的数值执行原子的加1操作
exists 返回key是否存在
mget 获取所有的key值 $redis.mget('key')

smembers 获取集合中的所有元素#$redis.smembers('key')
srandmember 从集合里面随机获取一个元素 $redis.srandmember('test_user_ids')
zadd 添加到有序set的一个或多个成员或更新 $redis.zadd("sort_key", -Time.now.to_i, 'value')
zrem 从排序的集合中删除一个或多个元素 $redis.zrem('sort_key', [test,..])
sadd 添加一个或多个指定的member元素到集合的key $redis.sadd('key', [test,...])

srem 从集合中删除一个或多个元素 $redis.zrem('sort_key', [test,..])
scard 获取集合里面的元素数量 $redis.scard('key')

zrange $redis.zrange('key', i, j)

zcard  $redis.zcard('key', filed) -> index

 

hgetall 从Hash中读取全部的域和值 $redis.hgetall('key')
hget 返回key 指定的哈希集中该字段所关联的值 $redis.hget('key', 'field1')
hdel 删除一个或多个Hash中的field $redis.hdel('key', [field1,field2,..])
hset 设置Hash里面的一个字段值 $redis.hset('key', 'field', 'value')
hmset 设置Hash字段值 $redis.hmset('key', {field1: test1, field2: test2,...})

multi 标记一个事务块
pipelined 减少了RTT,也减少了IO调用次数、需要控制Pipeline的大小,否则会消耗Redis的内存、1024

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Men-DD

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值