Redis快速入门——你确定不来瞅瞅?

Redis介绍

1、Redis是Key-Value型NoSQL数据库
2、Redis将数据存储在内存中,同时也能持久化到磁盘
3、Redis常用于缓存,利用内存的高效提高程序的处理速度

Redis特点

1、速度快
2、持久化
3、主从复制
4、广泛的语言支持
5、多种数据结构
6、分布式与高可用

Redis的常用基本配置

配置项 示例 说明
daemonize daemonize yes 是否启用后台运行,默认no
port port 6379 设置端口号,默认6379
logfile logfile日志文件 设置日志文件
databases databases 255 设置redis数据库总量
dir dir 数据文件目录 设置数据文件存储目录
requirepass requirepass 12345 设置使用密码

Redis通用命令

命令 示例 说明
select select 0 选择0号数据库
set set name lily 设置key=name,value=lily
get get hello 获得key=hello结果
keys keys he* 根据Pattern表达式查询符合条件的Key
dbsize dbsize 返回key的总数
exists exists a 检查key=a是否存在
del del a 删除key=a的数据
expire expire hello 20 设置key=hello 20秒后过期
ttl ttl hello 查看key=a的过期剩余时间

Redis数据类型

String - 字符串类型
List - 列表类型
Zset - 有序集合类型
Hash - Hash类型
Set - 集合类型

String 字符串类型

name Lily
counter 3321
sn 7361-7749

String最大512mb
建议单个kv不超过100kb

字符串命令

命令 示例 说明
get get hello 获得key=hello结果
set set hello world 设置key=hello,value=hello
mset / mset mset hello world java best / mget hello java 一次性设置或者获取多个值
del del hello 删除key=hello
incr / decr incr count / decr count key值自增/自减1
incrby / decrby incrby count 99 / decrby count 99 自增自减指定步长

Hash键值类型

Hash类型用于存储结构化数据
emp:1

name smith
age 21
birthday 1999-10-24
height 180

Hash命令

hget hget emp:1 age 获取hash中key=age的
hset hset emp:1 age 23 设置hash中age=23
hmset / hmget / hgetall hmset emp:1 age 30 name kaka / hmget emp:1 age name / hgetall emp:1 设置hash多个值/获取hash多个值/获取hash所有值
hdel hdel emp:1 age 删除user:1的age
hexists hexists emp:1 name 检查是否存在
hlen hlen emp:1 获取指定长度

List列表类型

List列表就是一系列字符串的“数组”,按插入顺序排序
List列表最大长度为2的32次方-1,可以包含40亿个元素

List命令

rpush listkey c b a -右侧插入
lpush listkey f e d -左侧插入
rpop listkey -右侧弹出
lpop listkey -左侧弹出

List指令

llen listkey -获取长度
lrange listkey 0 2 Irange
listkey 1 -1获取子集

Set与Zset集合类型

Set集合是字符串的无序集合,集合成员是唯一的
Zset集合是字符串的有序集合,集合成员是唯一的

内容制作不易,记得点赞收藏哦~

猜你喜欢

转载自blog.csdn.net/Turniper/article/details/109128991