redis使用入门

redis是nosql的一种,为非关系型数据库,在大型网站会使用它来减轻数据库的压力,即在访问数据时会先从redis中查找是否有,如果有就直接从redis中获取。当然redis不仅仅可以做缓存,下面入门学习一下。

什么是redis

Redis(全称:Remote Dictionary Server 远程字典服务)是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。redis是用C编写,代码总量10万行左右,是开放源代码的一款优秀软件,它的特点总结下就是nosql、key-value、基于内存、可持久化,官网:https://redis.io/ 。

redis使用入门

(1)nosql:即not only structured query language,不仅支持结构化查询语句,它存储的数据结构不再是表格类型数据,如mysql中的一张表,保存的是结构化的表格类型数据,需要区别开来。

(2)key-value:存储的数据结构,key为用户自己定义的具有含义的字符,value为非结构化的数据,可以是网页数据、json数据或其他具有意义的数据,redis提供了5种数据类型供使用。

(3)基于内存:redis是在内存中处理数据,支持上限为万条/秒的处理能力,处理速度极快。

(4)可持久化:内存中保存的数据在断电后容易丢失,redis支持内存保存数据的持久化,断电恢复后可以读取保存在硬盘的数据,恢复redis数据。

以上是redis的特点,它的作用主要是减轻后端数据库的压力,如下图,以读为例,业务层会先从redis读取想要的数据,如果有就直接返回,没有才从DB读取,读取后返回结果并在redis保存一份读取到的数据,下次读取可以直接从redis获取。

redis使用入门

redis和memoryCache 

在redis之前,曾经大量使用memoryCache,它也是基于内存,是类似redis的一种缓存数据库,它和redis相比主要的不足就是不支持持久化,以及能处理的数据类型单一,这是后面限制其使用的缺点。

对比项目redismemoryCahe
读写效率内存处理数据,为单进程单线程,采用非阻塞线程操作,使用了IO多路复用。  内存处理,为单进程多线程,性能高于redis
是否持久化 支持内存数据持久化 不支持,容灾能力低于redis
保存数据类型 value支持5种数据类型 只支持string类型,应用领域能处理的业务范围没redis大

redis雪崩

什么是redis雪崩,顾名思义就是系统崩了,而且有愈演愈烈的趋势。一个使用了redis稳定运行的系统,如果突然因为不可抗力发生redis的宕机或不可用,并且访问系统的并发量很高的前提下,大量的并发访问将转而涌入数据库,造成数据库压力骤增,导致数据库承受不了压力而宕机。在redis恢复之前,重启系统后由于海量的访问还依然存在,导致系统继续宕机,接下来就陷入了宕机-重启-宕机的循环中无法恢复造成系统不可用的现象,这就是雪崩。

redis安装

以上是redis的基本概念,接下来安装一下redis,本次操作在centos6.5中完成。

(1)解压

[ /home/software]# tar -zxvf redis-3.2.11.tar.gz

(2)解压完后会在/home/software下生成一个redis-3.2.11的文件夹,进入后可以编译,但是编译会报错‘‘gcc和cc的命令未找到‘‘,需要先执行以下命令安装gcc。

yum -y install gcc gcc-c++ libstdc++-devel

(3)在编译前还需要执行以下命令,否则编译会报错‘‘emalloc/jemalloc.h: No such file or directory‘‘,这个命令在redis根目录的README.md文件中有说明。

make MALLOC=libc

(4)使用如下命令在redis根目录编译,编译成功后会执行安装

# 编译[ /home/software/redis-3.2.11]# make && make install
cd src && make all
make[1]: Entering directory `/home/software/redis-3.2.11/src‘

Hint: It‘s a good idea to run ‘make test‘ ;)

make[1]: Leaving directory `/home/software/redis-3.2.11/src‘
cd src && make install
make[1]: Entering directory `/home/software/redis-3.2.11/src‘

Hint: It‘s a good idea to run ‘make test‘ ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: Leaving directory `/home/software/redis-3.2.11/src‘

(5)所有的命令会在src目录下,使用如下命令默认参数下启动redis,就会看到一个redis的图标,并且提示可以通过6379端口来连接redis,说明就安装成功。

[ /home/software/redis-3.2.11/src]# redis-server
4885:C 02 Dec 22:32:02.180 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
4885:M 02 Dec 22:32:02.181 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._
           _.-``__ ‘‘-._
      _.-``    `.  `_.  ‘‘-._           Redis 3.2.11 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ‘‘-._
 (    ‘      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|‘` _.-‘|     Port: 6379
 |    `-._   `._    /     _.-‘    |     PID: 4885
  `-._    `-._  `-./  _.-‘    _.-‘
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|
 |    `-._`-._        _.-‘_.-‘    |           http://redis.io
  `-._    `-._`-.__.-‘_.-‘    _.-‘
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|
 |    `-._`-._        _.-‘_.-‘    |
  `-._    `-._`-.__.-‘_.-‘    _.-‘
      `-._    `-.__.-‘    _.-‘
          `-._        _.-‘
              `-.__.-‘

4885:M 02 Dec 22:32:02.194 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
4885:M 02 Dec 22:32:02.194 # Server started, Redis version 3.2.11
4885:M 02 Dec 22:32:02.195 # 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.
4885:M 02 Dec 22:32:02.198 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command ‘echo never > /sys/kernel/mm/transparent_hugepage/enabled‘ as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
4885:M 02 Dec 22:32:02.198 * The server is now ready to accept connections on port 6379

(6)使用如下命令来实现客户端登录,需先开一个窗口,默认登录的服务器是127.0.0.1:6379

[ ~]# redis-cli
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379>

(7)关闭redis,退出客户端

# 关闭服务127.0.0.1:6379> shutdown# 退出客户端
not connected> quit

redis基本命令&数据类型操作

redis支持String、Hash、List、Set和ZSet五种数据类型,下面基于这五种数据类型来进行操作,后续使用jedis编程,方法名也和命令名类似。具体的操作可以参考redis官方提示:https://redis.io/commands,也可以使用命令help来获取帮助信息。

通用操作

如下操作是通用操作命令,不仅仅局限于某种数据类型,为了使用通用操作命令,先设置String类型数据在redis进行测试。

# 查看当前redis服务进程的所有key值,不能查看分布式的其他节点127.0.0.1:6379> keys *
(empty list or set)# 先设置一个key
127.0.0.1:6379> set name clyang
OK
127.0.0.1:6379> get name
"clyang"# ttl查看key的超时数据的剩余秒数
127.0.0.1:6379> ttl name
(integer) -1# expire设置key-value对的超时时间
127.0.0.1:6379> expire name 10
(integer) 1# ttl查看发现超时时间还有8秒
127.0.0.1:6379> ttl name
(integer) 8
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set name clyang
OK# pttl查看key-value超时毫秒数
127.0.0.1:6379> pttl name
(integer) -1# 设置key-value超时毫秒数
127.0.0.1:6379> pexpire name 10000
(integer) 1# pttl查看还剩7000多毫秒
127.0.0.1:6379> pttl name
(integer) 7034
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set name clyang
OK# type查看key对应的value类型
127.0.0.1:6379> type name
string# 删除当前key对应的key-value
127.0.0.1:6379> del name
(integer) 1
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set name clyang
OK# 持久化的命令,默认保存为dump.rdb到本地,redis重启后可以从里面恢复
127.0.0.1:6379> save
OK# 将内存数据清空,3.0版本后,默认为0号库,3.0版本前将内存划分为16个数据库区域,编号0-15,可以使用select命令来选择数据库区域
127.0.0.1:6379> flushdb
OK# 将内存数据和持久化数据一起删除
127.0.0.1:6379> flushall
OK# help+redis命令可以查看命命令的解释提示
127.0.0.1:6379> help set

  SET key value [EX seconds] [PX milliseconds] [NX|XX]
  summary: Set the string value of a key
  since: 1.0.0
  group: string

127.0.0.1:6379>

String类型

string类型,就是value是string的,可以保存一个对象的json字符串,也可以是其他具有意义的数据,key一般加前缀,具有操作意义,如query_name,代表查询的名字,delete_number,代表删除的数字等。

(1)set来添加key-value,可以配合非必需项目,get来获取对应的value。

# EX 设置超时秒数# PX 设置超时毫秒数# NX 当前没有key的数据时才能set成功,只能第一次创建key才成功# XX 当前有key的数据时才能set成功,只能修改已经有的key-valueset key value [EX seconds] [PX milliseconds] [NX|XX]

实操记录。

# 设置100秒超时,第一次创建可以使用NX127.0.0.1:6379> set name clyang EX 100 NX
OK
127.0.0.1:6379> ttl name
(integer) 94# get查看key的值
127.0.0.1:6379> get name
"clyang"# name已经存在,使用NX将设置失效
127.0.0.1:6379> set name clyang NX
(nil)
127.0.0.1:6379> get name
"clyang"
127.0.0.1:6379> ttl name
(integer) 61# XX可以用在已有的key上
127.0.0.1:6379> set name messi PX 100000 XX
OK# key的值修改成功
127.0.0.1:6379> get name
"messi"
127.0.0.1:6379> pttl name
(integer) 90959# append可以在原有value的基础后面添加字符串
127.0.0.1:6379> append name herry
(integer) 10# 变成了messi+herry合体
127.0.0.1:6379> get name
"messiherry"

(2)批量set和get,可以节省网络带宽,提高效率。

127.0.0.1:6379> mset key value [key value ...]127.0.0.1:6379> mget key [key ...]

实操记录

127.0.0.1:6379> mset name clyang age 28 gender male salary 25000 height 180 weight 120
OK
127.0.0.1:6379> mget name age gender salary height weight
1) "clyang"
2) "28"
3) "male"
4) "25000"
5) "180"
6) "120"
127.0.0.1:6379>

(3)exists查看是否存在某个key,目前最新版本的redis支持最大能存储的value大小为1GB,如果使用get命令效率会低,使用exists命令显然效率更高,如果只是用来判断这个key是否存在优先用它。

127.0.0.1:6379> exists name
(integer) 1

(4)incr/decr和incrby/decrby命令,可以对value为纯数字的值进行增加或减少。

# 具体查看解释127.0.0.1:6379> help incr

  INCR key
  summary: Increment the integer value of a key by one
  since: 1.0.0
  group: string

127.0.0.1:6379> help decr

  DECR key
  summary: Decrement the integer value of a key by one
  since: 1.0.0
  group: string

127.0.0.1:6379> help incrby

  INCRBY key increment
  summary: Increment the integer value of a key by the given amount
  since: 1.0.0
  group: string

127.0.0.1:6379> help decrby

  DECRBY key decrement
  summary: Decrement the integer value of a key by the given number
  since: 1.0.0
  group: string

实操记录

127.0.0.1:6379> get age
"28"
127.0.0.1:6379> incr age
(integer) 29
127.0.0.1:6379> decr age
(integer) 28
127.0.0.1:6379> incrby age 10
(integer) 38
127.0.0.1:6379> decrby age 5
(integer) 33

hash类型

hash类型为双层map,一般用在表示面向对象的数据结构,如一个person对象,第一层key可以为p,第一层value就是person对象,其中第二层map的value_key为属性名,value_value为属性值。

(1)hset/hget设置获取值,hexists查看是否存在对应的属性。

# key为具有意义的值,field为具体对象的属性名,value为具体对象的属性值127.0.0.1:6379> hset key field value# 获取属性值,field为对象数据属性名
127.0.0.1:6379> hget key field# 查看是否具有属性
127.0.0.1:6379> hexists key field

实操记录

127.0.0.1:6379> hset u username clyang
(integer) 1
127.0.0.1:6379> hset u password 123
(integer) 1
127.0.0.1:6379> hget u username
"clyang"
127.0.0.1:6379> hget u password
"123"
127.0.0.1:6379> hexists u username
(integer) 1
127.0.0.1:6379> hexists u nickname
(integer) 0

(2)hmset/hmget批量设置和获取值。

127.0.0.1:6379> hmset key field value [field value ...]
127.0.0.1:6379> hmget key field [field ...]

实操记录

127.0.0.1:6379> hmset p name messi age 18 score 50
OK
127.0.0.1:6379> hmget p name age score
1) "messi"
2) "18"
3) "50"

(3)hkeys/hvals获取属性名的集合和属性值的集合。

127.0.0.1:6379> help hkeys

  HKEYS key
  summary: Get all the fields in a hash
  since: 2.0.0
  group: hash

127.0.0.1:6379> help hvals

  HVALS key
  summary: Get all the values in a hash
  since: 2.0.0
  group: hash

实操记录

127.0.0.1:6379> hkeys p
1) "name"
2) "age"
3) "score"
127.0.0.1:6379> hvals p
1) "messi"
2) "18"
3) "50"

(4)hlen,获取hash类型数据属性个数

127.0.0.1:6379> help hlen

  HLEN key
  summary: Get the number of fields in a hash
  since: 2.0.0
  group: hash

实操记录

# 上面可以看到p对应三个属性name,age和score,因此为3127.0.0.1:6379> hlen p
(integer) 3

(5)hdel,删除hash数据中的属性。

127.0.0.1:6379> help hdel

  HDEL key field [field ...]
  summary: Delete one or more hash fields
  since: 2.0.0
  group: hash

实操记录

# 查询有3个属性127.0.0.1:6379> hkeys p
1) "name"
2) "age"
3) "score"# 删除score属性
127.0.0.1:6379> hdel p score
(integer) 1# 还剩下name和age属性
127.0.0.1:6379> hkeys p
1) "name"
2) "age"

(6)hincrby,可以为纯数字的属性值指定自增或自减。这种情况如果是使用string类型的数据,想增加json字符串中age的值,需要先拿到string类型数据解析成对象,然后对对象属性进行调整,再重新set为新的string类型的value,相比hash类型数据来说会稍微麻烦,hash类型数据可以直接定位到要修改的数据。

127.0.0.1:6379> help hincrby

  HINCRBY key field increment
  summary: Increment the integer value of a hash field by the given number
  since: 2.0.0
  group: hash

实操记录

# 查看纯数字类型的属性127.0.0.1:6379> hvals p
1) "messi"
2) "18"# age增加10
127.0.0.1:6379> hincrby p age 10
(integer) 28# age减少5
127.0.0.1:6379> hincrby p age -5
(integer) 23

list类型

list类型为双向链表,头尾(左右/上下)操作快,中间操作慢 ,可以用来做秒杀队列,只是比较低端一点。

(1)lpush/rpush,从头或者尾插入元素,lrange查看元素。

127.0.0.1:6379> lpush key value [value ...]
127.0.0.1:6379> rpush key value [value ...]127.0.0.1:6379> lrange key start stop

实操记录

127.0.0.1:6379> lpush list1 100 200 300 400
(integer) 4
127.0.0.1:6379> rpush list2 100 200 300 400
(integer) 4# 查看list全部内容
127.0.0.1:6379> lrange list1 0 -1
1) "400"
2) "300"
3) "200"
4) "100"
127.0.0.1:6379> lrange list2 0 -1
1) "100"
2) "200"
3) "300"
4) "400"

(2)linsert,在list某个元素前或后插入元素,pivot为参考元素,value为插入的元素。

127.0.0.1:6379> help linsert

  LINSERT key BEFORE|AFTER pivot value
  summary: Insert an element before or after another element in a list
  since: 2.2.0
  group: list

实操记录

127.0.0.1:6379> lrange list1 0 -1
1) "400"
2) "300"
3) "200"
4) "100"# 前后顺序不是参考push的顺序,是实际的从左到右的顺序,左就是before,右就是after
127.0.0.1:6379> linsert list1 BEFORE 200 before
(integer) 5
127.0.0.1:6379> linsert list1 AFTER 200 after
(integer) 6
127.0.0.1:6379> lrange list1 0 -1
1) "400"
2) "300"
3) "before"
4) "200"
5) "after"
6) "100"

(3)lrem删除,count可以为正整数、负整数和0,正代表从头(左/上)删除指定个数的values,负从尾(右/下)删除指定个数的values,0代表全删指定的value。

127.0.0.1:6379> help lrem

  LREM key count value
  summary: Remove elements from a list
  since: 1.0.0
  group: list

实操记录

127.0.0.1:6379> lpush list3 100 200 200 100 200 100 100 200
(integer) 8
127.0.0.1:6379> lrange list3 0 -1
1) "200"
2) "100"
3) "100"
4) "200"
5) "100"
6) "200"
7) "200"
8) "100"# 从头删除2个100
127.0.0.1:6379> lrem list3 2 100
(integer) 2
127.0.0.1:6379> lrange list3 0 -1
1) "200"
2) "200"
3) "100"
4) "200"
5) "200"
6) "100"# 从尾删除2个200
127.0.0.1:6379> lrem list3 -2 200
(integer) 2
127.0.0.1:6379> lrange list3 0 -1
1) "200"
2) "200"
3) "100"
4) "100"# 删除所有的200
127.0.0.1:6379> lrem list3 0 200
(integer) 2
127.0.0.1:6379> lrange list3 0 -1
1) "100"
2) "100"

(4)ltrim,保留list中一定范围内的元素值,其他都删除,类似string的trim截取。

127.0.0.1:6379> help ltrim

  LTRIM key start stop
  summary: Trim a list to the specified range
  since: 1.0.0
  group: list

实操记录

127.0.0.1:6379> lpush list4 1 2 3 4 5 6 7
(integer) 7
127.0.0.1:6379> lrange list4 0 -1
1) "7"
2) "6"
3) "5"
4) "4"
5) "3"
6) "2"
7) "1"# 截取只留下3位
127.0.0.1:6379> ltrim list4 0 2
OK
127.0.0.1:6379> lrange list4 0 -1
1) "7"
2) "6"
3) "5"

(5)lpop/rpop,从头删除一个元素或从尾删除一个元素,并且将删除的元素返回,这个较为常用。

127.0.0.1:6379> help lpop

  LPOP key
  summary: Remove and get the first element in a list
  since: 1.0.0
  group: list

127.0.0.1:6379> help rpop

  RPOP key
  summary: Remove and get the last element in a list
  since: 1.0.0
  group: list

实操记录

127.0.0.1:6379> lrange list4 0 -1
1) "7"
2) "6"
3) "5"
127.0.0.1:6379> lpop list4
"7"
127.0.0.1:6379> rpop list4
"5"

(6)llen,查看元素个数

127.0.0.1:6379> help llen

  LLEN key
  summary: Get the length of a list
  since: 1.0.0
  group: list

实操记录

127.0.0.1:6379> lrange list2 0 -1
1) "100"
2) "200"
3) "300"
4) "400"
127.0.0.1:6379> llen list2
(integer) 4

(7)lset,按照list下标修改元素的值。

127.0.0.1:6379> help lset

  LSET key index value
  summary: Set the value of an element in a list by its index
  since: 1.0.0
  group: list

实操记录

127.0.0.1:6379> lrange list2 0 -1
1) "100"
2) "200"
3) "300"
4) "400"
127.0.0.1:6379> lset list2 0 one
OK
127.0.0.1:6379> lrange list2 0 -1
1) "one"
2) "200"
3) "300"
4) "400"

(8)rpoplpush,移除列表source的最后一个元素,并将该元素添加到另一个列表destination并返回这个值。

127.0.0.1:6379> help rpoplpush

  RPOPLPUSH source destination
  summary: Remove the last element in a list, prepend it to another list and return it
  since: 1.2.0
  group: list

实操记录

127.0.0.1:6379> lpush list5 100 200 300
(integer) 3
127.0.0.1:6379> lpush list6 one two three
(integer) 3
127.0.0.1:6379> lrange list5 0 -1
1) "300"
2) "200"
3) "100"
127.0.0.1:6379> lrange list6 0 -1
1) "three"
2) "two"
3) "one"# 将list5进行rpop获取100,然后lpush到list6上,并且返回这个值
127.0.0.1:6379> rpoplpush list5 list6
"100"
127.0.0.1:6379> lrange list6 0 -1
1) "100"
2) "three"
3) "two"
4) "one"
127.0.0.1:6379> lrange list5 0 -1
1) "300"
2) "200"

set类型

不容许元素重复,没有排序的集合。

(1)add,向集合添加元素,smembers查看集合内容。

127.0.0.1:6379> help sadd

  SADD key member [member ...]
  summary: Add one or more members to a set
  since: 1.0.0
  group: set

实操记录

127.0.0.1:6379> sadd likes football basketball waterball
(integer) 3
127.0.0.1:6379> smembers likes
1) "football"
2) "waterball"
3) "basketball"

(2)scard,查看元素个数。

127.0.0.1:6379> help scard

  SCARD key
  summary: Get the number of members in a set
  since: 1.0.0
  group: set

实操记录

127.0.0.1:6379> scard likes
(integer) 3

(3)srem,删除set集合某个元素。

127.0.0.1:6379> help srem

  SREM key member [member ...]
  summary: Remove one or more members from a set
  since: 1.0.0
  group: set

实操记录

127.0.0.1:6379> srem likes waterball basketball
(integer) 2
127.0.0.1:6379> smembers likes
1) "football"

(4)sismember,判断元素是否在集合中。

127.0.0.1:6379> help sismember

  SISMEMBER key member
  summary: Determine if a given value is a member of a set
  since: 1.0.0
  group: set

实操记录

127.0.0.1:6379> sismember likes football
(integer) 1
127.0.0.1:6379> sismember likes waterball
(integer) 0

(5)srandmember,随机抽取set中的元素,count代表个数。

127.0.0.1:6379> help srandmember

  SRANDMEMBER key [count]
  summary: Get one or multiple random members from a set
  since: 1.0.0
  group: set

实操记录

127.0.0.1:6379> sadd language chinese japanese english korea
(integer) 4
127.0.0.1:6379> srandmember language 2
1) "chinese"
2) "english"

(6)交集、并集和差集。

127.0.0.1:6379> help sinter

  SINTER key [key ...]
  summary: Intersect multiple sets
  since: 1.0.0
  group: set

127.0.0.1:6379> help sunion

  SUNION key [key ...]
  summary: Add multiple sets
  since: 1.0.0
  group: set

127.0.0.1:6379> help sdiff

  SDIFF key [key ...]
  summary: Subtract multiple sets
  since: 1.0.0
  group: set

实操记录

127.0.0.1:6379> smembers language
1) "japanese"
2) "korea"
3) "english"
4) "chinese"
127.0.0.1:6379> sadd language2 japanese korea spanish
(integer) 3
127.0.0.1:6379> smembers language2
1) "japanese"
2) "spanish"
3) "korea"# 交集
127.0.0.1:6379> sinter language language2
1) "japanese"
2) "korea"# 并集
127.0.0.1:6379> sunion language language2
1) "english"
2) "chinese"
3) "spanish"
4) "japanese"
5) "korea"# 差集
127.0.0.1:6379> sdiff language language2
1) "chinese"
2) "english"
127.0.0.1:6379> sdiff language2 language
1) "spanish"

zset类型

在set的基础上,增加排序的功能,需要有一个用于排序的分数,使用分数对集合中的元素进行排名,可以用在各种排行榜。

(1)zadd,添加成员,zrange可以查看成员,类似lrange。

127.0.0.1:6379> zadd key [NX|XX] [CH] [INCR] score member [score member ...]

  ZADD key [NX|XX] [CH] [INCR] score member [score member ...]
  summary: Add one or more members to a sorted set, or update its score if it already exists
  since: 1.2.0
  group: sorted_set

实操记录

127.0.0.1:6379> zadd movie 100 hero
(integer) 1
127.0.0.1:6379> zadd movie 80 shanghaibaolei
(integer) 1
127.0.0.1:6379> zadd movie 20 king
(integer) 1
127.0.0.1:6379> zrange movie 0 2
1) "king"
2) "shanghaibaolei"
3) "hero"

(2)zcard,计算成员个数,类似scard。

127.0.0.1:6379> help zcard

  ZCARD key
  summary: Get the number of members in a sorted set
  since: 1.2.0
  group: sorted_set

实操记录

127.0.0.1:6379> zcard movie
(integer) 3

(3)zscore,获取某个成员的分数,这里就是获取某个电影的分数。

127.0.0.1:6379> help zscore

  ZSCORE key member
  summary: Get the score associated with the given member in a sorted set
  since: 1.2.0
  group: sorted_set

实操记录

127.0.0.1:6379> zscore movie shanghaibaolei
"80"

(4)zrank,计算成员的排名,分数越高,返回的数值越大。

127.0.0.1:6379> help zrank

  ZRANK key member
  summary: Determine the index of a member in a sorted set
  since: 2.0.0
  group: sorted_set

实操记录

127.0.0.1:6379> zrank movie hero
(integer) 2
127.0.0.1:6379> zrank movie king
(integer) 0

(5)zincrby,增加成员的分数。

127.0.0.1:6379> help zincrby

  ZINCRBY key increment member
  summary: Increment the score of a member in a sorted set
  since: 1.2.0
  group: sorted_set

实操记录

# king增加100分,变成120分127.0.0.1:6379> zincrby movie 100 king
"120"
127.0.0.1:6379> zscore movie king
"120"

(6)zrange,返回指定排名范围的成员,升序。

127.0.0.1:6379> help zrange

  ZRANGE key start stop [WITHSCORES]
  summary: Return a range of members in a sorted set, by index
  since: 1.2.0
  group: sorted_set

实操记录

# 分数从小到大,返回第2和第3名
  127.0.0.1:6379> zrange movie 0 2  1) "shanghaibaolei"  2) "hero"  3) "king"
127.0.0.1:6379> zrange movie 1 2
1) "hero"
2) "king"

(7)zrangebyscore,返回指定分数范围的成员。

127.0.0.1:6379> help zrangebyscore

  ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
  summary: Return a range of members in a sorted set, by score
  since: 1.0.5
  group: sorted_set

实操记录

# 上海堡垒只有80分,因此没有展示出来127.0.0.1:6379> zrangebyscore movie 90 130
1) "hero"  # 100
2) "king"  # 120
127.0.0.1:6379>

(8)zcount,返回指定范围的成员个数。

127.0.0.1:6379> help zcount

  ZCOUNT key min max
  summary: Count the members in a sorted set with scores within the given values
  since: 2.0.0
  group: sorted_set

实操记录

# 参考上个例子127.0.0.1:6379> zcount movie 90 130
(integer) 2

(9)zrem,删除成员,zremrangebyscore,删除指定范围内的成员,zremrangebyrank,删除指定排名内的成员,只演示第一个。

127.0.0.1:6379> help zrem

  ZREM key member [member ...]
  summary: Remove one or more members from a sorted set
  since: 1.2.0
  group: sorted_set

127.0.0.1:6379> help zremrangebyscore

  ZREMRANGEBYSCORE key min max
  summary: Remove all members in a sorted set within the given scores
  since: 1.2.0
  group: sorted_set

127.0.0.1:6379> help zremrangebyrank

  ZREMRANGEBYRANK key start stop
  summary: Remove all members in a sorted set within the given indexes
  since: 2.0.0
  group: sorted_set

实操记录

# 删除上海堡垒127.0.0.1:6379> zrem movie shanghaibaolei
(integer) 1
127.0.0.1:6379> zrange movie 0 3
1) "hero"
2) "king"

(10)zinterstore/zunionstore,交集并集的操作。

127.0.0.1:6379> help zinterstore

  ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]
  summary: Intersect multiple sorted sets and store the resulting sorted set in a new key
  since: 2.0.0
  group: sorted_set

127.0.0.1:6379> help zunionstore

  ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]
  summary: Add multiple sorted sets and store the resulting sorted set in a new key
  since: 2.0.0
  group: sorted_set

交集实操记录

127.0.0.1:6379> zadd test01 10 messi 20 herry
(integer) 2
127.0.0.1:6379> zadd test02 50 messi 30 herry
(integer) 2# 有序集合的数据+分数展示
127.0.0.1:6379> zrange test01 0 -1 withscores
1) "messi"
2) "10"
3) "herry"
4) "20"
127.0.0.1:6379> zrange test02 0 -1 withscores
1) "herry"
2) "30"
3) "messi"
4) "50"# 分数相加,输出到key为out的集合中,需要2个交集
127.0.0.1:6379> zinterstore out 2 test01 test02
(integer) 2
127.0.0.1:6379> zrange out 0 -1 withscores
1) "herry"
2) "50"
3) "messi"
4) "60"

并集实操记录

127.0.0.1:6379> zadd result01 10 math 20 chinese
(integer) 2
127.0.0.1:6379> zadd result02 30 physical 40 chemical
(integer) 2
127.0.0.1:6379> zrange result01 0 -1 withscores
1) "math"
2) "10"
3) "chinese"
4) "20"
127.0.0.1:6379> zrange result02 0 -1 withscores
1) "physical"
2) "30"
3) "chemical"
4) "40"# 测试发现result01和result02的数据不一样才能union,否则提示语法错误?
127.0.0.1:6379> zunionstore scores 2 result01 result02
(integer) 4
127.0.0.1:6379> zrange scores 0 -1 withscores
1) "math"
2) "10"
3) "chinese"
4) "20"
5) "physical"
6) "30"
7) "chemical"
8) "40"

以上是redis入门的相关知识,后续继续深入。

参考博文:

(1)https://blog.csdn.net/weixin_30572613/article/details/94879855 编译redis报错/bin/sh :cc:command not found

(2)https://www.cnblogs.com/ruiy/p/4964982.html  jemalloc/jemalloc.h: No such file or directory编译报错 

(3)https://www.jianshu.com/p/56999f2b8e3b redis入门

(4)https://www.jianshu.com/p/bda760565a2a redis和memoryCache

相关推荐