type
status
date
urlname
summary
tags
category
icon
password
catalog
sort
 
记录一次scan和keys的使用,scan和key都是redis搜索key的值函数,但实现却完全不同。生产环境用key的同学准备好跑路吧~

keys

Warning: consider KEYS as a command that should only be used in production environments with extreme care. It may ruin performance when it is executed against large databases. This command is intended for debugging and special operations, such as changing your keyspace layout. Don't use KEYS in your regular application code. If you're looking for a way to find keys in a subset of your keyspace, consider using sets.
上面是官方文档声明,KEYS命令不能用在生产的环境中,这个时候如果数量过大效率是十分低的。
keys函数支持传入一个正则字符串,遍历redis中所有匹配到的key。命令会引起阻塞,性能随着数据库数据的增多而越来越慢。

SCAN

Redis从2.8版本开始支持scan命令,SCAN命令的基本用法如下
  1. 复杂度虽然也是 O(n),通过游标分步进行不会阻塞线程;
  1. 有限制参数 COUNT ;
  1. 同 keys命令 一样提供模式匹配功能;
  1. 服务器不需要为游标保存状态,游标的唯一状态就是 scan 返回给客户端的游标整数;
但是需要注意返回结果可能重复,需要客户端去重。如果遍历过程中有数据修改,改动后的数据不保证同步。
scan 命令提供三个参数,第一个是cursor,第二个是要匹配的正则,第三个是单次遍历的槽位

RedisOperations sacn

spring中redisOperations只有HashOperations类提供scan方法

使用

第一个遍历是 cursor 值为0,然后将返回结果的第一个整数作为下一个遍历的游标,如果最后返回的到cursor的值为0就代表结束。
RestTemplate与OpenFeignspring cache
  • Giscus