Redis data structure
String String
list of strings
String collection set
sorted set of strings
hash hash
Common commands
Settings
set [key] [value]
Get first and then set
getset
[key] [value]
get value
get [key]
delete value
del [key]
increase or decrease
incr [key] Increase the value corresponding to the specified key by 1. If it does not exist, it will be created and equal to the default initial value, plus 1
If it is a value that cannot be converted to an int type, an error will be reported
decr [key] is the opposite of the incr command
incrby [key] 5 Add 5 to the original value, specify how much to increase
The same is true for desrby [key] 5, which has the opposite effect
concatenate strings
append [key] str Adds an additional string after the original value
data structure hash
Hash is a Map structure that stores String Key and String value
Settings
hset myhash username jack
haet myhash age 18
set multiple values
hmset myhash2 username rose age 18
Obtain
hget myhash username
hmget myhash username age
hgetall myhash
删除
hdel myhash2 username age 删除集合里的键值对
del myhash2 删除整个集合
增加数字
hincrby myhash age 5
判断是否存在
hexists myhash username
获取属性数量
hlen myhash
获取所有属性
hkeys myhash
获取所有值
hvals myhash
数据结构之list
存储list:
ArrayList使用数组方式
LinkedList使用双向链表方式
双向链表添加数据 删除数据
两端添加 左侧添加 lpush mylist a b c 右侧添加 rpush mylist2 a b c
查看列表 lrange mylist 0 5
两端弹出 lpop mylist
获取列表元素个数 llen mylist
数据结构之set
存储Set:
Set集合中不允许出现重复的元素
添加删除元素 sadd myset a b c
srem myset a b
获取集合的元素 smembers myset
集合的差集运算 sdiff myset1 myset2
集合的交集运算 sinter myset1 myset2
集合的并集运算 sunion myset1 myset2
Redis的持久化
两种持久化方式
RDB方式 默认支持,不需要配置 在指定的时间间隔内,将内存中的数据写进磁盘
AOF方式 以日志的方式,记录服务器的每一条操作 更高的数据安全性 文件要大一些 运行效率要低一点
无持久化
同时使用 PDB和AOF
Redis多数据库
客户端可以指定链接那个数据库 一个Redis实例最多连接16个数据库 0-15
选择1号数据库
select 1
将一个数据库里面的key移动到另一个数据库
move myset 1, 将当前库里的myset移动到 1 号数据库
Redis事物
multi exec discard
事物中的所有命令都会串行化顺序执行,命令执行失败,后面的命令还是会执行
multi 开启一个事物,后面的命令都会在事物里面
exec 提交事物
discad 相当于关系型数据库的rollback,提交之前废弃之前的命令