- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 application.yml 中有一个默认的 redis 缓存配置:
cache:
type: redis
redis:
time-to-live: 7200000 # 2 hour TTL - Tune this if needed later
redis:
host: myHost
port: myPort
password: myPass
ssl: true
cluster:
nodes: clusterNodes
timeout: 10000
它工作得很好,我不想为它创建任何自定义缓存管理器。
但是,我的代码中有一些缓存不需要使用 redis。出于这个原因,我想制作第二个 CacheManager,它是一个简单的 ConcurrentHashMap 并使用 @Cacheable 指定它
为此,我创建了一个新的 CacheManager Bean:
@Configuration
@EnableCaching
@Slf4j
class CachingConfiguration {
@Bean(name = "inMemoryCache")
public CacheManager inMemoryCache() {
SimpleCacheManager cache = new SimpleCacheManager();
cache.setCaches(Arrays.asList(new ConcurrentMapCache("CACHE"));
return cache;
}
}
这导致 inMemoryCache 成为我的默认缓存,而我的所有其他 @Cacheable() 都尝试使用 inMemoryCache。我不希望我创建的 CacheManager bean 成为我的默认值。无论如何我可以指定它是次要的并且不阻止 spring-cache 做它的魔法吗?
谢谢!
最佳答案
我能够保留 yml 中的默认 Redis 配置,并使用以下代码添加一个新的 inMemory CacheManager(我使用的是 Clustered Redis):
@Bean
@Primary
public RedisCacheManager redisCacheManager(LettuceConnectionFactory lettuceConnectionFactory) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
redisCacheConfiguration.usePrefix();
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(lettuceConnectionFactory)
.cacheDefaults(redisCacheConfiguration).build();
}
@Bean
public LettuceClusterConnection getConnection(LettuceConnectionFactory lettuceConnectionFactory) {
LettuceClusterConnection clusterConnection = (LettuceClusterConnection) lettuceConnectionFactory.getClusterConnection();
return clusterConnection;
}
@Bean(name = "inMemoryCache")
public CaffeineCacheManager inMemoryCache() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
cacheManager.setCaffeine(caffeineCacheBuilder());
return cacheManager;
}
Caffeine< Object, Object > caffeineCacheBuilder() {
return Caffeine.newBuilder()
.initialCapacity(3000)
.maximumSize(40000)
.expireAfterAccess(30, TimeUnit.MINUTES);
}
关于java - 如何在不覆盖默认 spring-cache 的情况下创建辅助 CacheManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56905704/
使用 ehcache 和 Spring 启动 tomcat 时收到此错误。 Another CacheManager with same name 'cacheManager' already exi
我正在尝试在我的应用程序中使用 Ehcache 管理器。我想在没有 xml 配置的情况下设置它。我有下一个依赖项: org.ehcache ehcache 3.6.0
我要使用 CacheManager对于我的 .net 项目。问题是我找不到 CacheManager.Memcached 用法的任何示例。 我是这样用的: public class GlobalCac
我注意到根据文档,要创建缓存,您必须实例化或获取 CacheManager 并将缓存添加到其中。 singletonManager = CacheManager.create(); Cache mem
我有一个简单的 API,我的客户可以在独立应用程序中使用它。我的 API 在幕后使用 Ehcache 来提高性能。 一切正常,除了我的客户端需要在 API 中调用 shutdown() 方法来调用 C
尝试在我的 Spring 应用程序中的 Controller 上运行单元测试。 我的测试类: @RunWith (SpringJUnit4ClassRunner.class) @WebAppConfi
我已经创建了一个 nopCommerce v3.5 插件,并希望在 Controller Action 下加载一个文本文件到 _cacheManager 然后在下一个中重用它请求。 但是 _cache
使用 CacheManager ,您如何最好地实现这种情况,其中缓存实例包含可能需要很长时间才能从慢速来源获取的数据? 我从不希望用户等待缓存填充(我不关心第一次加载) 我可以想到两种方法,但不确定它
我一直在尝试使用 CacheManager缓存一些http请求,但每次都因空指针异常而失败。后一些挖掘我相信我找到了原因: CacheManager.getCacheFileBaseDir() 总是返
我正在使用 Spring Boot 和 EhCache 开发日历应用程序。我正在尝试缓存以下方法: @Override @Cacheable(value = "concerts") public Li
如何在我的应用程序的任何部分获取对 Shiro 框架中的 cacheManager 对象的引用?例如,我想删除在删除用户或更新其权限期间缓存的旧用户数据。现在我正在按照以下方式处理它 public v
我正在使用 redis 作为我的 spring 缓存实现。官方文档说我们应该像这样配置缓存管理器: 当 jedis 版本 = 2.0.0 和 spring-data-redis 版本 = 1.0.
有人可以确认 CacheManager.Net 是否支持 redis 流水线吗? 我在 documentation 中找不到它 非常感谢。 干杯,你 最佳答案 有点。CacheManager 不直接支
从 CacheManager.NET 的角度来看,我有一个关于 Redis 连接参数的基本问题。如果我们的 Redis 集群有一个主节点和两个从节点,并且有哨兵进程的法定人数,我们是否应该提供指向哨兵
我没有找到 CacheManager bean...但是我没有尝试对 CacheManager 做任何事情! 这是我的错误! org.springframework.beans.factory.Bea
net.sf.ehcache.CacheManager.ALL_CACHE_MANAGERS 是否有替代品?在 org.ehcache ehcach
根据Ehcache ,一个应用程序可以有多个CacheManager。由于可以在单个 CacheManager 中维护多个缓存,那么拥有多个 CacheManager 会给我们带来什么好处呢? Dis
我的 application.yml 中有一个默认的 redis 缓存配置: cache: type: redis redis: time-to-live: 7200000
当我使用单元测试缓存移动来测试我的应用程序时,效果很好。但是当我尝试通过功能测试来测试我的 api 时,我遇到了一个异常。 public function testGet() { Cache:
我在尝试使用 Guice 在 Play Framework 2.4.6 中进行 Controller 测试时遇到了标题所示的错误。它发生在 Controller 中的任何 view.render 代码
我是一名优秀的程序员,十分优秀!