基本使用
查看某个关键字的用法:help 关键字
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> help lpush
LPUSH key value [value ...]
summary: Prepend one or multiple values to a list
since: 1.0.0
group: list
删除某个key:del key
127.0.0.1:6379> get key1
"value1"
127.0.0.1:6379> del key1
(integer) 1
127.0.0.1:6379> get key1
(nil)
查询当前Redis所有key:keys *
127.0.0.1:6379> keys *
1) "k1"
2) "k3"
3) "k2"
4) "key1"
数据库操作
Redis默认有16个数据库,每个数据库的数据互不干扰,编号是[0,15]默认使用0号数据库。
切换数据库:select index
127.0.0.1:6379> get key1
"value1"
127.0.0.1:6379> select 2
OK
127.0.0.1:6379[2]> get key1
(nil)
127.0.0.1:6379[2]> set key1 hello
OK
127.0.0.1:6379[2]> get key1
"hello"
127.0.0.1:6379[2]> select 0
OK
127.0.0.1:6379> get key1
"value1"
查看当前数据库中key-value的数量:dbsize
127.0.0.1:6379> dbsize
(integer) 4
清空当前数据库的key-val:flushdb
清空所有数据库的key-val:flushall
127.0.0.1:6379[2]> keys *
1) "k1"
2) "k2"
127.0.0.1:6379[2]> select 0
OK
127.0.0.1:6379> keys *
1) "k1"
2) "k3"
127.0.0.1:6379> flushdb #清空当前数据库下的数据
OK
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> select 2
OK
127.0.0.1:6379[2]> keys * #flushdb二号数据库依然有数据
1) "k1"
2) "k2"
127.0.0.1:6379[2]> select 1
OK
127.0.0.1:6379> flushall #在零号数据库执行flushdb,二号数据库的数据也被清空了
OK
127.0.0.1:6379> select 2
OK
127.0.0.1:6379[2]> keys *
(empty list or set)
127.0.0.1:6379[2]>
键值对的存活时间
创建的时候设置键值对的有效期(单位秒):setex key second value
127.0.0.1:6379> setex key2 12 hello
OK
127.0.0.1:6379> get key2
"hello"
127.0.0.1:6379> get key2 #12s后在获取已经获取不到key2了
(nil)
给已有的key设置过期时间:expire key second
127.0.0.1:6379> set k v
OK
127.0.0.1:6379> expire k 5
(integer) 1
127.0.0.1:6379> get k
"v"
127.0.0.1:6379> get k #5s后已经获取不到k
(nil)
获取key的剩余有效时间:ttl key
127.0.0.1:6379> setex k 30 v
OK
127.0.0.1:6379> ttl k
(integer) 21
设置key在指定时间戳(unix)后失效:expireat key timestamp
数据类型相关命令
string操作
设置单个值:set key value
获取单个值:get key
127.0.0.1:6379> get key1
(nil)
127.0.0.1:6379> set key1 value1
OK
127.0.0.1:6379> get key1
"value1"
设置多个值:mset key1 value1 key2 value2 ...
获取多个值:mget key1 key2 ...
127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3
OK
127.0.0.1:6379> mget k1 k2
1) "v1"
2) "v2"
对key的整型value值+1:incr key
对key的整型value值+1:decr key
127.0.0.1:6379> set int 1
OK
127.0.0.1:6379> incr int #值加一
(integer) 2
127.0.0.1:6379> get int
"2"
127.0.0.1:6379> decr int #值减一
(integer) 1
127.0.0.1:6379> get int
"1"
对key的整型value值按指定步长递增:incrby key 步长
对key的整型value值按指定步长递减:decrby key 步长
127.0.0.1:6379> get int
"1"
127.0.0.1:6379> incrby int 5
(integer) 6
127.0.0.1:6379> get int
"6"
127.0.0.1:6379> decrby int 3
(integer) 3
127.0.0.1:6379> get int
"3"
获取key的长度:strlen key
127.0.0.1:6379> strlen int
(integer) 1
hash操作
Redis中的hash是一个键值对集合,key是string类型,value是由多个键值对(field:value)组成,适用于存储对象。
设置key的单个键值对:hset key field value
获取key的单个键值对:hget key field
127.0.0.1:6379> hset u1 name lena
(integer) 1
127.0.0.1:6379> hset u1 age 2
(integer) 1
127.0.0.1:6379> hset u1 class 187
(integer) 1
127.0.0.1:6379> hget u1 age
"2" #即使输入的是数字类型,也会以string类型存储
获取key所有键值对:hgetall key
127.0.0.1:6379> hgetall u1
1) "name"
2) "lena"
3) "age"
4) "2"
5) "class"
6) "187"
设置key的多个键值对:hmset key f1 v1 f2 v2 ...
获取key的多个键值对:hmget key f1 f2 ...
127.0.0.1:6379> hmset u2 name hello school zhku age 12
OK
127.0.0.1:6379> hmget u2 school age
1) "zhku"
2) "12"
统计key有多少个键值对:hlen key
127.0.0.1:6379> hlen u2
(integer) 3
判断key的某个字段是否存在:hexists key field
127.0.0.1:6379> hexists u1 school
(integer) 0
127.0.0.1:6379> hexists u2 school
(integer) 1
对key的某个字段值按步长递增:``hincrby key field 步长`
127.0.0.1:6379> hincrby u2 age 5
(integer) 17
127.0.0.1:6379> hget u2 age
"17"
删除key中某个字段:hdel key field
127.0.0.1:6379> hgetall u2
1) "name"
2) "hello"
3) "school"
4) "zhku"
5) "age"
6) "12"
127.0.0.1:6379> hdel u2 age
(integer) 1
127.0.0.1:6379> hgetall u2
1) "name"
2) "hello"
3) "school"
4) "zhku"
获取key中所有字段名称:hkeys key
获取key中所有字段值:hvals key
127.0.0.1:6379> hkeys u2
1) "name"
2) "school"
3) "age"
127.0.0.1:6379> hvals u2
1) "hello"
2) "zhku"
3) "17"
list操作
Redis中的list是一个有序、且元素值可以重复的双向链表。
向List的左边存入数据:lpush key value1 value2 value3
向List的左边取出数据:lpop key
127.0.0.1:6379> lpush stu lena cindy tity
(integer) 3
127.0.0.1:6379> lpop stu
"tity"
127.0.0.1:6379> lpop stu
"cindy"
向List的右边存入数据:rpush key value1 value2 value3
向List的右边取出数据:rpop key
127.0.0.1:6379> rpush city tity cindy
(integer) 2
127.0.0.1:6379> rpop city
"cindy"
127.0.0.1:6379> rpop city
"tity"
查询链表的值[start,end]:lrange key start end
若想查询全部元素,可以输入start=0,end=-1,即表示查询全部元素。索引从0开始。
127.0.0.1:6379> lpush stu lena cindy tity
(integer) 4
127.0.0.1:6379> lrange stu 1 3
1) "cindy"
2) "lena"
3) "lena"
127.0.0.1:6379> lrange stu 0 -1
1) "tity"
2) "cindy"
3) "lena"
4) "lena"
清空链表中的元素:del key
127.0.0.1:6379> lrange city 0 -1
1) "lena"
2) "lena"
3) "lena"
127.0.0.1:6379> del city
(integer) 1
127.0.0.1:6379> lrange city 0 -1
(empty list or set)
获取链表的长度:llen key
127.0.0.1:6379> lpush stu lena cindy tity
(integer) 3
127.0.0.1:6379> llen stu
(integer) 3
获取链表指定下标的元素:lindex key index
修改链表指定下标的元素:lset key index value
127.0.0.1:6379> lindex stu 2
"lena"
127.0.0.1:6379> lset stu 2 lena777
OK
127.0.0.1:6379> lindex stu 2
"lena777"
set操作
redis中的set是无序、不重复的。
向set添加元素:sadd key value1 value2 ...
取出指定set所有值:smembers key
127.0.0.1:6379> sadd k1 v1 v2 v4 v3
(integer) 4
127.0.0.1:6379> smembers k1
1) "v1"
2) "v2"
3) "v3"
4) "v4"
判断值是否是set的成员:sismember key value
127.0.0.1:6379> sismember k1 v
(integer) 0
127.0.0.1:6379> sismember k1 v4
(integer) 1
删除指定的值:srem key value
127.0.0.1:6379> smembers k1
1) "v1"
2) "v2"
3) "v3"
4) "v4"
127.0.0.1:6379> srem k1 v3
(integer) 1
127.0.0.1:6379> smembers k1
1) "v1"
2) "v2"
3) "v4"
判断set的长度:scard key
127.0.0.1:6379> scard k1
(integer) 3
获取两个set集合的差集:sdiff key1 key2
获取两个set集合的交集:sinter key1 key2
获取两个set集合的并集:sunion key1 key2
127.0.0.1:6379> smembers k1
1) "v4"
2) "v2"
3) "v1"
127.0.0.1:6379> smembers k2
1) "v4"
2) "v2"
3) "v5"
127.0.0.1:6379> sdiff k1 k2
1) "v1"
127.0.0.1:6379> sinter k1 k2
1) "v4"
2) "v2"
127.0.0.1:6379> sunion k1 k2
1) "v2"
2) "v4"
3) "v5"
4) "v1"
sorted set操作
redis中的sorted set即zset类型,是一个有序集合。
是在原来集合的基础上为集合的每个元素关联一个分数,根据分数进行排序,以此实现有序。
向集合中添加元素:zadd key score value
查看key中指定字段的分数:zscore key value
127.0.0.1:6379> zadd zs 100 lena
(integer) 1
127.0.0.1:6379> zscore zs lena
"100"
查看key某一范围的元素(按score正序排序):zrange key start stop
查看key某一范围的元素(按score倒序排序):zrevrange key start stop
127.0.0.1:6379> zrange zs 0 -1 #按正序查看所有元素
1) "tity"
2) "jack"
3) "cinty"
4) "lena"
127.0.0.1:6379> zrange zs 1 3 #正序查看[1,3]的元素
1) "jack"
2) "cinty"
3) "lena"
127.0.0.1:6379> zrevrange zs 0 -1 #按倒序查看所有元素
1) "lena"
2) "cinty"
3) "jack"
4) "tity"
127.0.0.1:6379> zrevrange zs 1 3 #倒序查看[1,3]的元素
1) "cinty"
2) "jack"
3) "tity"
获取key下元素的数量:zcard key
127.0.0.1:6379> zcard zs
(integer) 4
将key下指定元素的分数增加步长值:zincrby key increse_score value
127.0.0.1:6379> zincrby zs 10 cinty
"109" #返回该字段增加后的值
删除key下指定元素:zrem key value1 value2 ...
127.0.0.1:6379> zrange zs 0 -1
1) "tity"
2) "jack"
3) "lena"
4) "cinty"
127.0.0.1:6379> zrem zs jack cinty
(integer) 2
127.0.0.1:6379> zrange zs 0 -1
1) "tity"
2) "lena"
使用go语言操作redis可参考一下文章:
[1] redigo操作redis:https://blog.csdn.net/lena7/article/details/120345051
[2] go-redis操作redis:https://blog.csdn.net/lena7/article/details/120828397