我正在尝试@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
每次都执行方法.
我在这里做错了什么,或者这是有意的吗?
我开发了一个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
吗?
我已经实现了缓存,现在我想添加一个到期时间.
如何在春季启动时设置到期时间@Cacheable
?
这是一段代码:
@Cacheable(value="forecast",unless="#result == null")
Run Code Online (Sandbox Code Playgroud) 我正在使用Spring Cache抽象,我定义了多个缓存.有时,当数据发生变化时,我想要驱逐多个缓存.有没有使用Spring的@CacheEvict
注释驱逐多个缓存?
我希望在我的Web应用程序中配置多个Spring缓存管理器,并且我可以在项目的不同位置使用不同的缓存管理器.有没有办法做到这一点.
目前我正在使用Spring Cache和@Cacheable
/ @CacheEvict
annotations.
我想得到某种类似的控制台日志声明 "INFO: i got those values from the cache, NOT from the host. awesome"
有一个干净,简单的方法吗?slf4j
如果有任何兴趣,我们显然使用btw.
我正在使用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吗?
我有一个简单的sprint启动应用程序1.5.11.RELEASE
,@EnableCaching
在Application Configuration
类上使用spring boot .
<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)
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启动"dev"配置文件禁用开发期间的所有缓存.在application.properties中,没有seam作为一般设置来关闭它.什么是最简单的方法?
如何禁用特定方法的缓存?我试着像这样使用SpEl:
@Cacheable(value = "complex-calc", condition = "#{${spring.profiles.active} != 'dev'}")
public String someBigCalculation(String input){
...
}
但我可以让它发挥作用.关于SO的问题有几个问题,但它们指的是XML配置或其他东西,但我使用的是Spring Boot 1.3.3,它使用了自动配置.
我不想让事情过于复杂.
我试图从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-cache ×10
spring ×8
java ×6
caching ×3
spring-boot ×3
caffeine ×1
ehcache ×1
slf4j ×1
spring-4 ×1
spring-data ×1
spring-el ×1
testing ×1