Redis key中冒号的作用

在spring boot项目中使用Cacheable注解的时候,除了在注解中定义key之后,在redis中查看发现定义的key还有一串很长的包名,用冒号隔开。早期redis只能存储字符串,对于一些结构化的数据,想存进redis的时候就用冒号隔开,例如:

127.0.0.1:6379> set person:name ryan
OK
127.0.0.1:6379> set person:email [email protected]
OK
127.0.0.1:6379> get person:name
"ryan"
127.0.0.1:6379> get person:email
"[email protected]"
127.0.0.1:6379> keys *
1) "person:email"
2) "person:name"

所以这个冒号**在redis中并无什么功能上和性能上的作用**,只是用来标记key的命名空间方便程序取值的,现在用哈希表实现同样的功能

127.0.0.1:6379> hset person name ryan
(integer) 1
127.0.0.1:6379> hset person email [email protected]
(integer) 1
127.0.0.1:6379> hget person name
"ryan"
127.0.0.1:6379> hget person email
"[email protected]"

但是用冒号标记key的命名空间成为了key命名的一种约定,项目上还是使用这种命名方法,而且[官方文档](<https://redis.io/topics/data-types-intro>)也是建议这种命名key。通过redis manager连接redis发现,redis manager会将带冒号的可以整理成树状结构。实际上redis的key是二进制安全的,所以不一定只有字符串作为key,图片都可以,官方文档是这么说的:

Redis keys are binary safe, this means that you can use any binary sequence as a key, from a string like “foo” to the content of a JPEG file. The empty string is also a valid key.

如何在shell中批量删除某个命名空间下的所有key

redis的del命令可以批量删除,但是必须把所有key都写在命令参数里面

127.0.0.1:6379> DEL key [key ...] 
#DEL 命令的用法

key的数量少还好,如果上万个key实践起来不现实。xarges命令可以实现该功能,具体方法如下:

redis-cli keys pattern* | xargs redis-cli del

这样可以删除所有以pattern开头的key,其他类型的key可以参考redis [keys命令文档](<https://redis.io/commands/KEYS>)里面正则表达式的写法,下面实践一下:

[ryan@LcUxz3hEf42kWemb4o ~]$ redis-cli keys user_permission::first:com.ryan.priv.web.modular.auth.service.impl.UserServiceImpl:getUserInfoById:*
 1) "user_permission::first:com.ryan.priv.web.modular.auth.service.impl.UserServiceImpl:getUserInfoById:11"
 2) "user_permission::first:com.ryan.priv.web.modular.auth.service.impl.UserServiceImpl:getUserInfoById:26"
 3) "user_permission::first:com.ryan.priv.web.modular.auth.service.impl.UserServiceImpl:getUserInfoById:31"
 4) "user_permission::first:com.ryan.priv.web.modular.auth.service.impl.UserServiceImpl:getUserInfoById:2"
 5) "user_permission::first:com.ryan.priv.web.modular.auth.service.impl.UserServiceImpl:getUserInfoById:20"
 6) "user_permission::first:com.ryan.priv.web.modular.auth.service.impl.UserServiceImpl:getUserInfoById:17"
 7) "user_permission::first:com.ryan.priv.web.modular.auth.service.impl.UserServiceImpl:getUserInfoById:9"
 8) "user_permission::first:com.ryan.priv.web.modular.auth.service.impl.UserServiceImpl:getUserInfoById:16"
 9) "user_permission::first:com.ryan.priv.web.modular.auth.service.impl.UserServiceImpl:getUserInfoById:13"
10) "user_permission::first:com.ryan.priv.web.modular.auth.service.impl.UserServiceImpl:getUserInfoById:15"
11) "user_permission::first:com.ryan.priv.web.modular.auth.service.impl.UserServiceImpl:getUserInfoById:10"
12) "user_permission::first:com.ryan.priv.web.modular.auth.service.impl.UserServiceImpl:getUserInfoById:7"
13) "user_permission::first:com.ryan.priv.web.modular.auth.service.impl.UserServiceImpl:getUserInfoById:1"
14) "user_permission::first:com.ryan.priv.web.modular.auth.service.impl.UserServiceImpl:getUserInfoById:4"
15) "user_permission::first:com.ryan.priv.web.modular.auth.service.impl.UserServiceImpl:getUserInfoById:28"
16) "user_permission::first:com.ryan.priv.web.modular.auth.service.impl.UserServiceImpl:getUserInfoById:14"
17) "user_permission::first:com.ryan.priv.web.modular.auth.service.impl.UserServiceImpl:getUserInfoById:6"
18) "user_permission::first:com.ryan.priv.web.modular.auth.service.impl.UserServiceImpl:getUserInfoById:8"
19) "user_permission::first:com.ryan.priv.web.modular.auth.service.impl.UserServiceImpl:getUserInfoById:25"
[ryan@LcUxz3hEf42kWemb4o ~]$ redis-cli keys user_permission::first:com.ryan.priv.web.modular.auth.service.impl.UserServiceImpl:getUserInfoById:* | xargs redis-cli del
(integer) 19

先查看一下有多少这种规则的key,然后通过xargs命令批量删除,可以看到已经成功删除了19个key。

参考链接:

https://redis.io/topics/data-types-intro

what-is-the-purpose-of-colons-within-redis-keys