redis

更新日期: 2015-12-12 阅读次数: 8575 分类: Redis

Redis is an open source, BSD licensed, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.

通常 Redis 用来做什么

What Redis means actually? It means REmote DIctionary Server.

从 Redis 的名字可以看出,Redis 适合做数据 cache,多进程的数据共享,队列等。

并发能力

500k requests per second.

list value 的长度限制

如果使用 Redis 某个 key 对应的 list value 做任务队列,是否有长度限制。

Every list, set, and sorted set, can hold 2**32 elements(more than 4 billion). In other words your limit is likely the available memory in your system.

redis 有 database, table 或是 collection 的概念么?

redis 有 database 的概念,其他的则没有。

redis 默认的 database 是 0 (好简洁的名字!)

如果要切换到 database 1 只需要输入:

> select 1

然后命令行的提示就会变为, 127.0.0.1:6379[1]

为什么会有 database 的概念呢? 主要是用来将不同应用间的数据进行隔离。

redis 的数据操作特点

  • Keys are strings which identify pieces of data (values)
  • Values are arbitrary byte arrays that Redis doesn’t care about
  • Redis exposes (and is implemented as) five specialized data structures
  • Combined, the above make Redis fast and easy to use, but not suitable for every scenario

五种数据类型

strings, hashes, lists, sets and sorted sets.

value 是何种数据类型,取决于使用那种操作命令。

例如, set 对应的数据类型是 string.

  • hash 非常适合存储类 json 的数据
  • sorted set 适合做分布统计,分数排行

string or hash

当我们使用 redis 存储用户 session 对应的用户信息时,通常用户信息包含了 N 种信 息,例如 user_id, access_token, refresh_token 等。每次用户操作时,并不需要将所 有的数据都取出来,例如 refresh_token, 只有 access_token 过期时,才需要读取。 若用 string 类型存储 json,则每次都要把所有数据拉回来 loads。所以不如 hash 这 种数据类型方便。

除了五种数据结构之外,redis 还有什么

expire 为 key 设置过期时间:

expire pages:about 30

expireat pages:about 1356933600

清空 redis 中的所有数据

FLUSHALL

参考

更多关于 redis 的笔记

关于作者 🌱

我是来自山东烟台的一名开发者,有敢兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式