文档章节

Spring cache abstraction

hantsy
 hantsy
发布于 2013/12/29 21:13
字数 392
阅读 196
收藏 0

#Spring cache abstraction

Spring provides its own cache solution in 3.x. And it defines a CacheManager SPI for third party cache solutions. By default, in the Spring core, it provides two built-in implementations, which based on JDK ConcurrentMap and Ehcache.

ConcurrentMap is an in-memory cache solution, it is useful in the development stage. Ehcache integrates the Ehcache as backend cache provider.

##Enable Cache

We still use Ehcache in this example.

Add the following content in the Spring XML configuration.

<pre> &lt;cache:annotation-driven mode="aspectj" /> </pre>

Or add @EnableCaching(mode=AdviceMode.ASPECTJ) annotation on the configuration class if you are using Java based configuration.

<pre> @Configuration ... @EnableCaching(mode=AdviceMode.ASPECTJ) public class JpaConfig {...} </pre>

Then, specify a CacheManager provider in your configuration.

<pre> &lt;bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache" /> &lt;!-- EhCache library setup --> &lt;bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" /> &lt;!-- p:config-location="ehcache.xml" --> </pre>

The equivalent Java configuration.

<pre> @Bean public CacheManager cacheManager() { return new EhCacheCacheManager(ehcache().getObject()); } @Bean public EhCacheManagerFactoryBean ehcache() { EhCacheManagerFactoryBean ehcache = new EhCacheManagerFactoryBean(); //ehcache.setConfigLocation(new ClassPathResource("ehcache.xml")); return ehcache; } </pre>

Do not forget to add the ehcache dependency in your Maven pom.xml file.

<pre> &lt;dependency> &lt;groupId>net.sf.ehcache&lt;/groupId> &lt;artifactId>ehcache-core&lt;/artifactId> &lt;/dependency> </pre>

##Use Cache

Spring provides some annotations for applying the cache in your classes, @Cacheable, @CachePut, @CacheEvict, the former two are use for creating cache, and the last is for remove caches. @CachePut always replace the current cache value even there is no change.

<pre> @Repository public class JpaConferenceDaoImpl implements ConferenceDao { @Override @Cacheable(value="conference", key="#id") public Conference findById(Long id) { ... } @Override @CacheEvict(value="conference", allEntries=true) public void deleteAll() { ... } @Override @CachePut(value="conference", key="#result.id") public Conference findBySlug(String slug) { ... } } </pre>

##Summary

Compare to the JPA 2nd level cache, the Spring solutions has some advantages.

  • It is more flexible.

You can define which methods you create and evict caches. In JPA, the cache is created when the object is persisted, you can change the strategy. As the above example codes, creating cache happens when fetching a conference from database, and do nothing when saving a conference into database.

  • Spring cache can be used in any data persistence solutions. As the above example codes, we are using cache on methods, it does not depend on JPA at all, so cache can be used in Jdbc and other data persistence solution.

Besides the built-in cache implementations, Spring Data project provides CacheManager for GemFire and Redis. And JBoss infinispan(Maven artifact is infinispan-spring) also provides an CacheManager implementation.

The example codes are hosted on my Github account. The codes includes a sample configuration for Redis.

https://github.com/hantsy/spring-sandbox

© 著作权归作者所有

hantsy
粉丝 113
博文 88
码字总数 78079
作品 0
浦东
程序员
私信 提问
Spring Framework 3.1 RC1 发布

Spring 框架发布了 3.1 的首个 RC 版本,该版本已经完成了计划中的 3.1 正式版的所有特性: The environment abstraction and the environment profile mechanism. Comprehensive Java-base...

红薯
2011/10/13
1K
3
Spring Framework 4.3.1 和 4.2.7 发布

Spring Framework 4.3.1 和 4.2.7 发布了。 Spring Framework 4.3.1 更新内容: Bug [SPR-14356] - SockJs heartbeat during message send closes socket [SPR-14358] - Failure to resolve ......

淡漠悠然
2016/07/04
2K
2
Spring cache简单使用

Spring cache简单使用 前言 spring有一套和各种缓存的集成方式。类似于sl4j,你可以选择log框架实现,也一样可以实现缓存实现,比如ehcache,guava cache. 什么时候用缓存 首先,缓存是为了省...

庞陆阳
2016/10/28
41
0
Spring Framework 4.3.2 和 5.0.0.M1 发布

Spring Framework 4.3.2 和 5.0.0.M1 发布了,详情:Change Log Spring Framework 4.3.2 更新如下: Bug [SPR-10343] - ConfigurationClassParser needs to load annotations through sourc......

淡漠悠然
2016/07/29
8.3K
22
Spring Sync 1.0.0 Milestone 1 发布

Spring Sync 允许高效的,客户端和基于 Spring 服务器之间的数据交换和同步。 Spring Sync 1.0.0 Milestone 1 发布,此版本的依赖提供在 repo.spring.io milestone repository 的依赖容器 "o...

oschina
2014/10/23
1K
1

没有更多内容

加载失败,请刷新页面

加载更多

Mybatis Plus删除

/** @author beth @data 2019-10-17 00:30 */ @RunWith(SpringRunner.class) @SpringBootTest public class DeleteTest { @Autowired private UserInfoMapper userInfoMapper; /** 根据id删除......

一个yuanbeth
今天
4
0
总结

一、设计模式 简单工厂:一个简单而且比较杂的工厂,可以创建任何对象给你 复杂工厂:先创建一种基础类型的工厂接口,然后各自集成实现这个接口,但是每个工厂都是这个基础类的扩展分类,spr...

BobwithB
今天
4
0
java内存模型

前言 Java作为一种面向对象的,跨平台语言,其对象、内存等一直是比较难的知识点。而且很多概念的名称看起来又那么相似,很多人会傻傻分不清楚。比如本文我们要讨论的JVM内存结构、Java内存模...

ls_cherish
今天
4
0
友元函数强制转换

友元函数强制转换 p522

天王盖地虎626
昨天
5
0
js中实现页面跳转(返回前一页、后一页)

本文转载于:专业的前端网站➸js中实现页面跳转(返回前一页、后一页) 一:JS 重载页面,本地刷新,返回上一页 复制代码代码如下: <a href="javascript:history.go(-1)">返回上一页</a> <a h...

前端老手
昨天
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部