问题标签 [spring-cache]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
2779 浏览

java - 当涉及驱逐时,Spring 的缓存注释是否可以安全使用?

我正在使用 Spring 4.0.3,并利用 EhCache 2.8.1 支持的缓存抽象功能。

我担心方法级别的注释在编辑@Cacheable@CacheEvict可能无法正确锁定缓存,从而导致线程安全问题。我下面的测试似乎证实了这一点。我的问题是,我是在滥用框架还是误解了我的测试结果?或者我得出的结论是正确的,注释不会导致缓存被正确锁定,因此使用@CacheEvict不能保证未来的读取将是有效的?

测试:

创建一个数据库表来存储一个计数器值

在 ehcache.xml 中创建一个条目来缓存计数器值

在 Spring 控制器中创建两种请求映射方法——一种是读取计数器的值并在长时间延迟后返回,另一种是递增值然后快速返回

在两种不同的场景下遵循这三个步骤——首先从控制器方法中删除缓存注释,然后将它们添加回

  1. 访问/incrementcounter
  2. 在一个浏览器选项卡中,访问/viewcounter,然后在发起此请求后立即/incrementcounter在另一个选项卡 中访问
  3. 访问/viewcounter

试验结果

缓存结果是错误的,是吗?

0 投票
2 回答
41018 浏览

spring - 如何在 Spring Data 存储库上测试 Spring 的声明式缓存支持?

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

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

我遇到的问题是我想编写一个集成测试(针对 hsqldb),断言结果是第一次从 db 中检索,第二次从缓存中检索。

我最初想嘲笑 jpa 基础设施(实体管理器等),并以某种方式断言实体管理器没有被第二次调用,但它似乎太难/太麻烦了(参见https://stackoverflow.com/a/23442457/536299)。

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

0 投票
1 回答
545 浏览

caching - Fine-tuning and monitoring a Spring cache backed by a ConcurrentMapCache

I have set up a Spring cache manager backed by a ConcurrentMapCache for my application.

I am seeking for ways to monitor the cache and especially make sure the data in cache fits in memory. I considered using jvisualvm for that purpose but there might be other ways... If so what are they?

So my question is basically twofold:

  • What is the best way to monitor a cache backed by a ConcurrentMapCache?
  • What are the general guidelines for setting the time to live and cache size values of a cache?
0 投票
1 回答
3846 浏览

mockito - Issues with testing Spring's declarative caching support on a Spring data repository

I am trying to test Spring caching support (as described here) for a Spring Data JPA repository (that extends JpaRepository) and I am actually having issues with my configuration.

Here is my repository method:

Here is my CacheConfiguration:

Here is my test:

By running the above test, I noticed that a object of type SimpleKey is passed as the key and a null as the value to ConcurrentMapCache:

In my case the store is of type: com.google.common.cache.LocalCache and the put method has a null check resulting in a NPE....

Here is the stack trace:

0 投票
1 回答
1733 浏览

aspectj - Spring 的 AspectJ 模式缓存与 AspectJ 模式事务

我的问题与 Spring 的 AspectJ 模式有关,尤其是如何启用它:

  1. 事务管理
  2. 缓存

1) 我注意到为了启用 AspectJ 模式进行事务管理,我只需要执行以下操作:

2) 而为了使用 AspectJ 模式进行缓存,似乎必须:

- 将以下 jar 放入 Tomcat 的 lib 目录: -org.springframework:spring-instrument-tomcat 在 Tomcat 的 server.xml 中添加以下行:

- 添加以下配置:

- 最终能够使用 AspectJ 模式,如下所示:

以上是对的吗?如果是这样,为什么 AspectJ 模式缓存与 AspectJ 模式事务支持不同?

0 投票
3 回答
3543 浏览

java - spring 3中方法执行后如何强制@Cacheable工作?

我有将实体保存到数据库中的方法。它是:

我希望结果Employee由 key = idof缓存Employee。在方法运行id后分配员工。entityManager,persist我知道这@Cacheable是因为 AOP 而起作用。默认情况下,它在方法运行employee之前存储到缓存中。createEmployee

方法执行后如何强制org.springframework.cache.annotation.@Cacheable存储到缓存中?

堆栈跟踪

0 投票
1 回答
3533 浏览

java - spring @Cacheable 与 Ehcache,拼写为有效对象找到 null

我有一个类似的问题,但有时它有效。所描述的错误只会偶尔发生一次。

我正在使用 spring 3.2.5 和 ehcache 2.6.5。

异常跟踪:

我的缓存代码如下:

发生这种情况时:当我将 jenkins 配置为构建并自动部署到 tomcat7 / 当我使用 maven 在 eclipse WS 中构建并部署到 tomcat7 时。

当它完美运行时:在它失败一次后,如果我只用一些空格编辑 MailFilterServiceImpl.java 以使其在 eclipse 中重新编译并重新启动 tomcat7 服务器。

我需要让它在 CI 场景中工作。

[更新] 设置编译器选项 debug:true 可以解决这个问题。优化后似乎没有任何发言权。

0 投票
2 回答
1135 浏览

spring-cache - Spring 声明式缓存和空值处理

我已经设置了 Spring 声明式缓存,但我不确定如何处理 null 值,我正在寻求建议和最佳实践以正确使用我的缓存。

我有以下 Spring Data Repository 方法,null如果没有Member找到传入参数的电子邮件,则返回:

当然,返回的空值会导致NullPointerException如下:

我应该:

  • 配置我ConcurrentMapCache以允许空值(设置allowNullValues为真)并称之为一天?
  • 我错过了什么吗,有没有更好的处理方式?

欢迎任何意见和建议。

0 投票
1 回答
3706 浏览

spring - Spring 4 缓存键生成未按预期工作

将我们的 spring 升级到版本 4 后,我们的自定义密钥生成器停止工作。在迁移之前,我们执行了覆盖“generate”方法的代码,但是迁移到spring 4.0.5之后,代码根本没有执行。相反,我看到 SimpleKeyGenerator 是始终执行的那个。这是春天的虫子吗?为什么我不能像以前的版本那样用自己的代码覆盖 generate 方法?

来自根上下文的样本:

来自 java 密钥生成的示例(迁移之前)

}

迁移后的示例代码

}

附加信息:调试代码后,我看到每次调用“生成”方法时,它仅在 SimpleKeyGenerator 中执行,而不在我的自定义 CacheKeyGenerator 类中执行。我试图理解为什么,所以我做了一些调试。在调试时,我看到有 org.springframework.cache.interceptor.CacheAspectSupport 类,它有一个私有属性: private KeyGenerator keyGenerator = new SimpleKeyGenerator(); 这个类在 keyGenerator 属性上有一个 setter 方法,我看到当上下文启动时,这个 setter 方法是用我自定义的 CacheKeyGenerator 调用的,所以我断定我的配置是正确的,问题不在配置中。我还看到,当需要生成密钥时,keyGenerator 属性“丢失”了“CacheKeyGenerator”值并具有“

0 投票
0 回答
3126 浏览

java - Spring Autowire CacheManager

尝试 Autowire CacheManager 并抛出空指针。本质上,喜欢使用 Spring 的缓存管理器,而实际实现是由 ehcache 实现的。这个想法是将来交换实际的缓存提供程序,以写入spring的接口。所以,这里是配置。

在服务类中,尝试注入。

问题是“cacheMgr”变得空了。不确定问题出在哪里。