redis数据库

luckyFang 2021年07月21日 13次浏览

Redis

常见操作

安装Redis

brew install redis

连接redis

redis-cli -p 6379

测试连接

127.0.0.1:6379> ping
PONG

设置值

# set aaa(key) bbb(value)
127.0.0.1:6379> set name lu
OK

获取值

127.0.0.1:6379> get name
"lu"

查看所有值

keys * 

清空数据

FLUSHDB # 清空当前数据库
FLUSHALL # 清空所有数据库

判断是否存在

# EXISTS name
127.0.0.1:6379> EXISTS name 
(integer) 1 # 1 存在 

127.0.0.1:6379> EXISTS addr
(integer) 0 # 0 不存在

删除当前的key

127.0.0.1:6379> move name 1
(integer) 1
127.0.0.1:6379> keys *
(empty array)

设置失效时间

## 设置失效时间 单位 秒
127.0.0.1:6379> set token abcdef
OK
127.0.0.1:6379> EXPIRE token 10
(integer) 1

## ------查询失效时间--------
## time to live
127.0.0.1:6379> ttl token
(integer) 5
127.0.0.1:6379> ttl token
(integer) 3
127.0.0.1:6379> ttl token
(integer) 0
## 已失效
127.0.0.1:6379> ttl token
(integer) -2

查看key类型

127.0.0.1:6379> set addr 127.0.0.1
OK
127.0.0.1:6379> type addr
string

截取部分

# 通过 get-range 来获取部分(subString)
127.0.0.1:6379> set str Hello,World!
OK
127.0.0.1:6379> GETRANGE str 0 4
"Hello"
127.0.0.1:6379> GETRANGE str -6 -1
"World!"
127.0.0.1:6379> 

替换部分

# 从指定位置开始替换
127.0.0.1:6379> SETRANGE str 6 Redis
(integer) 12
127.0.0.1:6379> get str
"Hello,Redis!"

五大数据类型

String

常用命令

# 设置值
127.0.0.1:6379> set key hello
OK


# 获取值
127.0.0.1:6379> get key
"hello"

# 查看所有值
127.0.0.1:6379> keys * 
1) "addr"
2) "key"

# 是否存在
127.0.0.1:6379> EXISTS key
(integer) 1

# 追加文本
127.0.0.1:6379> APPEND key ,world
(integer) 11


127.0.0.1:6379> get key
"hello,world"

# key长度
127.0.0.1:6379> STRLEN key
(integer) 11

127.0.0.1:6379> APPEND key !
(integer) 12

127.0.0.1:6379> STRLEN key
(integer) 12

127.0.0.1:6379> get key
"hello,world!"
127.0.0.1:6379> 

increase自增

127.0.0.1:6379> set views 0
OK
## 每次+1
127.0.0.1:6379> incr views
(integer) 1
127.0.0.1:6379> incr views
(integer) 2
127.0.0.1:6379> get views
"2"

decrease自减

127.0.0.1:6379> get views
"2"
## 每次-1
127.0.0.1:6379> decr views
(integer) 1
127.0.0.1:6379> get views
"1"

自定义增长

127.0.0.1:6379> incrby views 10
(integer) 11
127.0.0.1:6379> get views
"11"

自定义减少

127.0.0.1:6379> decrby views 5
(integer) 6
127.0.0.1:6379> get views
"6"

setex(set with expire)

# 设置 token 值为 abc 10秒后过期
127.0.0.1:6379> setex token 10 abc
OK
127.0.0.1:6379> ttl token
(integer) 2

setnx(set if not expire)

# 如果key不存在则创建
127.0.0.1:6379> setnx key redis
(integer) 1
127.0.0.1:6379> keys *
1) "key"
127.0.0.1:6379> setnx key mysql
(integer) 0
127.0.0.1:6379> get key
"redis"
# 如果key存在则创建失败
127.0.0.1:6379> 

mset

# mset 批量设置
127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3
OK
127.0.0.1:6379> keys *
1) "k3"
2) "k1"
3) "k2"
127.0.0.1:6379> get k2
"v2"

msetnx

原子操作,要么一起成功,要么一起失败!

# 不存在则创建 存在则失败
127.0.0.1:6379> msetnx k1 v1 k4 v4
(integer) 0
127.0.0.1:6379> get k4
(nil)
127.0.0.1:6379> 

mget

# mget 批量获取
127.0.0.1:6379> mget k1 k2 k3
1) "v1"
2) "v2"
3) "v3"
127.0.0.1:6379> mget k1 k2
1) "v1"
2) "v2"
127.0.0.1:6379> 

对象操作

# 直接存入JSON
127.0.0.1:6379> set user:1 {name:zhangsan,age:21}
OK
127.0.0.1:6379> get user:1
"{name:zhangsan,age:21}"

# 通过key模拟对象存储
# user:{id}:{field} value
127.0.0.1:6379> mset user:1:name zhangsan user:1:age 2
OK
127.0.0.1:6379> mget user:1:name user:1:age
1) "zhangsan"
2) "2"

getset

先获取再赋值,存在返回,不存在返回nil

127.0.0.1:6379> getset db redis
(nil)
127.0.0.1:6379> get db
"redis"
127.0.0.1:6379> 



127.0.0.1:6379>  getset db mysql
"redis"
127.0.0.1:6379> get db
"mysql"

List

基本的数据类型:列表

  • 队列
  • 阻塞阻塞队列

所有命令都是l开头的

lrange(获取数据)

127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"

lpush(头部插入)

127.0.0.1:6379> LPUSH list one
(integer) 1
127.0.0.1:6379> LPUSH list two
(integer) 2
127.0.0.1:6379> LPUSH list three
(integer) 3
127.0.0.1:6379> 
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> LRANGE list 0 1
1) "three"
2) "two"

rpush(尾部插入)

127.0.0.1:6379> RPUSH list end
(integer) 4
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
4) "end"
127.0.0.1:6379> 

lpop(左边移除)

移除第一个元素

127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
4) "end"
127.0.0.1:6379> 
127.0.0.1:6379> LPOP list
"three"
127.0.0.1:6379> LRANGE list 0 -1
1) "two"
2) "one"
3) "end"

rpop(右边移除)

移除最后一个元素

127.0.0.1:6379> RPOP list
"end"
127.0.0.1:6379> LRANGE list 0 -1
1) "two"
2) "one"
127.0.0.1:6379> 

lindex(获取指定下标元素)

127.0.0.1:6379> LINDEX list 0
"two"
127.0.0.1:6379> LINDEX list 1
"one"
127.0.0.1:6379> 

llen(获取list长度)

127.0.0.1:6379> LLEN list
(integer) 2

lrem(从list中移除元素)

127.0.0.1:6379> LPUSH list one
(integer) 1
127.0.0.1:6379> LPUSH list two
(integer) 2
127.0.0.1:6379> LPUSH list three
(integer) 3
127.0.0.1:6379> LPUSH list three
(integer) 4
127.0.0.1:6379> 
127.0.0.1:6379> 
# 移除指定元素 个数
127.0.0.1:6379> LREM list 2 three
(integer) 2
127.0.0.1:6379> LRANGE list 0 -1
1) "two"
2) "one"
127.0.0.1:6379> 

ltrim(截取部分)

通过下标截取指定长度,左闭右闭

注意:此操作会改变list本身

127.0.0.1:6379> RPUSH list hello
(integer) 1
127.0.0.1:6379> RPUSH list hell1
(integer) 2
127.0.0.1:6379> RPUSH list hell2
(integer) 3
127.0.0.1:6379> RPUSH list hell3
(integer) 4
127.0.0.1:6379> 
127.0.0.1:6379> LRANGE list 0 -1
1) "hello"
2) "hell1"
3) "hell2"
4) "hell3"
# 截取第1个元素到第2个元素
# 初始下标为 0
127.0.0.1:6379> LTRIM list 1 2
OK
127.0.0.1:6379> LRANGE list 0 -1
1) "hell1"
2) "hell2"
127.0.0.1:6379> 

rpoplpush

最后一个元素弹出并加入到新的列表头部

127.0.0.1:6379> rpush list1 1
(integer) 1
127.0.0.1:6379> rpush list1 2
(integer) 2
127.0.0.1:6379> rpush list1 3
(integer) 3
127.0.0.1:6379> 
127.0.0.1:6379> 


# 最后一个元素 3 弹出 加入了 list2 的头部
127.0.0.1:6379> RPOPLPUSH list1 list2
"3"


127.0.0.1:6379> LRANGE list1 0 -1
1) "1"
2) "2"
127.0.0.1:6379> LRANGE list2 0 -1
1) "3"
127.0.0.1:6379> 

lset(设置指定元素)

设置指定下标元素,不存在则报错。

127.0.0.1:6379> LPUSH list one
(integer) 1
127.0.0.1:6379> lset list 0 two
OK
127.0.0.1:6379> LRANGE list 0 -1
1) "two"
127.0.0.1:6379> 

127.0.0.1:6379> lset lsit 1 3
(error) ERR no such key
127.0.0.1:6379> 

linsert(插入值)

127.0.0.1:6379> RPUSH list Hello
(integer) 1
127.0.0.1:6379> RPUSH list World!
(integer) 2
127.0.0.1:6379> LRANGE list 0 -1
1) "Hello"
2) "World!"
127.0.0.1:6379> LINSERT list before World! ,
(integer) 3
127.0.0.1:6379> LRANGE list 0 -1
1) "Hello"
2) ","
3) "World!"
127.0.0.1:6379> 

  • list本质上是一个链表
  • 如果key不存在则创建新的列表
  • 如果key存在,则新增内容
  • 空链表代表不存在
  • 只有pushpop操作才分 L R,其余 l均代表 list的意思

Set

set中的值不可重复

sadd(添加元素)

127.0.0.1:6379> sadd set jack
(integer) 1
127.0.0.1:6379> sadd set tom
(integer) 1
127.0.0.1:6379> sadd set eric
(integer) 1

smembers(查看全部元素)

127.0.0.1:6379> SMEMBERS set
1) "jack"
2) "eric"
3) "tom"

sismemeber(是否包含该元素)

127.0.0.1:6379> SISMEMBER set hello
(integer) 0
127.0.0.1:6379> SISMEMBER set tom
(integer) 1

scard(获取元素长度)

127.0.0.1:6379> scard set
(integer) 3

srem(移除元素)

127.0.0.1:6379> srem set jack
(integer) 1
127.0.0.1:6379> SMEMBERS set
1) "eric"
2) "tom"

srandmemeber(随机抽选)

127.0.0.1:6379> SRANDMEMBER set 1
1) "tom"
127.0.0.1:6379> SRANDMEMBER set 1
1) "tom"
127.0.0.1:6379> SRANDMEMBER set 1
1) "eric"

spop(随机删除)

127.0.0.1:6379> SPOP set 
"eric"

sdiff(差集)

用于找出两个集合的差集

127.0.0.1:6379> sadd s1 a
(integer) 1
127.0.0.1:6379> sadd s1 b
(integer) 1
127.0.0.1:6379> sadd s1 c
(integer) 1
127.0.0.1:6379> sadd s1 d
(integer) 1
127.0.0.1:6379> sadd s2 a
(integer) 1
127.0.0.1:6379> sadd s2 b
(integer) 1
127.0.0.1:6379> SDIFF s1 s2
1) "d"
2) "c"
127.0.0.1:6379> 

sinter(交集)

用于找出两个集合相交部分(共同好友)

127.0.0.1:6379> SINTER s1 s2
1) "b"
2) "a"
127.0.0.1:6379> 

sunion(并集)

两个集合合并

127.0.0.1:6379> SUNION s1 s2
1) "a"
2) "b"
3) "c"
4) "d"
  • 可用于推荐系统
  • 六度分隔

Hash

key-map

适合于存储对象

hset(设置字段值)

127.0.0.1:6379> hset hash name luzhenfang
(integer) 1
127.0.0.1:6379> hset hash age 22
(integer) 1

hget(获取字段值)

127.0.0.1:6379> hget hash name
"luzhenfang"
127.0.0.1:6379> hget hash age
"22"

hmset(设置多个值)

127.0.0.1:6379> hmset hash addr linfen sex m
OK

hmget(获取多个值)

127.0.0.1:6379> hmget hash name age addr
1) "luzhenfang"
2) "22"
3) "linfen"
127.0.0.1:6379> 

hgetall(获取全部元素)

127.0.0.1:6379> HGETALL hash
1) "name"
2) "luzhenfang"
3) "age"
4) "22"
5) "addr"
6) "linfen"
7) "sex"
8) "m"

hdel(删除字段)

127.0.0.1:6379> hdel hash sex
(integer) 1
127.0.0.1:6379> hgetall hash
1) "name"
2) "luzhenfang"
3) "age"
4) "22"
5) "addr"
6) "linfen"
127.0.0.1:6379> 

hlen(获取字段数量)

127.0.0.1:6379> hlen hash
(integer) 3
127.0.0.1:6379> 

hexists(字段是否存在)

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

hkeys(所有key)

127.0.0.1:6379> hkeys hash
1) "name"
2) "age"
3) "addr"
127.0.0.1:6379> 

hvals(所有value)

127.0.0.1:6379> hvals hash
1) "luzhenfang"
2) "22"
3) "linfen"
127.0.0.1:6379> 

hincrby(自增/自减)

127.0.0.1:6379> hset hash views 1
(integer) 0
# 自增
127.0.0.1:6379> HINCRBY hash views 10
(integer) 11
# 自减
127.0.0.1:6379> HINCRBY hash views -1
(integer) 10

hsetnx

不存在可以创建,存在则创建失败。

127.0.0.1:6379> hsetnx hash name lu
(integer) 0
127.0.0.1:6379> hsetnx hash height 175
(integer) 1
127.0.0.1:6379> 

Zset