redis命令总结
String
List
Set
Hash
sort
正则匹配 批量删除key
https://stackoverflow.com/questions/31029982/redis-how-to-delete-multiple-keys-matching-pattern
EVAL "return redis.call('del', 'defaultKey', unpack(redis.call('keys', ARGV[1])))" 0 "xx*"
![](https://img2020.cnblogs.com/blog/1281466/202012/1281466-20201218181133779-1379431819.png)
redisTemplate.execute((RedisCallback) connection -> {
Object eval = connection.eval("return redis.call('del', 'defaultKey', unpack(redis.call('keys', ARGV[1])))".getBytes(), ReturnType.INTEGER, 0, (prefix+"*").getBytes());
return eval;
});
分布式锁
https://redis.io/docs/manual/patterns/distributed-locks/
给java lock加上分布式特性
https://github.com/redisson/redisson/wiki/8.-Distributed-locks-and-synchronizers
https://www.baeldung.com/redis-redisson
看门狗看门时间可以配置, 如果一个lock获取后超过这个时间,看门狗会自动清理掉这个锁,避免它一直挂在那里。
因为lock的获取和释放,必须放在同一个线程中
https://redis.io/commands/set
2.6之后, set命令支持多个参数,
nx不存在时设置, 并ex设置过期时间
![](https://img2018.cnblogs.com/blog/1281466/201812/1281466-20181215213523879-1234931378.png)
不要再使用setnx了
解锁删除key
不能简单的使用del, 需要使用lua脚本, 防止本机删除了其他机器的key. 使用上面的lib, 不要自己实现
二级索引
https://redis.io/docs/manual/patterns/indexes/
HA sentinel + master_slaver
@@@build beautiful things, share happiness@@@