Spring JCache support
Spring 4 provides seamless JCache integration.
Enable Caching
In this example, use EhCache as JCache specification provider.
Add
@EnableCaching(mode=AdviceMode.ASPECTJ) annotation on the configuration class if you are using Java based configuration.@Configuration
...
@EnableCaching(mode=AdviceMode.ASPECTJ)
public class JpaConfig {...}
Then, specify a
CacheManager provider in your configuration.@Override
@Bean
public CacheManager cacheManager() {
JCacheCacheManager cacheManager = new JCacheCacheManager();
cacheManager.setCacheManager(new JCacheManager(
new JCacheCachingProvider(), ehcache(), null, null));
return cacheManager;
}
private net.sf.ehcache.CacheManager ehcache() {
return new net.sf.ehcache.CacheManager();
}
@Override
@Bean
public KeyGenerator keyGenerator() {
return new SimpleKeyGenerator();
}
Do not forget to add the ehcache-core, jcache, cache-api dependencies in your Maven pom.xml file.
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> </dependency> <dependency> <groupId>org.ehcache</groupId> <artifactId>jcache</artifactId> </dependency> <dependency> <groupId>javax.cache</groupId> <artifactId>cache-api</artifactId> </dependency>
Use JCache API
Like Spring core, JCache APIs provides a series of annotations for cache operations.
@Repository
public class JpaConferenceRepositoryImpl implements ConferenceRepository {
private static final Logger log = LoggerFactory
.getLogger(JpaConferenceRepositoryImpl.class);
@PersistenceContext
private EntityManager entityManager;
@Override
@CacheResult(cacheName = "conference")
public Conference findById(Long id) {
return (Conference) entityManager.find(Conference.class, id);
}
@Override
@CachePut(cacheName = "conference" )
public Conference save(final Conference conference) {
if (conference.isNew()) {
entityManager.persist(conference);
entityManager.flush();
return conference;
} else {
Conference conf = entityManager.merge(conference);
entityManager.flush();
return conf;
}
}
@Override
@CacheRemove(cacheName = "conference" )
public void delete(final Long id) {
entityManager.remove(entityManager.find(Conference.class, id));
entityManager.flush();
}
@Override
@CacheRemove(cacheName = "conference" )
public void delete(final Conference conf) {
entityManager.remove(entityManager.merge(conf));
entityManager.flush();
}
@Override
@CacheRemoveAll(cacheName = "conference")
public void deleteAll() {
List<Conference> all = entityManager.createQuery("from Conference",
Conference.class).getResultList();
for (Conference c : all) {
delete(c);
}
entityManager.flush();
}
@Override
@CacheResult(cacheName = "conference")
public Conference findBySlug(String slug) {
List<Conference> all = entityManager
.createQuery("from Conference where slug=:slug",
Conference.class).setParameter("slug", slug)
.getResultList();
if (!all.isEmpty()) {
return all.get(0);
}
return null;
}
}
Spring JCache support can reuse the Spring Caching configuration, but use JCache APIs in codes..
Code sample
Check out the sample codes.
And explore the spring-jcache folder.
评论