Redis入门

一、Redis的安装

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

  • 下载源码包

    $ wget https://download.redis.io/redis-stable.tar.gz
    
  • 编译Redis

    $ 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)

    $ make install
    

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

在这里插入图片描述

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

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

    $ cp /usr/local/src/redis-stable/redis.conf /usr/local/bin/redis/
    

    修改daemonize为yes

    在这里插入图片描述

  • 启动redis

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

    $ vim ./redis.conf
    
    protected-mode no
    bind 0.0.0.0
    
  • 停止Redis服务

    $ redis-cli shutdown
    
二、Redis支持的数据类型
  • 字符串(String)
  • 列表(list)
  • 有序列表(sorted set)
  • 哈希(hash)
  • 集合(set)
1. 字符串(String)

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

基础使用

> set bike:1 Deimos
> get bike:1

同时设置和获取多个值

> mset bike:1 "Deimos" bike:2 "Ares" bike:3 "Vanth"
> mget bike:1 bike:2 bike:3

字符串计数

> set total_crashes 0
> incr total_crashes
(integer) 1
> incrby total_crashes 10
(integer) 11
2.哈希(hash)
> 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

> hmget bike:1 model price no-such-field
1) "Deimos"
2) "4972"
3) (nil)

hincryby

> hincrby bike:1 price 100
(integer) 5072
> hincrby bike:1 price -100
(integer) 4972
3.列表(list)

Redis list是一个链表结构

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

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

先进先出

> LPUSH work:queue:ids 101
(integer) 1
> LPUSH work:queue:ids 237
(integer) 2
> RPOP work:queue:ids
"101"
> RPOP work:queue:ids
"237"

后进先出

> 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检查链表长度

> LLEN work:queue:ids
(integer) 0

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

> 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指定从尾部还是头部添加。

> 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添加元素

> 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是否有指定元素

> SISMEMBER user:123:favorites 742
(integer) 1
> SISMEMBER user:123:favorites 299
(integer) 0

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

> SINTER user:123:favorites user:456:favorites
1) "561"

SCARD返回Set的元素个数

> SCARD user:123:favorites
(integer) 3

SMEMBERS查看Set的成员列表

> sadd myset 1 2 3
(integer) 3
> smembers myset
1. 3
2. 1
3. 2

SREM从Set中删除指定元素

> srem myset 1

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

> sunionstore game:1:deck deck
5. 有序集合(Sorted Set)

ZADD添加元素

> zadd mysort 70 a 80 b 90 c
(integer) 3

ZSCORE获取指定成员的分数

> zscore mysort a
"70"

ZCARD成员列表个数

> zcard mysort
(integer) 3

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

> zrange mysort 0 -1
1) "a"
2) "b"
3) "c"

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

> zrevrange mysort 0 -1
1) "c"
2) "b"
3) "a"

ZINCRBY增加指定成员的分数

> zincrby mysort 10 10
"80"
三、key通用操作
1.keys

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

> keys *
2.exsists

检查给定 key 是否存在

> exists mykey
(integer) 0
3.get

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

> get mykey
4. rename

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

> RENAME mykey 1
"OK"
5. expire

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

> expire mykey 60
6. del

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

> DEL mykey
四、事务

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

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

> multi
> ... # 执行多个命令
> exec
五、Redis持久化
1. RDB持久化

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

备份时间配置

# 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

备份文件配置

# 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持久化

appendonly yes 
appendfilename "appendonly.aof"
appenddirname "appendonlydir"
# appendfsync always 每次修改都记录
# 每秒记录
appendfsync everysec  
# appendfsync no 不记录
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值