标签: spring-cache

从同一个类中调用时,忽略Spring缓存@Cacheable方法

我正在尝试@Cacheable从同一个类中调用一个方法:

@Cacheable(value = "defaultCache", key = "#id")
public Person findPerson(int id) {
   return getSession().getPerson(id);
} 

public List<Person> findPersons(int[] ids) {
   List<Person> list = new ArrayList<Person>();
   for (int id : ids) {
      list.add(findPerson(id));
   }
   return list;
}
Run Code Online (Sandbox Code Playgroud)

并希望结果findPersons也被缓存,但@Cacheable注释被忽略,并且findPerson每次都执行方法.

我在这里做错了什么,或者这是有意的吗?

spring caching spring-cache

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

如何测试Spring对Spring Data存储库的声明性缓存支持?

我开发了一个MemberRepository扩展的Spring Data存储库接口org.springframework.data.jpa.repository.JpaRepository.MemberRepository有一个方法:

@Cacheable(CacheConfiguration.DATABASE_CACHE_NAME)
Member findByEmail(String email);
Run Code Online (Sandbox Code Playgroud)

结果由Spring缓存抽象缓存(由a支持ConcurrentMapCache).

我的问题是,我想要写一个集成测试(针对HSQLDB)断言结果被从数据库第一次检索,并从缓存中的第二次.

我最初想过嘲笑jpa基础设施(实体经理等),并以某种方式断言实体经理第二次没有被调用但看起来太难/笨重(参见/sf/answers/1640972021/).

那么有人可以提供有关如何测试带有注释的Spring Data Repository方法的缓存行为的建议@Cacheable吗?

testing spring spring-data spring-data-jpa spring-cache

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

到期时间@cacheable spring boot

我已经实现了缓存,现在我想添加一个到期时间.

如何在春季启动时设置到期时间@Cacheable

这是一段代码:

@Cacheable(value="forecast",unless="#result == null")
Run Code Online (Sandbox Code Playgroud)

java caching spring-cache

30
推荐指数
5
解决办法
4万
查看次数

Spring Cache:Evict多个缓存

我正在使用Spring Cache抽象,我定义了多个缓存.有时,当数据发生变化时,我想要驱逐多个缓存.有没有使用Spring的@CacheEvict注释驱逐多个缓存?

java spring spring-cache

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

如何在spring cache java中配置多个缓存管理器

我希望在我的Web应用程序中配置多个Spring缓存管理器,并且我可以在项目的不同位置使用不同的缓存管理器.有没有办法做到这一点.

java spring caching spring-boot spring-cache

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

Spring缓存登录@Cacheable命中

目前我正在使用Spring Cache和@Cacheable/ @CacheEvictannotations.

我想得到某种类似的控制台日志声明 "INFO: i got those values from the cache, NOT from the host. awesome"

有一个干净,简单的方法吗?slf4j如果有任何兴趣,我们显然使用btw.

java spring slf4j spring-cache

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

带有项目/实体集合的Spring Cache

我正在使用Spring Cache,我在其中传递了一组键,返回是一个实体列表.我想让缓存框架理解返回列表中的每个元素都要使用相应的代码进行缓存.目前,似乎关键是整个列表,如果我在后续调用中缺少一个键,它将尝试再次重新加载整个集合.

@Override
@Cacheable(value = "countries")
public List<Country> getAll(List<String>codes) {
    return countryDao.findAllInCodes(codes);
}
Run Code Online (Sandbox Code Playgroud)

另一种可能性是返回是一个映射,同样我希望缓存足够智能,只能查询以前从未查询过的项目,也可以用它的密钥缓存每个项目.

@Override
@Cacheable(value = "countries")
public Map<String,Country> getAllByCode(List<String>codes) {
    return countryDao.findAllInCodes(codes);
}
Run Code Online (Sandbox Code Playgroud)

假设国家类看起来像这样:

class Country{
  String code; 
  String fullName;
  long id;

... // getters setters constructurs etc.. 
}
Run Code Online (Sandbox Code Playgroud)

这可以用Spring Cache吗?

java spring spring-cache

17
推荐指数
1
解决办法
1万
查看次数

是否可以在弹簧启动时使用咖啡因为每个缓存设置不同的规格?

我有一个简单的sprint启动应用程序1.5.11.RELEASE,@EnableCaching在Application Configuration类上使用spring boot .

的pom.xml

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
 </dependency>
 <dependency>
            <groupId>com.github.ben-manes.caffeine</groupId>
            <artifactId>caffeine</artifactId>
 </dependency>
Run Code Online (Sandbox Code Playgroud)

application.properties

spring.cache.type=caffeine
spring.cache.cache-names=cache-a,cache-b
spring.cache.caffeine.spec=maximumSize=100, expireAfterWrite=1d
Run Code Online (Sandbox Code Playgroud)

我的问题很简单,如何为每个缓存指定不同的大小/到期时间.也许它可能cache-a是有效的1 day.但cache-b也许可以1 week.关于咖啡因缓存的规范似乎是全局的,CacheManager而不是Cache.我错过了什么吗?也许我的用例有更合适的提供商?

spring-boot spring-cache caffeine

17
推荐指数
3
解决办法
3857
查看次数

Spring Boot - 如何在开发期间禁用@Cachable?

我正在寻找两件事:

  1. 如何使用Spring启动"dev"配置文件禁用开发期间的所有缓存.在application.properties中,没有seam作为一般设置来关闭它.什么是最简单的方法?

  2. 如何禁用特定方法的缓存?我试着像这样使用SpEl:

    @Cacheable(value = "complex-calc", condition = "#{${spring.profiles.active} != 'dev'}") public String someBigCalculation(String input){ ... }

但我可以让它发挥作用.关于SO的问题有几个问题,但它们指的是XML配置或其他东西,但我使用的是Spring Boot 1.3.3,它使用了自动配置.

我不想让事情过于复杂.

java spring spring-el spring-boot spring-cache

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

Spring框架中的ehcache配置

我试图从RSS源加载一些上下文,并在春天使用ehcache库将其作为缓存传递给客户端.这是我的代码:

    import org.springframework.cache.annotation.Cacheable;
@Service
public class GlossaryReaderService {

    @Cacheable(value = "glossaryList")
    public List<Glossary> readGlossary(String url) {

        XmlReader reader = null;
        List<Glossary> extractedGlossay = new ArrayList<Glossary>();
        SyndEntry entry;
        SyndContent desc;
        Glossary glossaryList = null;
        try {
            String decodedURL = URLDecoder.decode(url, "UTF-8");
            reader = new XmlReader(new URL(decodedURL));
            SyndFeed feed = new SyndFeedInput().build(reader);

            for (Iterator i = feed.getEntries().iterator(); i.hasNext();) {
                entry = (SyndEntry) i.next();
                desc = entry.getDescription();
                if (desc != null) { ...
                        extractedGlossay.add(glossaryList);
                    }
                }
            }

        } catch (IOException | IllegalArgumentException | …
Run Code Online (Sandbox Code Playgroud)

spring ehcache spring-cache spring-4

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