spring - 使用spring-boot-starter-data-redis时,驱逐策略如何设置? LFU 或 LRU 等?

标签 spring spring-boot redis spring-data-redis

当 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/

相关文章:

Java Spring RestTemplate 设置不需要的 header

java - Spring Boot Second DataSource 导致 Could not autowire 字段

java - ClassCastException 发生在 Solver.solve 上

node.js - 如何 "dont vote twice"机制

java - 解压缩/提取 Spring Tools Suite 下载的 Zip/Jar 文件时出错

spring-boot - Spring Webflux 上传大图片文件并以流方式使用 WebClient 发送文件

spring-boot - Spring boot - 几个请求的第一个请求的响应时间很长

java - 如何使用redis有序集实现比赛排行榜

node.js - 使用 Redis 和 Socket.io 进行用户身份验证

java - 控制 session 时间戳是否更新