使用Guava配置Spring缓存 [英] Config Spring cache using Guava

查看:322
本文介绍了使用Guava配置Spring缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关注关于缓存的春季文档我可以在项目中使用缓存,但是如何配置guava来定义每个缓存名称的过期时间或大小?

Following the spring documentation about cache I could use cache on my project, but how can I configure guava to define a expired time or size per cache name?

applicationConfig.xml

applicationConfig.xml

<bean id="cacheManager" class="org.springframework.cache.guava.GuavaCacheManager"/>

Foo.java

@Cacheable(value="courses", key="#user.id")
public List<Course> getCoursesByUser(User user) {
    ...
}


推荐答案

您可以单独配置缓存。请参阅 Spring Guava缓存

You can configure caches separately. See Spring Guava cache

@Bean
public CacheManager cacheManager() {
    SimpleCacheManager simpleCacheManager = new SimpleCacheManager();
    GuavaCache bookCache = new GuavaCache("book", CacheBuilder.newBuilder().build());
    GuavaCache booksExpirableCache = new GuavaCache("books", CacheBuilder.newBuilder()
            .expireAfterAccess(30, TimeUnit.MINUTES)
            .build());
    simpleCacheManager.setCaches(Arrays.asList(bookCache, booksExpirableCache));
    return simpleCacheManager;
}

这篇关于使用Guava配置Spring缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆