当 redis 通过 spring boot (<artifactId>spring-boot-starter-data-redis</artifactId>
) 用作缓存技术时,我看到很少有像 TTL 这样的属性可以在 application.properties
中设置文件。
前任:
spring.cache.cache-names=cache1,cache2
spring.cache.redis.time-to-live=600000
以及更多来自 - Appendix A. Common application properties 的片段spring.redis.database=0 # Database index used by the connection factory.
spring.redis.url= # Connection URL. Overrides host, port, and password. User is ignored. Example: redis://user:password@example.com:6379
spring.redis.host=localhost # Redis server host.
但我无法弄清楚如何设置缓存逐出策略,例如 - 最不常用或最近使用过等。我必须如何以及在哪里提供此配置详细信息?
最佳答案
Redis cache documentation状态:
It is possible to set the configuration directive using the redis.conf file, or later using the CONFIG SET command at runtime.
Redis configuration documentation状态:
maxmemory 2mb
maxmemory-policy allkeys-lru
结合两者,改变驱逐策略的命令是:CONFIG SET maxmemory-policy allkeys-lfu
使用 Spring Data Redis:如果使用非响应式(Reactive) Redis 连接:
RedisConnection conn = null;
try {
conn = connectionFactory.getConnection();
conn.setConfig("maxmemory-policy", "allkeys-lfu");
} finally {
if (conn != null) {
conn.close();
}
}
如果使用响应式(Reactive) Redis 连接:ReactiveRedisConnection conn = connectionFactory.getReactiveConnection();
conn
.serverCommands()
.setConfig("maxmemory-policy", "allkeys-lfu")
.filter(status -> status.equals("OK"))
.doFinally(unused -> conn.close())
.block(Duration.ofSeconds(5L));
关于spring - 使用spring-boot-starter-data-redis时,驱逐策略如何设置? LFU 或 LRU 等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64489012/