Redis

概念:Redis (REmote DIctionary Server) 是用 C 语言开发的一个开源的高性能键值对(key-value)数据库。

运行环境

centos7.0
两个虚拟机,一台克隆机。
各自安装了docker redis
启动了docker redis

  1. [root@localhost ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 362e38a62c99 redis:5.0 "docker-entrypoint.s…" 2 weeks ago Up 5 seconds 0.0.0.0:6379->6379/tcp c_redis

借此启动两台docker redis服务器,使用RedisDesktopManager软件作为客户端。
key-value中间有一个field作为存储名
类似于key:hash值,field:名字,value:存储数据

string 类型

string 类型数据的基本操作

添加/修改数据

  1. set key value

获取数据

  1. get key

删除数据

  1. del key

判定性添加数据

  1. setnx key value

例子:

  1. c_redis:0>set name hikktn
  2. "OK"
  3. c_redis:0>get name
  4. "hikktn"
  5. c_redis:0>del name
  6. "1"
  7. c_redis:0>get name
  8. null
  9. c_redis:0>setnx user root
  10. "1"
  11. c_redis:0>get user
  12. "root"

添加/修改多个数据

  1. mset key1 value1 key2 value2

获取多个数据

  1. mget key1 key2

获取数据字符个数(字符串长度)

  1. strlen key

追加信息到原始信息后部(如果原始信息存在就追加,否则新建)

  1. append key value

例子:

  1. c_redis:0>mset name1 tom name2 shu
  2. "OK"
  3. c_redis:0>mget name1 name2
  4. 1) "tom"
  5. 2) "shu"
  6. c_redis:0>strlen name1 name2
  7. "ERR wrong number of arguments for 'strlen' command"
  8. c_redis:0>strlen name1
  9. "3"
  10. c_redis:0>append name2 su
  11. "5"
  12. c_redis:0>mget name1 name2
  13. 1) "tom"
  14. 2) "shusu"

string 类型数据的扩展操作

设置数值数据增加指定范围的值

  1. incr key
  2. # 自增入力值
  3. incrby key increment
  4. # 自增浮点入力值
  5. incrbyfloat key increment

设置数值数据减少指定范围的值

  1. decr key
  2. # 自减入力值
  3. decrby key increment

设置数据具有指定的生命周期

  1. # 设置几秒后失效
  2. setex key seconds value
  3. #设置几毫秒钟失效
  4. psetex key milliseconds value

例子:
incr key和decr key(自增和自减,+1和-1)

  1. c_redis:0>set ace 5
  2. "OK"
  3. c_redis:0>incr ace
  4. "6"
  5. c_redis:0>get ace
  6. "6"
  7. c_redis:0>decr ace
  8. "5"

incrby key increment和decrby key increment (自增入力值和自减入力值)

  1. c_redis:0>get ace
  2. null
  3. c_redis:0>incrby ace 9
  4. "9"
  5. c_redis:0>incrby ace 10.0
  6. "ERR value is not an integer or out of range"
  7. c_redis:0>incrby ace 100
  8. "109"
  9. c_redis:0>get ace
  10. "109"
  11. c_redis:0>decrby ace 2
  12. "107"

incrbyfloat key increment(自增浮点数)

  1. c_redis:0>incrbyfloat aoc 10.2
  2. "10.2"
  3. c_redis:0>decrbyfloat aoc 0.2
  4. "ERR unknown command `decrbyfloat`, with args beginning with: `aoc`, `0.2`, "
  5. c_redis:0>get aoc
  6. "10.2"

setex key seconds value(在10秒钟内失效)

  1. c_redis:0>setex ace 10 109
  2. "OK"
  3. c_redis:0>get ace
  4. "109"
  5. c_redis:0>get ace
  6. null

psetex key milliseconds value (在5秒钟【毫秒】内失效)

  1. c_redis:0>set ace 100
  2. "OK"
  3. c_redis:0>psetex ace 5000 100
  4. "OK"
  5. c_redis:0>get ace
  6. "100"
  7. c_redis:0>get ace
  8. null

应用场景
关注,粉丝,博客

比如我的博客现在又55篇原创,那么key的命名方式,

  1. 表名 主键名 主键值 字段名<br />eg1 order id 254858751 user<br />eg2 fans id 5421884431 type<br />eg3 blog id 5459982214 txt<br /> 举例:
  2. 博主昵称:hikktn,粉丝数:59 →→→→ hikktn:id:1213457107:fans →→→→ 59
  3. 博主昵称:hikktn,博客数:55 →→→→ hikktn:id:1213457107:blog →→→→ 55
  4. 博主昵称:hikktn,点赞数:62 →→→→ hikktn:id:1213457107:fabulous →→→→ 62

另一种方式(JSON):

  1. hikktn:id:1213457108 →→→→ {"fans" : 59 , "blog" : 55 , "fabulous" : 62 }
  1. c_redis:0>set hikktn:id:1213457107:fans 59
  2. "OK"
  3. c_redis:0>set hikktn:id:1213457107:blog 55
  4. "OK"
  5. c_redis:0>set hikktn:id:1213457107:fabulous 62
  6. "OK"
  7. c_redis:0>set hikktn:id:1213457108 {\"fans\":59,\"blog\":55,\"fabulous\":62}
  8. "OK"
  9. c_redis:0>get hikktn:id:1213457108
  10. "{"fans":59,"blog":55,"fabulous":62}"