标签: spring-cache

为什么 @Cachable(...) 可以与 @Bean return mock() 一起使用,但不能与 @MockedBean 一起使用

为什么使用时缓存会被值填满

@Autowired
ServiceXY serviceXY

@TestConfiguration
static class AppDefCachingTestConfiguration {
    @Bean
    public ServiceXY ServiceXYMock() {
        return mock(ServiceXY.class);
    }
}
Run Code Online (Sandbox Code Playgroud)

但不与

@MockBean
ServiceXY serviceXY
Run Code Online (Sandbox Code Playgroud)

使用 @MockBean 时,在访问缓存值时会出现 NullPointerException,如我的测试中所示:

@Autowired
ConcurrentMapCacheManager cmcm; 

@Test
void anTest(){
when(serviceXY.methodThatFillsCache(anyString()).thenReturn("ABC");

serviceXY.methodThatFillsCache("TEST1");

cmcm.getCache("Cachename").get("TEST1",String.class).equals("ABC");
...
}
Run Code Online (Sandbox Code Playgroud)

mockito spring-bean spring-boot spring-cache spring-boot-test

4
推荐指数
1
解决办法
639
查看次数

如何为使用Spring Cache缓存的Spring Web Service设置正确的Last-Modified标头值?

我有这样的Spring MVC控制器:

@Controller
@RequestMapping(value = "/user")
public class UserController {
   .....      
   @Cacheable(value = "users", key = "#id")
   @RequestMapping(value = "/get", method = RequestMethod.GET)
   @ResponseBody
   public User getUser(Long id){
       return userService.get(id);
   }
   ....
}
Run Code Online (Sandbox Code Playgroud)

我想将标题Last-Modified添加到GetUser Web服务的HTTP响应中.
如何在我的商店中添加缓存时获得正确的日期?
如何将此日期的Last-Modified标题添加到Spring Controller方法的响应中?

java spring caching spring-mvc spring-cache

3
推荐指数
2
解决办法
7633
查看次数

Spring缓存常量作为键

作为我的代码的一部分,我有一个空参数的方法.对于e..g,

public MasterData fetchMasterData() {
// DO something.
}
Run Code Online (Sandbox Code Playgroud)

我想添加一个带密钥的@Cacheable作为'masterdata'.我尝试了以下内容,但它查找名为"masterdata"的bean.我试过了@Cacheable(cache="master", key="masterdata")

如果我保留key属性,我知道它需要空键.但我想明确地给出一个常数作为关键.

有没有办法做到这一点?

spring-cache

3
推荐指数
1
解决办法
2208
查看次数

Hazelcast缓存未根据<time-to-live-seconds>逐出

我们配置<time-to-live-seconds>为1200(20分钟)但缓存在缓存创建时间一分钟后自动逐出.

有人可以告诉如何在指定的时间段内使缓存生效吗?

spring caching hazelcast spring-cache hazelcast-imap

3
推荐指数
1
解决办法
4075
查看次数

使用JCache配置API限制缓存大小

我正在使用JCache API在应用程序中配置缓存,该应用程序在Ehcache 3中使用弹簧缓存。

cacheManager.createCache("users", new MutableConfiguration<String, User>()
                    .setStoreByValue(false)
                    .setManagementEnabled(true)
                    .setStatisticsEnabled(true)
                    .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.TEN_MINUTES)));
Run Code Online (Sandbox Code Playgroud)

如何将缓存大小限制为50个条目?通过Ehcache XML配置很容易做到这一点,但是如果有一种使用JCache config API进行控制的方法,我宁愿使用它。

java caching jcache spring-cache

3
推荐指数
1
解决办法
1692
查看次数

Spring ehcache vs Memcached?

我曾使用ehcache处理过弹簧问题.对我而言,暴露的不同API集及其实现也是如此.

除了API /实现之外,它们之间提供的功能有何不同?

更新: -我已经看过Hibernate EHCache vs MemCache,但这个问题主要来自hibernate的观点,但我的问题一般是针对任何缓存服务.对该问题的回答还表明,在功能方面没有太大差异

spring memcached ehcache spring-cache

3
推荐指数
1
解决办法
4265
查看次数

Config.class 中的 CacheManager bean 定义导致 NoSuchBeanDefinitionException

我有一个正在检查数据库条目的 Spring 服务。为了最小化我的存储库调用,两个查找方法都是“@Cacheable”。但是当我尝试初始化我的服务 bean 而我的配置类有一个 CacheManager bean 定义时,我得到以下 NoSuchBeanDefinitionException:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'foo.mediacode.directory.MediaCodeDirectoryService' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1093)
at foo.mediacode.directory.MediaCodeDirectoryService.implementation(MediaCodeDirectoryService.java:63)
at foo.campaigntree.directory.CampaignTreeDirectoryService.<init>(CampaignTreeDirectoryService.java:18)
... 15 more
Run Code Online (Sandbox Code Playgroud)

如果我取出 CacheManager bean 定义,我可以初始化我的服务 bean 并且它运行没有任何问题和缓存

这是我的代码:配置

...
    @Configuration
    @EnableCaching
    @EnableJpaRepositories(...)
    @PropertySource({...})
    public class MediaCodeDirectoryServiceConfig {

        private static Logger   configLogger    = Logger.getLogger(MediaCodeDirectoryServiceConfig.class.getName());

        @Value("${jpa.loggingLevel:FINE}")
        private String          loggingLevel;

        @Value("${mysql.databaseDriver}")
        private String          dataBaseDriver;

        @Value("${mysql.username}")
        private String          username;

        @Value("${mysql.password}")
        private String          password;

        @Value("${mysql.databaseUrl}")
        private String          databaseUrl;

        @Bean
        public …
Run Code Online (Sandbox Code Playgroud)

java eclipselink spring-data-jpa spring-cache

3
推荐指数
1
解决办法
1179
查看次数

Spring @Cacheable默认ttl

我通常@Cacheable在spring-boot应用程序中将其与缓存配置结合使用,并为每个缓存设置特定的TTL(生存时间)。

我最近继承了一个spring boot应用程序,该应用程序使用时@Cacheable未明确说明缓存管理器和ttl。我将其更改为明确。

但是,当没有明确的内容时,我无法找出默认值。

我确实看过文档,但那里什么也没找到

spring caching spring-mvc spring-cache

3
推荐指数
3
解决办法
2万
查看次数

将Spring Boot从1.5.12迁移到1.5.13后,ehcache提供错误

我已经配置了Spring Boot 1.5.12 + ehcache,一切正常,直到我将Spring Boot升级到1.5.13

application.yml 具有以下条目

spring:
  cache:
    jcache:
      provider: org.ehcache.jsr107.EhcacheCachingProvider
      config: ehcache.xml
Run Code Online (Sandbox Code Playgroud)

ehcache.xml位于resources目录下

我收到的错误是:

Caused by: java.lang.IllegalArgumentException: Cache configuration does not exist 'ServletContext resource [/ehcache.xml]'
    at org.springframework.util.Assert.isTrue(Assert.java:92)
    at org.springframework.boot.autoconfigure.cache.CacheProperties.resolveConfigLocation(CacheProperties.java:117)
    at org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration.createCacheManager(JCacheCacheConfiguration.java:113)
    at org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration.jCacheCacheManager(JCacheCacheConfiguration.java:97)
    at org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration$$EnhancerBySpringCGLIB$$e5c3a047.CGLIB$jCacheCacheManager$1(<generated>)
    at org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration$$EnhancerBySpringCGLIB$$e5c3a047$$FastClassBySpringCGLIB$$a6ae7187.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358)
    at org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration$$EnhancerBySpringCGLIB$$e5c3a047.jCacheCacheManager(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 47 common frames omitted
Run Code Online (Sandbox Code Playgroud)

看来Spring Boot已开始搜索ehcache.xml使用ServletContext解析器。

ps:除了将Spring Boot升级到1.5.13之外,我没有任何源代码更改

我在这里缺少一些必需的配置吗?

spring-boot spring-cache

3
推荐指数
1
解决办法
692
查看次数

Spring Cache:根据条件强制更新缓存

我有一个需要缓存的作业列表(按 id)。然而,在某些情况下,拥有最新版本的作业很重要,并且需要绕过缓存(强制更新)。发生这种情况时,新获取的作业应放置在缓存中。

我是这样实现的:

@Cacheable(cacheNames = "jobs", key = "#id", condition = "!#forceRefresh", sync = true)
public Job getJob(String id, boolean forceRefresh) {
    // expensive fetch
}
Run Code Online (Sandbox Code Playgroud)

期望的行为:

  • getJob("123", false) => 返回作业 v1(如果存在,则从缓存中获取)
  • getJob("123", true) => 返回作业 v2(更新版本,从数据库中获取)
  • getJob("123", false)=> 返回作业 v2(更新版本,从缓存中获取

实际上,最后一次调用getJob("123", false)返回 job v1,即陈旧版本。似乎第二次调用(强制更新)不会更新缓存中的值。

我怎样才能在这里实现正确的行为?

缓存配置(使用咖啡因):

CaffeineCache jobs = new CaffeineCache("jobs", Caffeine.newBuilder()
        .expireAfterWrite(1, TimeUnit.MINUTES)
        .maximumSize(100)
        .build());
Run Code Online (Sandbox Code Playgroud)

spring caching spring-boot spring-cache caffeine

3
推荐指数
1
解决办法
1153
查看次数