在spring启动应用程序中清除远程redis缓存

6tqwzwtp  于 2021-06-08  发布在  Redis
关注(0)|答案(1)|浏览(371)

我使用的是springboot2.3,使用的是app.properties的默认缓存机制。我定义了所有值:

spring.cache.type = redis
spring.redis.host = host
spring.redis.port = port
spring.redis.timeout = 4000
spring.redis.password = psw
spring.cache.redis.time-to-live = 28800000

我利用spring存储库中的缓存,例如:

@Cacheable(cacheNames = "contacts")
    @Override
    Page<Contact> findAll(Specification specification, Pageable pageable);

它按预期工作。然而,redis是我的几个应用程序使用的集群,我需要第二个应用程序能够删除redis中的一些/所有键。
应用程序a1利用缓存并将密钥放入其中。应用程序a2,需要清除一些键或所有键。
在a2中我做到了:

cacheManager.getCacheNames().forEach(cacheName -> cacheManager.getCache(cacheName).clear());

当然,缓存名称的列表是空的,因为在这个应用程序中,我没有向缓存添加密钥,而且,无论如何,我没有与a1相同的密钥。我应该列出遥控钥匙,然后我需要清除它们。有没有不使用spring数据redis库的简单方法?

v8wbuo2f

v8wbuo2f1#

您可以为redis中的整个缓存定义一个单独的前缀。类似于缓存项的命名空间。在您可以刷新此命名空间中的所有键之后。
注意:确保 CacheManager 只有redis缓存,没有内存缓存(l1)。

相关问题