Redis基础命令笔记

Published: 2016-02-20

Tags: Redis

本文总阅读量

“只能说...花时间把人脑中的数据存储在电脑中是值得的” —— 有感于半年前学过的Redis,现在想做个测试,然而基础命令都快忘光了,算了,重新整理一下,方便以后使用

数据类型及基本操作

String Hash List Set Zset五种数据类型

String

String类型可以包含任何数据,如图片或着序列化对象

最基本的用法是setget

127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set name david
OK
127.0.0.1:6379> get name 
"david"

setnx (nx为not exist)可以防止值被覆盖

127.0.0.1:6379> get name
"solideo"
127.0.0.1:6379> setnx name david
(integer) 0
127.0.0.1:6379> get name
"solideo"

setex 可以设置键值对的有效期

设置color的颜色为red,有效期为10s

127.0.0.1:6379> setex color 10 red
OK
127.0.0.1:6379> get color
"red"
127.0.0.1:6379> get color
"red"
127.0.0.1:6379> get color
(nil)

mset 一次性可以设置多个值

127.0.0.1:6379> mset key1 value1 key2 value2 key3 value3
OK
127.0.0.1:6379> get key1
"value1"
127.0.0.1:6379> get key2
"value2"
127.0.0.1:6379> get key3
"value3"

msetnx 一次性设置多个值,同理于setnx,不会覆盖已经存在的值,并且一个没有成功则都不会设置

127.0.0.1:6379> msetnx key3 value3 key4 value4
(integer) 0
127.0.0.1:6379> get key3
"value3"
127.0.0.1:6379> get key4
(nil)

getset 设置一个新值并返回旧值

127.0.0.1:6379> get name
"solideo"
127.0.0.1:6379> getset name dongdong
"solideo"
127.0.0.1:6379> get name
"dongdong"

mget 批量获取数据

127.0.0.1:6379> mget key1 key2 key3
1) "value1"
2) "value2"
3) "value3"

incr 值增加一

127.0.0.1:6379> set age 16
OK
127.0.0.1:6379> incr age
(integer) 17
127.0.0.1:6379> get age
"17"

incrby 增加指定值,不存在key时会默认原value值为0

数值为正为加,数值为负则减

127.0.0.1:6379> get age
"17"
127.0.0.1:6379> incrby age 5
(integer) 22
127.0.0.1:6379> incrby age -1
(integer) 21
127.0.0.1:6379> get age
"21"

append 追加值

127.0.0.1:6379> get name
"dongdong"
127.0.0.1:6379> append name .org
(integer) 12
127.0.0.1:6379> get name
"dongdong.org"

strlen 查看长度

127.0.0.1:6379> get name
"dongdong.org"
127.0.0.1:6379> strlen name
(integer) 12

Hash

Hash特别适合存储对象

hset 设置一个哈希表,字段和值

hget 获取数据

127.0.0.1:6379> hset user01 name dongdong
(integer) 1
127.0.0.1:6379> hget user01 name
"dongdong"

hsetnx 设置哈希表,如果存在则忽略

127.0.0.1:6379> hsetnx user01 name ming
(integer) 0
127.0.0.1:6379> hsetnx user02 name ying
(integer) 1

hmset 设置多个字段

127.0.0.1:6379> hmset user03 name tan age 20
OK

hmget 获取多个字段

127.0.0.1:6379> hmset user03 name tan age 20
OK
127.0.0.1:6379> hmget user03 name age
1) "tan"
2) "20"

hincrby 增加某一个键值的数值

127.0.0.1:6379> hincrby user03 age 5
(integer) 25
127.0.0.1:6379> hget user03 age
"25"

hexists 测试指定字段是否存在

127.0.0.1:6379> hexists user03 age
(integer) 1
127.0.0.1:6379> hexists user03 name
(integer) 1
127.0.0.1:6379> hexists user03 sex
(integer) 0

hlen 返回哈希表中的键的数量

127.0.0.1:6379> hlen user03
(integer) 2

hdel 删除指定哈希的一个字段

127.0.0.1:6379> hdel user03 age
(integer) 1
127.0.0.1:6379> hget user03 age
(nil)

hkeys 返回哈希表中所有的keys

127.0.0.1:6379> hmset user04 name dongdong age 20 sex male
OK
127.0.0.1:6379> hkeys user04
1) "name"
2) "age"
3) "sex"

hvals 获取哈希表中的值

127.0.0.1:6379> hvals user04
1) "dongdong"
2) "20"
3) "male"

hgetall 返回字段及值

127.0.0.1:6379> hgetall user04
1) "name"
2) "dongdong"
3) "age"
4) "20"
5) "sex"
6) "male"

List

链表结构,主要功能是push、pop,相当于子元素都是string类型的双向列表,这样,List即可以作为栈,又可以作为队列

lpush 从头部压入一个元素(栈)

lrange 读取列列表,0代表从第一个元素开始读取,-1代表从尾部开始

127.0.0.1:6379> lpush list01 world
(integer) 1
127.0.0.1:6379> lpush list01 hello
(integer) 2
127.0.0.1:6379> lrange list01 0 -1
1) "hello"
2) "world"
127.0.0.1:6379> 

rpush 从尾部压入一个元素(队列)

127.0.0.1:6379> rpush list02 hello
(integer) 1
127.0.0.1:6379> rpush list02 world
(integer) 2
127.0.0.1:6379> lrange list02 0 -1
1) "hello"
2) "world"

linsert 在中间压入元素

127.0.0.1:6379> lpush list03 one
(integer) 1
127.0.0.1:6379> lpush list03 three
(integer) 2
127.0.0.1:6379> lrange list03 0 -1
1) "three"
2) "one"
127.0.0.1:6379> linsert list03 before one two
(integer) 3
127.0.0.1:6379> lrange list03 0 -1
1) "three"
2) "two"
3) "one"

lset 修改某一元素的值

127.0.0.1:6379> lpush list04 one
(integer) 1
127.0.0.1:6379> lpush list04 two
(integer) 2
127.0.0.1:6379> lpush list04 four
(integer) 3
127.0.0.1:6379> lrange list04 0 -1
1) "four"
2) "two"
3) "one"
127.0.0.1:6379> lset list04 0 three
OK
127.0.0.1:6379> lrange list04 0 -1
1) "three"
2) "two"
3) "one"

lrem 删除指定值的元素

127.0.0.1:6379> lrange list05 0 -1
1) "one"
2) "one"
3) "one"
4) "two"
5) "three"
127.0.0.1:6379> lrem list05 3 one
(integer) 3
127.0.0.1:6379> lrange list05 0 -1
1) "two"
2) "three"

ltrim 保留指定范围内的元素(后边两个数字为范围)

127.0.0.1:6379> lrange list06 0 -1
1) "one"
2) "two"
3) "three"
4) "four"
127.0.0.1:6379> ltrim list06 1 2
OK
127.0.0.1:6379> lrange list06 0 -1
1) "two"
2) "three"

lpop 从头部弹出一个数据并返回

127.0.0.1:6379> lrange list07 0 -1
1) "four"
2) "three"
3) "two"
4) "one"
127.0.0.1:6379> lpop list07 
"four"

rpop 从尾部弹出一个数据并返回

127.0.0.1:6379> lrange list07 0 -1
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> rpop list07
"one"
127.0.0.1:6379> lrange list07 0 -1
1) "three"
2) "two"

rpoplpush 从一个列表的尾部弹出一个元素放入另一个列表的头部

127.0.0.1:6379> lrange list08 0 -1
1) "one"
2) "two"
3) "three"
127.0.0.1:6379> lrange list09 0 -1
1) "hello"
2) "world"
127.0.0.1:6379> rpoplpush list08 list09
"three"
127.0.0.1:6379> lrange list09 0 -1
1) "three"
2) "hello"
3) "world"

lindex 返回名称为key的list中index位置的元素

127.0.0.1:6379> rpoplpush list08 list09
"three"
127.0.0.1:6379> lrange list09 0 -1
1) "three"
2) "hello"
3) "world"
127.0.0.1:6379> lindex list09 0
"three"
127.0.0.1:6379> lindex list09 1
"hello" 

Set

Set是集合,它是string类型的无序集合

sadd 向集合添加元素

127.0.0.1:6379> sadd set01 one
(integer) 1
127.0.0.1:6379> sadd set01 two
(integer) 1
127.0.0.1:6379> sadd set01 two
(integer) 0

smembers 查看集合内的元素

127.0.0.1:6379> smembers set01
1) "one"
2) "two"

srem 删除名称为key的set中额元素

127.0.0.1:6379> sadd set02 one
(integer) 1
127.0.0.1:6379> sadd set02 two
(integer) 1
127.0.0.1:6379> sadd set02 three
(integer) 1
127.0.0.1:6379> srem set02 one
(integer) 1
127.0.0.1:6379> smembers set02
1) "two"
2) "three"

spop 从集合中弹出一个元素(随机弹出)

127.0.0.1:6379> smembers set02
1) "two"
2) "three"
127.0.0.1:6379> spop set02
"three"

sdiff 两个集合求差

如下面的,是根据set04求set03中不在set04中的元素

127.0.0.1:6379> smembers set03
1) "one"
2) "two"
127.0.0.1:6379> smembers set04
1) "one"
2) "three"
127.0.0.1:6379> sdiff set03 set04
1) "two"

sdiffstore 将上面的命令sdiff的结果保存在一个集合中

127.0.0.1:6379> sdiff set03 set04
1) "two"
127.0.0.1:6379> sdiffstore set05 set03 set04
(integer) 1
127.0.0.1:6379> smembers set05
1) "two"

sinter 求两个集合的交集

127.0.0.1:6379> smembers set03 
1) "one"
2) "two"
127.0.0.1:6379> smembers set04
1) "one"
2) "three"
127.0.0.1:6379> sinter set03 set04
1) "one"

sinterstore 同理于sdiffstore

sunion 两个集合的并集

127.0.0.1:6379> smembers set03
1) "one"
2) "two"
127.0.0.1:6379> smembers set04
1) "one"
2) "three"
127.0.0.1:6379> sunion set03 set04
1) "one"
2) "two"
3) "three"

sunionstore 同理于 sdiffstore

smove 从一个集合中移除一个元素保存到另一个集合

127.0.0.1:6379> smembers set05
1) "two"
127.0.0.1:6379> smembers set06
(empty list or set)
127.0.0.1:6379> smove set05 set06 two
(integer) 1
127.0.0.1:6379> smembers set05
(empty list or set)
127.0.0.1:6379> smembers set06
1) "two"

scard 查看集合中的元素个数

127.0.0.1:6379> scard set03
(integer) 2
127.0.0.1:6379> scard set04
(integer) 2
127.0.0.1:6379> scard set05
(integer) 0

sismember 判断某个元素是否在集合中

127.0.0.1:6379> sismember set02 one
(integer) 0
127.0.0.1:6379> sismember set02 three
(integer) 1
127.0.0.1:6379> sismember set02 123
(integer) 0

srandmember 随机返回一个元素,但是不删除元素

127.0.0.1:6379> smembers set03
1) "one"
2) "two"
127.0.0.1:6379> srandmember set03
"one"
127.0.0.1:6379> srandmember set03
"one"
127.0.0.1:6379> srandmember set03
"two"
127.0.0.1:6379> srandmember set03
"two"

zset

zset是set的升级版本,为有序集合

zadd 向有序集合添加元素

127.0.0.1:6379> zadd zset01 1 dongdong
(integer) 1
127.0.0.1:6379> zadd zset01 1 dongdong
(integer) 0
127.0.0.1:6379> zadd zset01 2 dongdong 
(integer) 0
127.0.0.1:6379> zrange zset01 0 -1 withscores
1) "dongdong"
2) "2"

当存在相同数据时不会插入数据,但是会修改数据的序号

zrange 查询有序集合

例子如上,0为第一个元素,-1为最后一个,并且withscores代表的是输出序号

zrem 从有序集合中删除元素

127.0.0.1:6379> zrem zset02 two
(integer) 1
127.0.0.1:6379> zrange zset02 0 -1 withscores
1) "one"
2) "1"

zincrby 以指定值增加或减少值的顺序

127.0.0.1:6379> zrange zset02 0 -1 withscores
1) "one"
2) "1"
127.0.0.1:6379> zincrby zset02 2 one
"3"
127.0.0.1:6379> zrange zset02 0 -1 withscores
1) "one"
2) "3"

zrank 升序排序,返回元素的索引

127.0.0.1:6379> zrange zset03 0 -1 withscores
1) "one"
2) "1"
3) "three"
4) "2"
5) "two"
6) "2"
127.0.0.1:6379> zrank zset03 three
(integer) 1
127.0.0.1:6379> zrank zset03 two
(integer) 2

zrevrank 降序排序,返回元素索引

127.0.0.1:6379> zrange zset03 0 -1 withscores
1) "one"
2) "1"
3) "three"
4) "2"
5) "two"
6) "2"
127.0.0.1:6379> zrevrank zset03 three
(integer) 1
127.0.0.1:6379> zrevrank zset03 two
(integer) 0

zrevrange 降序显示有序列表的数据

127.0.0.1:6379> zrevrange zset03 0 -1 withscores
1) "two"
2) "2"
3) "three"
4) "2"
5) "one"
6) "1"

zrangebyscore 排序后输出范围内的有序集合,根据socre确定范围

127.0.0.1:6379> zrangebyscore zset03 2 3 withscores
1) "three"
2) "2"
3) "two"
4) "2"

zcount 返回集合中score在给定区间的数量

127.0.0.1:6379> zrange zset03 0 -1 withscores
1) "one"
2) "1"
3) "three"
4) "2"
5) "two"
6) "2"
127.0.0.1:6379> zcount zset03 2 5
(integer) 2

zcard 返回集合中的元素个数

127.0.0.1:6379> zrange zset03 0 -1 withscores
1) "one"
2) "1"
3) "three"
4) "2"
5) "two"
6) "2"
127.0.0.1:6379> zcard zset03
(integer) 3

zremrangebyrank 按下标排序删除区间内元素

127.0.0.1:6379> zrange zset03 0 -1 withscores
1) "one"
2) "1"
3) "three"
4) "2"
5) "two"
6) "2"
127.0.0.1:6379> zremrangebyrank zset03 1 1
(integer) 1
127.0.0.1:6379> zrange zset03 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"

zremrangebyscore 按score排序删除区间内元素

127.0.0.1:6379> zrange zset03 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
127.0.0.1:6379> zremrangebyscore zset03 1 2
(integer) 2
127.0.0.1:6379> zrange zset03 0 -1 withscores
1) "three"
2) "3"

OK, 花了小半天整理redis的基本数据类型~~~