首頁 > 資料庫 > mysql教程 > NoSQL数据库Redis使用命令简介

NoSQL数据库Redis使用命令简介

WBOY
發布: 2016-06-07 14:49:53
原創
1004 人瀏覽過

NoSQL 数据库Redis使用命令简介 NOSQL数据库可以按照它们的数据模型分成4类: 1.键-值对存储库(Key-Value) redis…… 2.BigTable实现(BigTable-implementations) HBase 3.文档库(Document-stores) mongo DB 4.图形数据库(Graph Database) 代表: Neo

NoSQL 数据库Redis使用命令简介

NOSQL数据库可以按照它们的数据模型分成4类:

1.键-值对存储库(Key-Value)
redis……
2.BigTable实现(BigTable-implementations)
HBase
3.文档库(Document-stores)
mongo DB
4.图形数据库(Graph Database)
代表: Neo4j

Redis 操作简介

服务启动

redis服务启动命令: redis-server /usr/local/etc/redis.conf
redis服务启动后,可以使用redis 客户端操作访问redis了: redis-cli

配置查看

查看配置项命令: CONFIG GET
在客户端命令模式下,输入config get * 可以看到redis服务所有的配置。

redis库

redis服务缺省有16个库,从db0到db15,使用select 可以选择指定的库。

redis key

Redis的key和支持的数据类型及其操作

  1. keys
    redis本质上一个key-value db,所以我们首先来看看他的key.首先key也是字符串类型,但是key中不能包括边界字符
    由于key不是binary safe的字符串,所以像”my key”和”mykey\n”这样包含空格和换行的key是不允许的
    顺便说一下在redis内部并不限制使用binary字符,这是redis协议限制的。”\r\n”在协议格式中会作为特殊字符。
  2. redis 1.2以后的协议中部分命令已经开始使用新的协议格式了(比如MSET)。

  3. 关于key的一个格式约定介绍下,object-type:id:field。比如user:1000:password,blog:xxidxx:title
    还有key的长度最好不要太长。道理很明显占内存啊,而且查找时候相对短key也更慢。不过也推荐过短的key,
    比如u:1000:pwd,这样的。显然没上面的user:1000:password可读性好。

key相关操作命令

下面介绍下key相关的命令
1 DEL key 该命令用于在 key 存在是删除 key。
2 DUMP key 序列化给定 key ,并返回被序列化的值。
3 EXISTS key 检查给定 key 是否存在。
4 EXPIRE key seconds 为给定 key 设置过期时间。
5 EXPIREAT key timestamp EXPIREAT 的作用和 EXPIRE 类似,都用于为 key 设置过期时间。 不同在于 EXPIREAT 命令接受的时间参数是 UNIX 时间戳(unix timestamp)。
6 PEXPIRE key milliseconds 设置 key 的过期时间亿以毫秒计。
7 PEXPIREAT key milliseconds-timestamp 设置 key 过期时间的时间戳(unix timestamp) 以毫秒计
8 KEYS pattern 查找所有符合给定模式( pattern)的 key 。
9 MOVE key db 将当前数据库的 key 移动到给定的数据库 db 当中。
10 PERSIST key 移除 key 的过期时间,key 将持久保持。
11 PTTL key 以毫秒为单位返回 key 的剩余的过期时间。
12 TTL key 以秒为单位,返回给定 key 的剩余生存时间(TTL, time to live)。
13 RANDOMKEY 从当前数据库中随机返回一个 key 。
14 RENAME key newkey 修改 key 的名称
15 RENAMENX key newkey 仅当 newkey 不存在时,将 key 改名为 newkey 。
16 TYPE key 返回 key 所储存的值的类型。

del key1 key2 ….keyN 删除给定key,返回删除key的数目,0表示给定key都不存在
type key 返回给定key的value类型。返回 none 表示不存在key,string字符类型,list 链表类型 set 无序集合类型…

<code class=" hljs livecodeserver">redis> <span class="hljs-built_in">set</span> testa dsf
OK
redis> <span class="hljs-built_in">set</span> testb dsaf
OK
redis> <span class="hljs-built_in">set</span> testc dff
OK

redis> <span class="hljs-built_in">keys</span> t*
<span class="hljs-number">1</span>) <span class="hljs-string">"testb"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"testc"</span>
<span class="hljs-number">3</span>) <span class="hljs-string">"testa"</span>

redis> <span class="hljs-built_in">keys</span> test[ab]
<span class="hljs-number">1</span>) <span class="hljs-string">"testb"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"testa"</span>

redis> <span class="hljs-built_in">keys</span> test*
<span class="hljs-number">1</span>) <span class="hljs-string">"testb"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"testc"</span>
<span class="hljs-number">3</span>) <span class="hljs-string">"testa"</span>

redis> <span class="hljs-built_in">keys</span> *
<span class="hljs-number">1</span>) <span class="hljs-string">"testb"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"testa"</span>
<span class="hljs-number">3</span>) <span class="hljs-string">"intval"</span>
<span class="hljs-number">4</span>) <span class="hljs-string">"abc"</span>
<span class="hljs-number">5</span>) <span class="hljs-string">"testc"</span></code>
登入後複製

dbsize 返回当前数据库的key数量
select db-index 通过索引选择数据库,默认连接的数据库所有是0,默认数据库数是16个。返回1表示成功,0失败

rename oldkey newkey 原子的重命名一个key,如果newkey存在,将会被覆盖,返回1表示成功,0失败。可能是oldkey不存在或者和newkey相同
renamenx oldkey newkey 同上,但是如果newkey存在返回失败

expire key seconds 为key指定过期时间,单位是秒。返回1成功,0表示key已经设置过过期时间或者不存在
ttl key 返回设置过过期时间的key的剩余过期秒数 -1表示key不存在或者没有设置过过期时间

move key db-index 将key从当前数据库移动到指定数据库。返回1成功。0 如果key不存在,或者已经在指定数据库中

<code class=" hljs livecodeserver">redis> <span class="hljs-built_in">keys</span> <span class="hljs-operator">a</span>*
<span class="hljs-number">1</span>) <span class="hljs-string">"abc"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"acc"</span>
redis> move acc <span class="hljs-number">1</span>
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">1</span></code>
登入後複製

flushdb 删除 当前数据库中所有key,此方法不会失败。慎用

flushall 删除 所有数据库中的所有key,此方法不会失败。更加慎用


Redis支持的数据类型及其操作

数据类型

Redis支持五种类型的数据结构:

  • 字符串 (String)
  • 有序集合 (ZSet)
  • 集合 (Set)
  • 哈希 (Hash)
  • 列表 (List)

String(字符串)

string是redis最基本的类型,string类型是二进制安全的。意思是redis的string可以包含任何数据。
比如jpg图片或者序列化的对象 。string类型是Redis最基本的数据类型,一个键最大能存储512MB。

Hash(哈希)
每个 hash 可以存储 2^32 - 1 键值对(40多亿)。在Redis里,hash 是一个string类型的field和value的映射表,hash特别适合用于存储对象。
所以,redis的hash对应了java、C#等语言里的map。

List(列表)
Redis 的 list 类型其实就是一个每个子元素都是string类型的双向链表。你可以将 元素添加到列表的头和尾。

set(集合)
Redis的set是string类型的无序集合。set的是通过hash table实现的,所以添加,删除,查找的复杂度都是O(1)。

zset(sorted set:有序集合)
Redis zset 和 set 一样也是string类型元素的集合,且不允许重复的成员。不同的是每个元素都会关联一个double类型的score。
redis正是通过分数来为集合中的成员进行从小到大的排序。zset的成员是唯一的,但分数(score)却可以重复。

string操作

<code>1   SET key value 设置指定 key 的值
2   GET key 获取指定 key 的值。
3   GETRANGE key start end 返回 key 中字符串值的子字符
4   GETSET key value 将给定 key 的值设为 value ,并返回 key 的旧值(old value)。
5   GETBIT key offset 对 key 所储存的字符串值,获取指定偏移量上的位(bit)。
6   MGET key1 [key2..] 获取所有(一个或多个)给定 key 的值。
7   SETBIT key offset value 对 key 所储存的字符串值,设置或清除指定偏移量上的位(bit)。
8   SETEX key seconds value 将值 value 关联到 key ,并将 key 的过期时间设为 seconds (以秒为单位)。
9   SETNX key value 只有在 key 不存在时设置 key 的值。
10  SETRANGE key offset value 用 value 参数覆写给定 key 所储存的字符串值,从偏移量 offset 开始。
11  STRLEN key 返回 key 所储存的字符串值的长度。
12  MSET key value [key value ...] 同时设置一个或多个 key-value 对。
13  MSETNX key value [key value ...] 同时设置一个或多个 key-value 对,当且仅当所有给定 key 都不存在。
14  PSETEX key milliseconds value 这个命令和 SETEX 命令相似,但它以毫秒为单位设置 key 的生存时间,而不是像 SETEX 命令那样,以秒为单位。
15  INCR key 将 key 中储存的数字值增一。
16  INCRBY key increment 将 key 所储存的值加上给定的增量值(increment) 。
17  INCRBYFLOAT key increment 将 key 所储存的值加上给定的浮点增量值(increment) 。
18  DECR key 将 key 中储存的数字值减一。
19  DECRBY key decrement key 所储存的值减去给定的减量值(decrement) 。
20  APPEND key value 如果 key 已经存在并且是一个字符串, APPEND 命令将 value 追加到 key 原来的值的末尾。
</code>
登入後複製

INCR*和DECR*操作: 对整型值进行处理加减的操作。

<code class=" hljs vhdl"><span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>> get abc
<span class="hljs-string">"abc"</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>> INCR abc
(error) ERR value <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> an <span class="hljs-typename">integer</span> <span class="hljs-keyword">or</span> <span class="hljs-keyword">out</span> <span class="hljs-keyword">of</span> <span class="hljs-keyword">range</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>> <span class="hljs-keyword">TYPE</span> abc
<span class="hljs-typename">string</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>> set intval <span class="hljs-number">2</span>
OK
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>> <span class="hljs-keyword">type</span> intval
<span class="hljs-typename">string</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>> INCR intval
(<span class="hljs-typename">integer</span>) <span class="hljs-number">3</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>> INCRBYFLOAT intval  <span class="hljs-number">0.15</span>
<span class="hljs-string">"3.15"</span></code>
登入後複製

Hash操作

Redis hash 命令以H开头,代表Hash。

下表列出了 redis hash 基本的相关命令:

序号 命令及描述
1 HDEL key field2 [field2] 删除一个或多个哈希表字段
2 HEXISTS key field 查看哈希表 key 中,指定的字段是否存在。
3 HGET key field 获取存储在哈希表中指定字段的值
4 HGETALL key 获取在哈希表中指定 key 的所有字段和值
5 HINCRBY key field increment 为哈希表 key 中的指定字段的整数值加上增量 increment 。
6 HINCRBYFLOAT key field increment 为哈希表 key 中的指定字段的浮点数值加上增量 increment 。
7 HKEYS key 获取所有哈希表中的字段
8 HLEN key 获取哈希表中字段的数量
9 HMGET key field1 [field2] 获取所有给定字段的值
10 HMSET key field1 value1 [field2 value2 ] 同时将多个 field-value (域-值)对设置到哈希表 key 中。
11 HSET key field value 将哈希表 key 中的字段 field 的值设为 value 。
12 HSETNX key field value 只有在字段 field 不存在时,设置哈希表字段的值。
13 HVALS key 获取哈希表中所有值
14 HSCAN key cursor [MATCH pattern] [COUNT count] 迭代哈希表中的键值对。

<code class=" hljs applescript"><span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>> HMSET product:<span class="hljs-number">10001</span> <span class="hljs-property">name</span> <span class="hljs-string">"redis tutorial"</span> description <span class="hljs-string">"redis basic commands for caching"</span> likes <span class="hljs-number">20</span> visitors <span class="hljs-number">23000</span>
OK
redis <span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>> HGETALL product:<span class="hljs-number">10001</span>
<span class="hljs-number">1</span>) <span class="hljs-string">"name"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"redis tutorial"</span>
<span class="hljs-number">3</span>) <span class="hljs-string">"description"</span>
<span class="hljs-number">4</span>) <span class="hljs-string">"redis basic commands for caching"</span>
<span class="hljs-number">5</span>) <span class="hljs-string">"likes"</span>
<span class="hljs-number">6</span>) <span class="hljs-string">"20"</span>
<span class="hljs-number">7</span>) <span class="hljs-string">"visitors"</span>
<span class="hljs-number">8</span>) <span class="hljs-string">"23000"</span>

<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">3</span>]> HLEN product:<span class="hljs-number">10001</span>
(<span class="hljs-type">integer</span>) <span class="hljs-number">4</span>

<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">3</span>]> HKEYS product:<span class="hljs-number">10001</span>
<span class="hljs-number">1</span>) <span class="hljs-string">"name"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"description"</span>
<span class="hljs-number">3</span>) <span class="hljs-string">"likes"</span>
<span class="hljs-number">4</span>) <span class="hljs-string">"visitors"</span>

<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">3</span>]> HGET  product:<span class="hljs-number">10001</span> <span class="hljs-property">name</span>
<span class="hljs-string">"redis tutorial"</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">3</span>]> HGET  product:<span class="hljs-number">10001</span> likes
<span class="hljs-string">"20"</span>

<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">3</span>]> HINCRBY product:<span class="hljs-number">10001</span> visitors <span class="hljs-number">1</span>
(<span class="hljs-type">integer</span>) <span class="hljs-number">23001</span>


Note: 
HINCRBY可以创建一个新的field:
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">3</span>]> HINCRBY product:<span class="hljs-number">10001</span> visitoes <span class="hljs-number">1</span>
(<span class="hljs-type">integer</span>) <span class="hljs-number">1</span>

<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">3</span>]> HDEL product:<span class="hljs-number">10001</span> visitoes
(<span class="hljs-type">integer</span>) <span class="hljs-number">1</span>

<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">3</span>]> HGETALL product:<span class="hljs-number">10001</span>
<span class="hljs-number">1</span>) <span class="hljs-string">"name"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"redis tutorial"</span>
<span class="hljs-number">3</span>) <span class="hljs-string">"description"</span>
<span class="hljs-number">4</span>) <span class="hljs-string">"redis basic commands for caching"</span>
<span class="hljs-number">5</span>) <span class="hljs-string">"likes"</span>
<span class="hljs-number">6</span>) <span class="hljs-string">"20"</span>
<span class="hljs-number">7</span>) <span class="hljs-string">"visitors"</span>
<span class="hljs-number">8</span>) <span class="hljs-string">"23001"</span>


<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">3</span>]> HVALS product:<span class="hljs-number">10001</span>
<span class="hljs-number">1</span>) <span class="hljs-string">"redis tutorial"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"redis basic commands for caching"</span>
<span class="hljs-number">3</span>) <span class="hljs-string">"20"</span>
<span class="hljs-number">4</span>) <span class="hljs-string">"23001"</span></code>
登入後複製

List

Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素导列表的头部(左边)或者尾部(右边).
一个列表最多可以包含 2^32 - 1 个元素 (4294967295, 每个列表超过40亿个元素)。

Redis的列表操作命令大多以L开头,代表List。

列表命令
下表列出了列表相关的基本命令:
B – Block, L – first, R – last
序号 命令及描述
1 BLPOP key1 [key2 ] timeout 移出并获取列表的第一个元素, 如果列表没有元素会阻塞(block)列表直到等待超时或发现可弹出元素为止。
2 BRPOP key1 [key2 ] timeout 移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
3 BRPOPLPUSH source destination timeout 从列表中弹出一个值,将弹出的元素插入到另外一个列表中并返回它; 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
4 LINDEX key index 通过索引获取列表中的元素
5 LINSERT key BEFORE|AFTER pivot value 在列表的元素前或者后插入元素
6 LLEN key 获取列表长度
7 LPOP key 移出并获取列表的第一个元素
8 LPUSH key value1 [value2] 将一个或多个值插入到列表头部
9 LPUSHX key value 将一个或多个值插入到已存在的列表头部
10 LRANGE key start stop 获取列表指定范围内的元素
11 LREM key count value 移除列表元素
12 LSET key index value 通过索引设置列表元素的值
13 LTRIM key start stop 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。
14 RPOP key 移除并获取列表最后一个元素
15 RPOPLPUSH source destination 移除列表的最后一个元素,并将该元素添加到另一个列表并返回
16 RPUSH key value1 [value2] 在列表中添加一个或多个值
17 RPUSHX key value 为已存在的列表添加值

示例:

<code class=" hljs livecodeserver"><span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">4</span>]> LPUSH product:book  redis
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">1</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">4</span>]> LPUSH product:book  mongodb
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">2</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">4</span>]> LPUSH product:book  mongodb <span class="hljs-operator">in</span> action
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">5</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">4</span>]> LPUSH product:book  <span class="hljs-string">"mongodb in action"</span>
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">6</span>
<span class="hljs-comment">
// 长度</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">4</span>]> LLEN product:book
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">6</span>
<span class="hljs-comment">
// index 从0开始,指定的大于列表其长度时,获取到最后的元素。</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">4</span>]> LRANGE product:book <span class="hljs-number">0</span> <span class="hljs-number">7</span>
<span class="hljs-number">1</span>) <span class="hljs-string">"mongodb in action"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"action"</span>
<span class="hljs-number">3</span>) <span class="hljs-string">"in"</span>
<span class="hljs-number">4</span>) <span class="hljs-string">"mongodb"</span>
<span class="hljs-number">5</span>) <span class="hljs-string">"mongodb"</span>
<span class="hljs-number">6</span>) <span class="hljs-string">"redis"</span>

<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">4</span>]> RPUSH product:book <span class="hljs-string">"spring in action"</span>
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">7</span>

<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">4</span>]> LRANGE product:book <span class="hljs-number">0</span> <span class="hljs-number">10</span>
<span class="hljs-number">1</span>) <span class="hljs-string">"mongodb in action"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"action"</span>
<span class="hljs-number">3</span>) <span class="hljs-string">"in"</span>
<span class="hljs-number">4</span>) <span class="hljs-string">"mongodb"</span>
<span class="hljs-number">5</span>) <span class="hljs-string">"mongodb"</span>
<span class="hljs-number">6</span>) <span class="hljs-string">"redis"</span>
<span class="hljs-number">7</span>) <span class="hljs-string">"spring in action"</span>
<span class="hljs-comment">
// index: start from 0.</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">4</span>]> lset product:book <span class="hljs-number">3</span> <span class="hljs-string">"what"</span>
OK

<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">4</span>]> LRANGE product:book <span class="hljs-number">0</span> <span class="hljs-number">10</span>
<span class="hljs-number">1</span>) <span class="hljs-string">"mongodb in action"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"action"</span>
<span class="hljs-number">3</span>) <span class="hljs-string">"in"</span>
<span class="hljs-number">4</span>) <span class="hljs-string">"what"</span>
<span class="hljs-number">5</span>) <span class="hljs-string">"mongodb"</span>
<span class="hljs-number">6</span>) <span class="hljs-string">"redis"</span>
<span class="hljs-number">7</span>) <span class="hljs-string">"spring in action"</span>
<span class="hljs-comment">
//</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">4</span>]> RPOP product:book
<span class="hljs-string">"spring in action"</span>

<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">4</span>]> LRANGE product:book <span class="hljs-number">0</span> <span class="hljs-number">10</span>
<span class="hljs-number">1</span>) <span class="hljs-string">"mongodb in action"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"action"</span>
<span class="hljs-number">3</span>) <span class="hljs-string">"in"</span>
<span class="hljs-number">4</span>) <span class="hljs-string">"what"</span>
<span class="hljs-number">5</span>) <span class="hljs-string">"mongodb"</span>
<span class="hljs-number">6</span>) <span class="hljs-string">"redis"</span>

<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">4</span>]> LPOP product:book
<span class="hljs-string">"mongodb in action"</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">4</span>]> LRANGE product:book <span class="hljs-number">0</span> <span class="hljs-number">10</span>
<span class="hljs-number">1</span>) <span class="hljs-string">"action"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"in"</span>
<span class="hljs-number">3</span>) <span class="hljs-string">"what"</span>
<span class="hljs-number">4</span>) <span class="hljs-string">"mongodb"</span>
<span class="hljs-number">5</span>) <span class="hljs-string">"redis"</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">4</span>]> 


<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">4</span>]> LINDEX product:book <span class="hljs-number">1</span>
<span class="hljs-string">"in"</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">4</span>]> LRANGE product:book <span class="hljs-number">0</span> <span class="hljs-number">10</span>
<span class="hljs-number">1</span>) <span class="hljs-string">"action"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"in"</span>
<span class="hljs-number">3</span>) <span class="hljs-string">"what"</span>
<span class="hljs-number">4</span>) <span class="hljs-string">"mongodb"</span>
<span class="hljs-number">5</span>) <span class="hljs-string">"redis"</span>
<span class="hljs-comment">
// 移除元素 -- value必须指定正确,否则,不会移除。</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">4</span>]> lrem product:book <span class="hljs-number">2</span>  <span class="hljs-string">"what"</span>
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">1</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">4</span>]> LRANGE product:book <span class="hljs-number">0</span> <span class="hljs-number">10</span>
<span class="hljs-number">1</span>) <span class="hljs-string">"action"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"in"</span>
<span class="hljs-number">3</span>) <span class="hljs-string">"mongodb"</span>
<span class="hljs-number">4</span>) <span class="hljs-string">"redis"</span></code>
登入後複製

Redis 集合(Set)

Redis的Set是string类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。

Redis中集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1)。
集合中最大的成员数为 2^32 - 1 (4294967295, 每个集合可存储40多亿个成员)。

Redis 集合命令
下表列出了 Redis 集合基本命令:集合的命令以S开头,表示Set.

序号 命令及描述
1 SADD key member1 [member2] 向集合添加一个或多个成员
2 SCARD key 获取集合的成员数
3 SDIFF key1 [key2] 返回给定所有集合的差集
4 SDIFFSTORE destination key1 [key2] 返回给定所有集合的差集并存储在 destination 中
5 SINTER key1 [key2] 返回给定所有集合的交集
6 SINTERSTORE destination key1 [key2] 返回给定所有集合的交集并存储在 destination 中
7 SISMEMBER key member 判断 member 元素是否是集合 key 的成员
8 SMEMBERS key 返回集合中的所有成员
9 SMOVE source destination member 将 member 元素从 source 集合移动到 destination 集合
10 SPOP key 移除并返回集合中的一个随机元素
11 SRANDMEMBER key [count] 返回集合中一个或多个随机数
12 SREM key member1 [member2] 移除集合中一个或多个成员
13 SUNION key1 [key2] 返回所有给定集合的并集
14 SUNIONSTORE destination key1 [key2] 所有给定集合的并集存储在 destination 集合中
15 SSCAN key cursor [MATCH pattern] [COUNT count] 迭代集合中的元素

示例:

<code class=" hljs livecodeserver"><span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">4</span>]> select <span class="hljs-number">5</span>
OK
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> dbsize
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">0</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> sadd product:<span class="hljs-built_in">set</span> iphone
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">1</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> dbsize
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">1</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> sadd product:<span class="hljs-built_in">set</span> nexus lumia
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">2</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> dbsize
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">1</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> SMEMBERS product:<span class="hljs-built_in">set</span>
<span class="hljs-number">1</span>) <span class="hljs-string">"iphone"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"lumia"</span>
<span class="hljs-number">3</span>) <span class="hljs-string">"nexus"</span>

<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> sadd book:<span class="hljs-built_in">set</span> <span class="hljs-string">"Spring in action"</span> <span class="hljs-string">"redis in action"</span>
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">2</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> SMEMBERS book:<span class="hljs-built_in">set</span>
<span class="hljs-number">1</span>) <span class="hljs-string">"redis in action"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"Spring in action"</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> SCARD
(error) ERR wrong <span class="hljs-built_in">number</span> <span class="hljs-operator">of</span> arguments <span class="hljs-keyword">for</span> <span class="hljs-string">'scard'</span> <span class="hljs-command"><span class="hljs-keyword">command</span></span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> SCARD product
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">0</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> SCARD product:book
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">0</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> SCARD product:<span class="hljs-built_in">set</span>
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">3</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> SCARD book:<span class="hljs-built_in">set</span>
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">2</span>


<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> SUNION book:<span class="hljs-built_in">set</span> product:<span class="hljs-built_in">set</span>
<span class="hljs-number">1</span>) <span class="hljs-string">"iphone"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"lumia"</span>
<span class="hljs-number">3</span>) <span class="hljs-string">"Spring in action"</span>
<span class="hljs-number">4</span>) <span class="hljs-string">"redis in action"</span>
<span class="hljs-number">5</span>) <span class="hljs-string">"nexus"</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> Sinter  book:<span class="hljs-built_in">set</span> product:<span class="hljs-built_in">set</span>
(<span class="hljs-constant">empty</span> list <span class="hljs-operator">or</span> <span class="hljs-built_in">set</span>)
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> sadd book:<span class="hljs-built_in">set</span> iphone
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">1</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> Sinter  book:<span class="hljs-built_in">set</span> product:<span class="hljs-built_in">set</span>
<span class="hljs-number">1</span>) <span class="hljs-string">"iphone"</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> SDIFF book:<span class="hljs-built_in">set</span> product:<span class="hljs-built_in">set</span>
<span class="hljs-number">1</span>) <span class="hljs-string">"Spring in action"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"redis in action"</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> SDIFF product:<span class="hljs-built_in">set</span>  book:<span class="hljs-built_in">set</span>
<span class="hljs-number">1</span>) <span class="hljs-string">"nexus"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"lumia"</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> SDIFFSTORE product:other  product:<span class="hljs-built_in">set</span>  book:<span class="hljs-built_in">set</span>
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">2</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> SMEMBERS product:other
<span class="hljs-number">1</span>) <span class="hljs-string">"nexus"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"lumia"</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> srem product:other nexus
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">1</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> SMEMBERS product:other
<span class="hljs-number">1</span>) <span class="hljs-string">"lumia"</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> smembers  book:<span class="hljs-built_in">set</span>
<span class="hljs-number">1</span>) <span class="hljs-string">"iphone"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"redis in action"</span>
<span class="hljs-number">3</span>) <span class="hljs-string">"Spring in action"</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> sadd book:<span class="hljs-built_in">set</span> <span class="hljs-string">"redis in action"</span>
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">0</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> smembers  book:<span class="hljs-built_in">set</span>
<span class="hljs-number">1</span>) <span class="hljs-string">"iphone"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"redis in action"</span>
<span class="hljs-number">3</span>) <span class="hljs-string">"Spring in action"</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">5</span>]> smembers  book:<span class="hljs-built_in">set</span>
<span class="hljs-number">1</span>) <span class="hljs-string">"iphone"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"redis in action"</span>
<span class="hljs-number">3</span>) <span class="hljs-string">"Spring in action"</span></code>
登入後複製

Redis 有序集合(sorted set)

Redis 有序集合和集合一样也是string类型元素的集合,且不允许重复的成员。 集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1)。 集合中最大的成员数为 2^32 - 1 (4294967295, 每个集合可存储40多亿个成员)。

不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。有序集合的成员是唯一的,但分数(score)却可以重复。

Redis 有序集合命令
下表列出了 redis 有序集合的基本命令,命令以Z开头:

序号 命令及描述
1 ZADD key score1 member1 [score2 member2] 向有序集合添加一个或多个成员,或者更新已存在成员的分数
2 ZCARD key 获取有序集合的成员数
3 ZCOUNT key min max 计算在有序集合中指定区间分数的成员数
4 ZINCRBY key increment member 有序集合中对指定成员的分数加上增量 increment
5 ZINTERSTORE destination numkeys key [key …] 计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 key 中
6 ZLEXCOUNT key min max 在有序集合中计算指定字典区间内成员数量
7 ZRANGE key start stop [WITHSCORES] 通过索引区间返回有序集合成指定区间内的成员
8 ZRANGEBYLEX key min max [LIMIT offset count] 通过字典区间返回有序集合的成员
9 ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT] 通过分数返回有序集合指定区间内的成员
10 ZRANK key member 返回有序集合中指定成员的索引
11 ZREM key member [member …] 移除有序集合中的一个或多个成员
12 ZREMRANGEBYLEX key min max 移除有序集合中给定的字典区间的所有成员
13 ZREMRANGEBYRANK key start stop 移除有序集合中给定的排名区间的所有成员
14 ZREMRANGEBYSCORE key min max 移除有序集合中给定的分数区间的所有成员
15 ZREVRANGE key start stop [WITHSCORES] 返回有序集中指定区间内的成员,通过索引,分数从高到底
16 ZREVRANGEBYSCORE key max min [WITHSCORES] 返回有序集中指定分数区间内的成员,分数从高到低排序
17 ZREVRANK key member 返回有序集合中指定成员的排名,有序集成员按分数值递减(从大到小)排序
18 ZSCORE key member 返回有序集中,成员的分数值
19 ZUNIONSTORE destination numkeys key [key …] 计算给定的一个或多个有序集的并集,并存储在新的 key 中
20 ZSCAN key cursor [MATCH pattern] [COUNT count] 迭代有序集合中的元素(包括元素成员和元素分值)

示例:

<code class=" hljs livecodeserver"><span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">6</span>]> zadd product:major:lesson <span class="hljs-number">1</span> java
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">1</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">6</span>]> dbsize
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">1</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">6</span>]> zadd product:major:lesson <span class="hljs-number">2</span> mongodb
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">1</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">6</span>]> zadd product:major:lesson <span class="hljs-number">3</span> redis
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">1</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">6</span>]> zadd product:major:lesson <span class="hljs-number">4</span> mysql
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">1</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">6</span>]> dbsize
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">1</span>

<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">6</span>]> ZRANGE product:major:lesson <span class="hljs-number">0</span> <span class="hljs-number">10</span> 
<span class="hljs-number">1</span>) <span class="hljs-string">"java"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"mongodb"</span>
<span class="hljs-number">3</span>) <span class="hljs-string">"redis"</span>
<span class="hljs-number">4</span>) <span class="hljs-string">"mysql"</span>

<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">6</span>]> ZRANGE product:major:lesson <span class="hljs-number">0</span> <span class="hljs-number">10</span>  withscores
<span class="hljs-number">1</span>) <span class="hljs-string">"java"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"1"</span>
<span class="hljs-number">3</span>) <span class="hljs-string">"mongodb"</span>
<span class="hljs-number">4</span>) <span class="hljs-string">"2"</span>
<span class="hljs-number">5</span>) <span class="hljs-string">"redis"</span>
<span class="hljs-number">6</span>) <span class="hljs-string">"3"</span>
<span class="hljs-number">7</span>) <span class="hljs-string">"mysql"</span>
<span class="hljs-number">8</span>) <span class="hljs-string">"4"</span>

<span class="hljs-comment">
//</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">6</span>]> ZRANGEBYSCOre product:major:lesson <span class="hljs-number">1</span> <span class="hljs-number">4</span> 
<span class="hljs-number">1</span>) <span class="hljs-string">"java"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"mongodb"</span>
<span class="hljs-number">3</span>) <span class="hljs-string">"redis"</span>
<span class="hljs-number">4</span>) <span class="hljs-string">"mysql"</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">6</span>]> ZRANGEBYSCOre product:major:lesson <span class="hljs-number">2</span> <span class="hljs-number">4</span> 
<span class="hljs-number">1</span>) <span class="hljs-string">"mongodb"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"redis"</span>
<span class="hljs-number">3</span>) <span class="hljs-string">"mysql"</span>

<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">6</span>]> ZCARD product:major:lesson
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">4</span>

<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">6</span>]> ZCARD product:major:lesson
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">4</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">6</span>]> ZCOUNT product:major:lesson <span class="hljs-number">0</span> <span class="hljs-number">100</span>
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">4</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">6</span>]> ZCOUNT product:major:lesson <span class="hljs-number">2</span> <span class="hljs-number">10</span>
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">3</span>
<span class="hljs-comment">
// 返回zset指定member的score</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">6</span>]> ZSCORE product:major:lesson mysql
<span class="hljs-string">"4"</span>
<span class="hljs-comment">
// 有序集合中指定成员的索引 -- start from 0.</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">6</span>]> ZRANK product:major:lesson mysql
(<span class="hljs-keyword">integer</span>) <span class="hljs-number">3</span>
<span class="hljs-comment">
// ZREVRange</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">6</span>]> ZREVRange  product:major:lesson <span class="hljs-number">2</span> <span class="hljs-number">4</span> 
<span class="hljs-number">1</span>) <span class="hljs-string">"mongodb"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"java"</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">6</span>]> ZREVRange  product:major:lesson <span class="hljs-number">2</span> <span class="hljs-number">3</span>
<span class="hljs-number">1</span>) <span class="hljs-string">"mongodb"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"java"</span>
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">6379</span>[<span class="hljs-number">6</span>]> ZREVRange  product:major:lesson <span class="hljs-number">0</span> <span class="hljs-number">1</span> 
<span class="hljs-number">1</span>) <span class="hljs-string">"mysql"</span>
<span class="hljs-number">2</span>) <span class="hljs-string">"redis"</span>
</code>
登入後複製

telnet方式连接redis

<code class=" hljs perl">
telnet> <span class="hljs-keyword">open</span> <span class="hljs-number">127.0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">1</span> <span class="hljs-number">6379</span>
Trying <span class="hljs-number">127.0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">1</span>...
Connected to localhost.
Escape character is <span class="hljs-string">'^]'</span>.
dbdize
-ERR unknown command <span class="hljs-string">'dbdize'</span>
dbsize
:<span class="hljs-number">5</span>
<span class="hljs-keyword">keys</span> t*
<span class="hljs-variable">*3</span>
<span class="hljs-variable">$5</span>
testb
<span class="hljs-variable">$5</span>
testa
<span class="hljs-variable">$5</span>
testc

<span class="hljs-keyword">keys</span> *
<span class="hljs-variable">*5</span>
<span class="hljs-variable">$5</span>
testb
<span class="hljs-variable">$5</span>
testa
<span class="hljs-variable">$6</span>
intval
<span class="hljs-variable">$3</span>
abc
<span class="hljs-variable">$5</span>
testc
<span class="hljs-keyword">shutdown</span>
Connection closed by foreign host.
</code>
登入後複製

关闭服务

使用shutdown命令来关闭服务。

<code class=" hljs perl"><span class="hljs-comment">#关闭指定端口的redis-server  </span>
<span class="hljs-variable">$redis</span>-cli -p <span class="hljs-number">6380</span> <span class="hljs-keyword">shutdown</span> </code>
登入後複製

或者在telnet连接后,输入shutdown命令关闭服务。

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板