1.发布订阅
PSUBSCRIBE pattern [pattern ...]
summary: Listen for messages published to channels matching the given patterns
since: 2.0.0
订阅符合正则表达式的一类频道
eg:PSUBSCRIBE p* b* //订阅以p开头和b开头的所有频道
PUBLISH channel message
summary: Post a message to a channel
since: 2.0.0
发布消息到指定频道
PUBSUB subcommand [argument [argument ...]]
summary: Inspect the state of the Pub/Sub subsystem
since: 2.8.0
PUNSUBSCRIBE [pattern [pattern ...]]
summary: Stop listening for messages posted to channels matching the given patterns
since: 2.0.0
停止订阅符合正则的一类频道
SUBSCRIBE channel [channel ...]
summary: Listen for messages published to the given channels
since: 2.0.0
订阅某个频道
UNSUBSCRIBE [channel [channel ...]]
summary: Stop listening for messages posted to the given channels
since: 2.0.0
停止订阅某个频道。
2. 布隆过滤器
RedisBloom安装及使用
- 去redis.io找到module,里面找到RedisBloom模块,GitHub地址https://github.com/RedisBloom/RedisBloom
- 下载项目zip包,或者在linux中weget url(前提需要安装wget指令)
- 在linux中下载unzip指令 yum install unzip
- unzip 路径解压RedisBloom
- cd 解压的路径,然后make
- make完成会生成redisbloom.so扩展库
- 在启动redis-server时加入--loadmodule 绝对路径/redisbloom.so,也可以在全局配置文件中添加相关配置(注意路径为绝对路径)。
创建一个新的布隆过滤器并添加新的元素
BF.ADD key value .....
# 127.0.0.1:6379> BF.ADD newFilter foo
(integer) 1
查找布隆过滤器中是否包含某个元素
# 127.0.0.1:6379> BF.EXISTS newFilter foo
(integer) 1
返回值为1,表示布隆过滤器中包含这个元素
# 127.0.0.1:6379> BF.EXISTS newFilter bar
(integer) 0
返回值为1,表示布隆过滤器不包含这个元素
使用场景:
缓存穿透:当查询一个数据在redis缓存中不存在时,会进行查询数据库,如果数据库也不存在就会返回位空。当出现大量查询这种数据库中不存在的值时,就会造成频繁的从访问redis缓存到访问数据库,造成效率降低。通过布隆过滤器就可以很好的解决这个问题;
- 如果实现这个功能就需要在插入数据时,把插入的数据通过BF.ADD添加进布隆过滤器。
- 每次添加进去的值会通过多个函数,每个函数会产生一个下标索引,然后在一个位图上的下标索引处从0改为1
- 如果需要查询一个数据,会通过过滤器,经过多个函数返回的索引,在位图中查找,如果所有位都为1,则表明这个值可能存在数据库中,如果有个别位为0,则这个值一定不存在数据库中,
- 如果布隆过滤器返回值为1,就可以访问redis缓存,如果缓存中不存在这个值,就会访问数据库查询。有可能这个数据不存在数据库中,返回为空。
- 然后把值和数据查询的结果添加进入redis缓存。
3.过期时间
过期时间包括三种值:
- 0<=expire 表示设置了过期时间
- expire = -1 表示这个值没有设置过期时间,一直存在与内存
- expire = -2 表示这个值为过期数据
注意:在redis中一旦设置了过期时间,是无法刷新时间的,在过期之前,如果修改数据,过期时间会变为-1.
Redis如何淘汰过期的keys
Redis keys过期有两种方式:被动和主动方式。
当一些客户端尝试访问它时,key会被发现并主动的过期。
当然,这样是不够的,因为有些过期的keys,永远不会访问他们。 无论如何,这些keys应该过期,所以定时随机测试设置keys的过期时间。所有这些过期的keys将会从密钥空间删除。
具体就是Redis每秒10次做的事情:
- 测试随机的20个keys进行相关过期检测。
- 删除所有已经过期的keys。
- 如果有多于25%的keys过期,重复步奏1.
这是一个平凡的概率算法,基本上的假设是,我们的样本是这个密钥控件,并且我们不断重复过期检测,直到过期的keys的百分百低于25%,这意味着,在任何给定的时刻,最多会清除1/4的过期keys。
4.缓存
可以在redis配置文件redis.conf设置最大的缓存内存。
缓存回收策略:
# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
# is reached. You can select one from the following behaviors:
#
# volatile-lru -> Evict using approximated LRU, only keys with an expire set.
# allkeys-lru -> Evict any key using approximated LRU.
# volatile-lfu -> Evict using approximated LFU, only keys with an expire set.
# allkeys-lfu -> Evict any key using approximated LFU.
# volatile-random -> Remove a random key having an expire set.
# allkeys-random -> Remove a random key, any key.
# volatile-ttl -> Remove the key with the nearest expire time (minor TTL)
# noeviction -> Don't evict anything, just return an error on write operations.
#
# LRU means Least Recently Used
# LFU means Least Frequently Used
#
# Both LRU, LFU and volatile-ttl are implemented using approximated
# randomized algorithms.
#
# Note: with any of the above policies, when there are no suitable keys for
# eviction, Redis will return an error on write operations that require
# more memory. These are usually commands that create new keys, add data or
# modify existing keys. A few examples are: SET, INCR, HSET, LPUSH, SUNIONSTORE,
# SORT (due to the STORE argument), and EXEC (if the transaction includes any
# command that requires memory).
#
# The default is:
#
# maxmemory-policy noeviction
LRU表示最近最少使用替换算法,侧重于最后一次使用的时间。
LFU表示最近最不常用替换算法,侧重于一段时间使用的次数。
# volatile-lru ->针对快要过期的key,进行LRU算法。
# allkeys-lru -> 针对所有的key,进行LRU算法
# volatile-lfu ->针对快要过期的key,进行LFU算法
# allkeys-lfu -> 针对所有的key,进行LFU算法
# volatile-random ->针对快要过期的key,进行随机回收
# allkeys-random ->针对所有的key,进行随机回收
# volatile-ttl -> Remove the key with the nearest expire time (minor TTL)
# noeviction ->缓存满了直接报错。(保证了数据的完整性)
一般的经验规则:
- 使用allkeys-lru策略:当你希望你的请求符合一个幂定律分布,也就是说,你希望部分的子集元素将比其它其它元素被访问的更多。如果你不确定选择什么,这是个很好的选择。.
- 使用allkeys-random:如果你是循环访问,所有的键被连续的扫描,或者你希望请求分布正常(所有元素被访问的概率都差不多)。
- 使用volatile-ttl:如果你想要通过创建缓存对象时设置TTL值,来决定哪些对象应该被过期。
allkeys-lru 和 volatile-random策略对于当你想要单一的实例实现缓存及持久化一些键时很有用。不过一般运行两个实例是解决这个问题的更好方法。
为了键设置过期时间也是需要消耗内存的,所以使用allkeys-lru这种策略更加高效,因为没有必要为键取设置过期时间当内存有压力时。
5.事务
DISCARD -
summary: Discard all commands issued after MULTI
since: 2.0.0
放弃事务
EXEC -
summary: Execute all commands issued after MULTI
since: 1.2.0
执行事务
MULTI -
summary: Mark the start of a transaction block
since: 1.2.0
开启事务
UNWATCH -
summary: Forget about all watched keys
since: 2.2.0
取消监听
WATCH key [key ...]
summary: Watch the given keys to determine execution of the MULTI/EXEC block
since: 2.2.0
监听某个key是否被修改
两个事务谁先执行EXEC,就会把队列内的语句全执行。