介绍 Spring 3.1 M1 中的缓存功能
2012-09-30 01:16
453 查看
该版本最酷的新特性就是引入全方位的缓存支持。Spring 3.1 提供了对已有的 Spring 应用增加缓存的支持,这个特性对应用本身来说是透明的,通过缓存抽象层,使得对已有代码的影响降低到最小。
该缓存机制针对于 Java 的方法,通过给定的一些参数来检查方法是否已经执行,Spring 将对执行结果进行缓存,而无需再次执行方法。
可通过下列配置来启用缓存的支持(注意使用新的schema):
[html] view plaincopy<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
<cache:annotation-driven />
</beans>
接下来可使用 @Cacheable 和 @CacheEvict 来对缓存进行操作。
[Java] view plaincopy@Cacheable("persons")
public Person profile(Long personId) { ... }
以上代码声明了一个名为 persons 的缓存区域,当调用该方法时,Spring 会检查缓存中是否存在 personId 对应的值。
也可以指定多个缓存区域,当你在应用有需要这样做的话:
[Java] view plaincopy@Cacheable({"persons", "profiles"})
public Person profile(Long personId) { ... }
当指定多个区域时,Spring 会一个个的检查,一旦某个区域存在指定值时则返回。
而 @CacheEvict 则用来从缓存中清除数据,例如:
[Java] view plaincopy@CacheEvict (value = "persons", allEntries=true)
public List<Person> listPersons()
@CacheEvict 可以指定清除缓存的条件。
还可以指定缓存的Key:
[Java] view plaincopy@Cacheable(value="persons", key="personId")
public Person profile(Long personId, Long groundId) { ... }
或者根据条件决定是否缓存:
[Java] view plaincopy@Cacheable(value="persons", condition="personId > 50")
public Person profile(Long personId) { ... }
缓存管理器的配置:
[XML] view plaincopy<!-- generic cache manager -->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="default"/>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="persons"/>
</set>
</property>
</bean>
基于 Ehcache 缓存的配置:
[XML] view plaincopy<bean id="cacheManager" class="org.springframework.cache.ehcache.EhcacheCacheManager" p:cache-manager="ehcache"/>
<!-- Ehcache library setup -->
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
[XML] view plaincopy
[XML] view plaincopy
该缓存机制针对于 Java 的方法,通过给定的一些参数来检查方法是否已经执行,Spring 将对执行结果进行缓存,而无需再次执行方法。
可通过下列配置来启用缓存的支持(注意使用新的schema):
[html] view plaincopy<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
<cache:annotation-driven />
</beans>
接下来可使用 @Cacheable 和 @CacheEvict 来对缓存进行操作。
[Java] view plaincopy@Cacheable("persons")
public Person profile(Long personId) { ... }
以上代码声明了一个名为 persons 的缓存区域,当调用该方法时,Spring 会检查缓存中是否存在 personId 对应的值。
也可以指定多个缓存区域,当你在应用有需要这样做的话:
[Java] view plaincopy@Cacheable({"persons", "profiles"})
public Person profile(Long personId) { ... }
当指定多个区域时,Spring 会一个个的检查,一旦某个区域存在指定值时则返回。
而 @CacheEvict 则用来从缓存中清除数据,例如:
[Java] view plaincopy@CacheEvict (value = "persons", allEntries=true)
public List<Person> listPersons()
@CacheEvict 可以指定清除缓存的条件。
还可以指定缓存的Key:
[Java] view plaincopy@Cacheable(value="persons", key="personId")
public Person profile(Long personId, Long groundId) { ... }
或者根据条件决定是否缓存:
[Java] view plaincopy@Cacheable(value="persons", condition="personId > 50")
public Person profile(Long personId) { ... }
缓存管理器的配置:
[XML] view plaincopy<!-- generic cache manager -->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="default"/>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="persons"/>
</set>
</property>
</bean>
基于 Ehcache 缓存的配置:
[XML] view plaincopy<bean id="cacheManager" class="org.springframework.cache.ehcache.EhcacheCacheManager" p:cache-manager="ehcache"/>
<!-- Ehcache library setup -->
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
[XML] view plaincopy
[XML] view plaincopy
相关文章推荐
- 介绍 Spring 3.1 M1 中的缓存功能
- 介绍 Spring 3.1 M1 中的缓存功能
- 介绍 Spring 3.1 M1 中的缓存功能
- 介绍 Spring 3.1 M1 中的缓存功能
- Spring 3.1 M1 中的缓存功能
- ASP.NET 2.0的页面缓存功能介绍
- ASP.NET 2.0的页面缓存功能介绍
- ASP.NET 2.0的页面缓存功能介绍
- Springmvc +Ehcache 缓存介绍
- Spring 3.1 中使用 @Cacheable 实现缓存
- ASP.NET 2.0的页面缓存功能介绍(转)
- spring-boot-klock-starter V1.1 主体功能重大更新内容介绍
- Jeesite增加spring-data-redis缓存功能以及相关配置
- 润乾报表集群缓存同步功能介绍
- 8 -- 深入使用Spring -- 5... Spring 3.1 新增的缓存机制
- ASP.NET 2.0的页面缓存功能介绍
- spring 缓存中@Cacheable、@CachePut和@CacheEvict介绍 以及#p0的含义
- spring集成redis实现缓存功能
- Spring缓存简单介绍(Spring Cache)
- spring整合ehcache 注解实现查询缓存,并实现实时缓存更新或删除 写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天