redis基础 --- #1

这是 DataWhale 新闻推荐系统实战的 Task2,主要任务有:

  • mysql 基础
  • MongoDB 基础
  • redis 基础

本篇博客主要是关于 redis 基础,Fun-rec的文档在这里


Redis(Remote Dictionary Server ),即远程字典服务,是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库。由于是内存数据库,读写非常高速,可达10w/s的评率,所以一般应用于数据变化快、实时通讯、缓存等。但内存数据库通常要考虑机器的内存大小。Redis 是完全开源免费的,遵守 BSD 协议,是一个灵活的高性能 key-value 数据结构存储,可以用来作为数据库、缓存和消息队列。相比于其他的 key-value 缓存产品有以下三个特点:

  • Redis 支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载到内存使用。
  • Redis 不仅支持简单的 key-value 类型的数据,同时还提供 list,set,zset,hash 等数据结构的存储。
  • Redis 支持主从复制,即 master-slave 模式的数据备份。

这次没有找到合适的关于 Redis 的架构的介绍,等找到了再补吧。

1. 安装

Ubuntu 上安装 Redis 很简单:sudo apt-get install redis-server。Windows 上的安装参考官方教程
在 Ubuntu 上安装完后,默认是自动启动 Redis 服务的,可以检查一下是否已经启动了 Redis 服务:

接下来就可以进入 Redis 客户端与服务器交互了:

Redis 是有很多参数可以配置的,在终端敲下 redis-server --help 看看:
从上图我们可以看出来,要启动 Redis 服务器,可以将配置写进配置文件(默认为在 /etc/redis/redis.conf),也可以直接传参数,还有哨兵模式(sentinel mode)的启动方式。

连接

看完 Redis 服务器的启动方式,再来看看客户端,同样,敲下redis-cli --help

Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
  -h <hostname>      Server hostname (default: 127.0.0.1).
  -p <port>          Server port (default: 6379).
  -s <socket>        Server socket (overrides hostname and port).
  -a <password>      Password to use when connecting to the server.
  -u <uri>           Server URI.
  -r <repeat>        Execute specified command N times.
  -i <interval>      When -r is used, waits <interval> seconds per command.
                     It is possible to specify sub-second times like -i 0.1.
  -n <db>            Database number.
  -x                 Read last argument from STDIN.
  -d <delimiter>     Multi-bulk delimiter in for raw formatting (default: \n).
  -c                 Enable cluster mode (follow -ASK and -MOVED redirections).
  --raw              Use raw formatting for replies (default when STDOUT is
                     not a tty).
  --no-raw           Force formatted output even when STDOUT is not a tty.
  --csv              Output in CSV format.
  --stat             Print rolling stats about server: mem, clients, ...
  --latency          Enter a special mode continuously sampling latency.
                     If you use this mode in an interactive session it runs
                     forever displaying real-time stats. Otherwise if --raw or
                     --csv is specified, or if you redirect the output to a non
                     TTY, it samples the latency for 1 second (you can use
                     -i to change the interval), then produces a single output
                     and exits.
  --latency-history  Like --latency but tracking latency changes over time.
                     Default time interval is 15 sec. Change it using -i.
  --latency-dist     Shows latency as a spectrum, requires xterm 256 colors.
                     Default time interval is 1 sec. Change it using -i.
  --lru-test <keys>  Simulate a cache workload with an 80-20 distribution.
  --slave            Simulate a slave showing commands received from the master.
  --rdb <filename>   Transfer an RDB dump from remote server to local file.
  --pipe             Transfer raw Redis protocol from stdin to server.
  --pipe-timeout <n> In --pipe mode, abort with error if after sending all data.
                     no reply is received within <n> seconds.
                     Default timeout: 30. Use 0 to wait forever.
  --bigkeys          Sample Redis keys looking for big keys.
  --hotkeys          Sample Redis keys looking for hot keys.
                     only works when maxmemory-policy is *lfu.
  --scan             List all keys using the SCAN command.
  --pattern <pat>    Useful with --scan to specify a SCAN pattern.
  --intrinsic-latency <sec> Run a test to measure intrinsic system latency.
                     The test will run for the specified amount of seconds.
  --eval <file>      Send an EVAL command using the Lua script at <file>.
  --ldb              Used with --eval enable the Redis Lua debugger.
  --ldb-sync-mode    Like --ldb but uses the synchronous Lua debugger, in
                     this mode the server is blocked and script changes are
                     are not rolled back from the server memory.
  --help             Output this help and exit.
  --version          Output version and exit.

Examples:
  cat /etc/passwd | redis-cli -x set mypasswd
  redis-cli get mypasswd
  redis-cli -r 100 lpush mylist x
  redis-cli -r 100 -i 1 info | grep used_memory_human:
  redis-cli --eval myscript.lua key1 key2 , arg1 arg2 arg3
  redis-cli --scan --pattern '*:12345*'

  (Note: when using --eval the comma separates KEYS[] from ARGV[] items)

通过 redis-cli 的参数我们可以知悉 redis-cli 的功能还是很丰富的,不只是单单连接一下服务器而已。

关闭 Redis 服务器

redis-cli shutdown

数据库

  1. 切换数据库
    在 Redis 中一共有 16 个数据库,这 16 个数据库不许呀我们手动去创建,需要使用哪个直接切换就可以:select <db-index>
    如上图所示,一开始连接的时候,默认使用的 0 号数据库,当我们切换后,提示符会显示我们正在使用的是哪个数据库(图中红框框出处),当为 0 号数据库时则不会给出提示。
  2. 删除数据库中的内容:flushdb
  3. 删除所有数据库中的内容:flushall

2. Redis 数据类型

一开始我们已经知道了 Redis 是一个 key-value 的数据库,在传统的 key-value 存储中,key 和 value 一般都是字符串类型的,但是在 Redis 中,value 还可以是其他类型。且 Redis 中的 key 和 value 是二进制安全(binary safe,不对输入数据进行处理,以二进制的形式保存数据,例如 你可以将 \0 作为 key,也可以将一幅图片的数据作为 key,而 Redis 不会对其中的内容做任何处理,而是以字节的形式保存它)的。虽然 Redis 中的 value 可以有多种类型,但是都是以 string 为基础的。

Redis 中的 KEY

Redis 的 KEY 是二进制安全的,KEY 最多为512MB。在命名 KEY 时,Redis 给出了一些建议:

  • 不要太长。太长的 KEY 不利于在数据库中查找。如果是要查询一个很大的对象是否存在,可以先将其哈希之后,再来对比哈希值是否存在
  • 不要太短。太短了可能会损失可读性,在可读性和长短之间找到一个平衡
  • KEY 的取名要遵循一定的模式,如 "object-type:id"
  1. 查看 KEY 的数量:dbsize
  2. 查找符合某个模板的 KEY:keys pattern,其中的 pattern 可以是 ?、* 、[abc...]、[a-d]等方式。其中?代表一个任意一个字符,*代表任意0个或多个字符,[abc...]代表只能是[]中的值,[a-d]代表a到d范围内总的值
  3. 查询 KEY 的数量:exists key1 [key2 ...],查询数据库中一个或多个 KEY 的数量
  4. 删除:del key

Redis string

Redis List

Redis Hash

Redis Set

Redis Sorted set

Redis Bitmap

Redis HyperLogLog

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值