Redis入门

一、Redis的安装

Redis的官方文档介绍了多种安装方式(包括Linux、Windows、MacOs平台上的安装和从源码包安装):Redis安装。这里只介绍源码安装方式。

  • 下载源码包

    shell 复制代码
    $ wget https://download.redis.io/redis-stable.tar.gz
  • 编译Redis

    shell 复制代码
    $ tar -xzvf redis-stable.tar.gz
    $ cd redis-stable
    $ ./configure prefix=/usr/local/bin/redis   # 指定安装目录为/usr/local/bin/redis
    $ make distclean && make
    $ make test

    如果编译成功,将会在src目录下生成两个二进制文件

    • redis-server: Redis Server启动程序
    • redis-cli:Redis 客户端连接工具
  • 安装Redis到**/usr/local/bin/redis**(默认安装到/usr/local)

    bash 复制代码
    $ make install

    安装成功后将会在**/usr/local/bin/redis**下有一个bin文件夹:

  • 修改Redis启动方式为后台启动

    将源码包中的redis.conf复制到安装路径/usr/local/bin/redis/

    shell 复制代码
    $ cp /usr/local/src/redis-stable/redis.conf /usr/local/bin/redis/

    修改daemonize为yes

  • 启动redis

    shell 复制代码
    $ ./bin/redis-server ./redis.conf
  • 配置Redis其他机器可以连接本机Redis服务

    shell 复制代码
    $ vim ./redis.conf
    conf 复制代码
    protected-mode no
    bind 0.0.0.0
  • 停止Redis服务

    shell 复制代码
    $ redis-cli shutdown

二、Redis支持的数据类型

  • 字符串(String)
  • 列表(list)
  • 有序列表(sorted set)
  • 哈希(hash)
  • 集合(set)
1. 字符串(String)

默认设置下String的值最大不能超过512M。

基础使用

shell 复制代码
> set bike:1 Deimos
> get bike:1

同时设置和获取多个值

shell 复制代码
> mset bike:1 "Deimos" bike:2 "Ares" bike:3 "Vanth"
> mget bike:1 bike:2 bike:3

字符串计数

shell 复制代码
> set total_crashes 0
> incr total_crashes
(integer) 1
> incrby total_crashes 10
(integer) 11
2.哈希(hash)
shell 复制代码
> hset bike:1 model Deimos brand Ergonom type 'Enduro bikes' price 4972
(integer) 4
> hget bike:1 model
"Deimos"
> hget bike:1 price
"4972"
> hgetall bike:1
1) "model"
2) "Deimos"
3) "brand"
4) "Ergonom"
5) "type"
6) "Enduro bikes"
7) "price"
8) "4972"

hmget

shell 复制代码
> hmget bike:1 model price no-such-field
1) "Deimos"
2) "4972"
3) (nil)

hincryby

plaintext 复制代码
> hincrby bike:1 price 100
(integer) 5072
> hincrby bike:1 price -100
(integer) 4972
3.列表(list)

Redis list是一个链表结构

LPUSH 从链表头部添加元素,RPUSH从链表尾部添加元素

LPOP 从链表头部弹出元素,RPOP从链表尾部弹出元素

先进先出

shell 复制代码
> LPUSH work:queue:ids 101
(integer) 1
> LPUSH work:queue:ids 237
(integer) 2
> RPOP work:queue:ids
"101"
> RPOP work:queue:ids
"237"

后进先出

shell 复制代码
> LPUSH work:queue:ids 101
(integer) 1
> LPUSH work:queue:ids 237
(integer) 2
> LPOP work:queue:ids
"237"
> LPOP work:queue:ids
"101"

LLEN检查链表长度

shell 复制代码
> LLEN work:queue:ids
(integer) 0

LTRIM只保留指定区间的元素,其他的元素将会被删除

shell 复制代码
> LPUSH notifications:user:1 "You've got mail!"
(integer) 1
> LTRIM notifications:user:1 0 99
OK
> LPUSH notifications:user:1 "Your package will be delivered at 12:01 today."
(integer) 2
> LTRIM notifications:user:1 0 99
OK

LMOVE将一个链表的头部(尾部)元素删除并添加到另外一个链表的头部(或尾部)

LMOVE source destination LEFT|RIGHT LEFT|RIGHT中第一个LEFT|RIGHT指定从头部还是尾部删除,第二个LEFT|RIGHT指定从尾部还是头部添加。

shell 复制代码
> LPUSH board:todo:ids 101
(integer) 1
> LPUSH board:todo:ids 273
(integer) 2
> LMOVE board:todo:ids board:in-progress:ids LEFT LEFT
"273"
> LRANGE board:todo:ids 0 -1
1) "101"
> LRANGE board:in-progress:ids 0 -1
1) "273"
4.集合(Set)

Set中不允许出现重复的元素

SADD添加元素

shell 复制代码
> SADD user:123:favorites 347
(integer) 1
> SADD user:123:favorites 561
(integer) 1
> SADD user:123:favorites 742
(integer) 1
> SADD user:456:favorites 561
(integer) 1

SISMEMBER判断Set是否有指定元素

shell 复制代码
> SISMEMBER user:123:favorites 742
(integer) 1
> SISMEMBER user:123:favorites 299
(integer) 0

SINTER返回两个或多个Set中共同存在的元素(交集)

shell 复制代码
> SINTER user:123:favorites user:456:favorites
1) "561"

SCARD返回Set的元素个数

shell 复制代码
> SCARD user:123:favorites
(integer) 3

SMEMBERS查看Set的成员列表

shell 复制代码
> sadd myset 1 2 3
(integer) 3
> smembers myset
1. 3
2. 1
3. 2

SREM从Set中删除指定元素

sehll 复制代码
> srem myset 1

SUNIONSTORE计算两个集合的并集并存储到另外一个集合中,如果另外一个集合存在则覆盖

shell 复制代码
> sunionstore game:1:deck deck
5. 有序集合(Sorted Set)

ZADD添加元素

sehll 复制代码
> zadd mysort 70 a 80 b 90 c
(integer) 3

ZSCORE获取指定成员的分数

sehll 复制代码
> zscore mysort a
"70"

ZCARD成员列表个数

sehll 复制代码
> zcard mysort
(integer) 3

ZRANGE指定范围的成员列表(按分数从小到大排序)

shell 复制代码
> zrange mysort 0 -1
1) "a"
2) "b"
3) "c"

ZREVRANGE指定范围的成员列表(按分数从大到小排序)

sehll 复制代码
> zrevrange mysort 0 -1
1) "c"
2) "b"
3) "a"

ZINCRBY增加指定成员的分数

sehll 复制代码
> zincrby mysort 10 10
"80"

三、key通用操作

1.keys

查找所有符合给定模式 pattern的 key,若 key 存在返回 1 ,否则返回 0 。

shell 复制代码
> keys *
2.exsists

检查给定 key 是否存在

sehll 复制代码
> exists mykey
(integer) 0
3.get

获取指定 key 的值。如果 key 不存在,返回 nil 。如果key 储存的值不是字符串类型,返回一个错误。

shell 复制代码
> get mykey
4. rename

修改 key 的名字为 newkey 。若key 不存在返回错误。

sehll 复制代码
> RENAME mykey 1
"OK"
5. expire

设置 key 的过期时间,key 过期后将不再可用。单位以秒计。

sehll 复制代码
> expire mykey 60
6. del

删除已存在的键。不存在的 key 会被忽略。

sehll 复制代码
> DEL mykey

四、事务

Redis事务中的所有命令将会串行化执行,并且执行事务中的命令是Redis不会再执行其他的任何命令。

multi 开启事务,multi后面执行的命令将会在一个事务中执行,直到执行exec或者discard(exec提交事务,discard回滚事务)。

shell 复制代码
> multi
> ... # 执行多个命令
> exec

五、Redis持久化

1. RDB持久化

默认支持的持久化方式,是把当前内存中的数据集快照写入磁盘,也就是 Snapshot 快照(数据库中所有键值对数据)。恢复时是将快照文件直接读到内存里。

备份时间配置

properties 复制代码
# Save the DB to disk.
#
# save <seconds> <changes> [<seconds> <changes> ...]

# Snapshotting can be completely disabled with a single empty string argument
# as in following example:
# 禁用rdb持久化
# save ""
#
#
# Unless specified otherwise, by default Redis will save the DB:
#   * After 3600 seconds (an hour) if at least 1 change was performed
#   * After 300 seconds (5 minutes) if at least 100 changes were performed
#   * After 60 seconds if at least 10000 changes were performed
# 启用rdb持久化  3600秒后,如果至少有一个命令执行,则备份;300秒后,如果至少有100个命令执行则备份;60秒后,如果至少有10000个命令执行则备份
save 3600 1 300 100 60 10000

备份文件配置

properties 复制代码
# The filename where to dump the DB  备份文件名
dbfilename dump.rdb

# Remove RDB files used by replication in instances without persistence
# enabled. By default this option is disabled, however there are environments
# where for regulations or other security concerns, RDB files persisted on
# disk by masters in order to feed replicas, or stored on disk by replicas
# in order to load them for the initial synchronization, should be deleted
# ASAP. Note that this option ONLY WORKS in instances that have both AOF
# and RDB persistence disabled, otherwise is completely ignored.
#
# An alternative (and sometimes better) way to obtain the same effect is
# to use diskless replication on both master and replicas instances. However
# in the case of replicas, diskless is not always an option.
rdb-del-sync-files no

# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.  备份文件dump.rdb存储位置
dir ./
2. AOF持久化

默认不启用,需要手动开启,通过保存Redis服务器所执行的写命令来记录数据库状态。

开启aof持久化

properties 复制代码
appendonly yes 
appendfilename "appendonly.aof"
appenddirname "appendonlydir"
# appendfsync always 每次修改都记录
# 每秒记录
appendfsync everysec  
# appendfsync no 不记录
相关推荐
BergerLee5 小时前
对不经常变动的数据集合添加Redis缓存
数据库·redis·缓存
Dylanioucn5 小时前
【分布式微服务云原生】掌握分布式缓存:Redis与Memcached的深入解析与实战指南
分布式·缓存·云原生
huapiaoy5 小时前
Redis中数据类型的使用(hash和list)
redis·算法·哈希算法
【D'accumulation】6 小时前
令牌主动失效机制范例(利用redis)注释分析
java·spring boot·redis·后端
Cikiss7 小时前
微服务实战——SpringCache 整合 Redis
java·redis·后端·微服务
一休哥助手8 小时前
Redis 五种数据类型及底层数据结构详解
数据结构·数据库·redis
盒马盒马8 小时前
Redis:zset类型
数据库·redis
Jay_fearless10 小时前
Redis SpringBoot项目学习
spring boot·redis
Wang's Blog10 小时前
Redis: 集群环境搭建,集群状态检查,分析主从日志,查看集群信息
数据库·redis
wclass-zhengge16 小时前
Redis篇(最佳实践)(持续更新迭代)
redis·缓存·bootstrap