Spring + Cache/Ehcache


Spring + Cache

http://haohaoxuexi.iteye.com/blog/2123030


@Cacheable

可以标记方法/类,表示该方法/类支持缓存。

该标签有三个属性:

value: 必须指定,方法返回时返回到哪个cache上

   @Cacheable("cache1")//Cache是发生在cache1上的
   public User find(Integer id) {
      return null;
   }
 
   @Cacheable({"cache1", "cache2"})//Cache是发生在cache1和cache2上的
   public User find(Integer id) {
      return null;
   }

key: 指定方法返回结果对应的key(key = "#参数名" / key = "#p参数index"),不指定时使用默认政策生成key

   @Cacheable(value="users", key="#id")
   public User find(Integer id) {
      return null;
   }
 
   @Cacheable(value="users", key="#p0")
   public User find(Integer id) {
      return null;
   }
 
   @Cacheable(value="users", key="#user.id")
   public User find(User user) {
      return null;
   }
 
   @Cacheable(value="users", key="#p0.id")
   public User find(User user) {
      return null;
   }

       key默认生成策略:

1、若方法没有参数,适用0作为key

2、若方法只有一个参数,则作为key

3、若方法有多于一个参数,则所有参数的hashcode作为key

除了方法参数,还可以通过一个root对象生成key

   @Cacheable(value={"users", "xxx"}, key="caches[1].name")  //省略了#root
   public User find(User user) {
      return null;
   }

condition: 指定发生的条件,条件为true/false,默认为空即缓存方法所有的返回结果

   @Cacheable(value={"users"}, key="#user.id", condition="#user.id%2==0")
   public User find(User user) {
      System.out.println("find user by user " + user);
      return user;
   }


@CachePut

使用@Cacheable标注的方法,每次都会从缓存中查找相同key的缓存元素,若存在就不执行该方法,直接返回缓存元素,否则才会执行将结果存入缓存。

@CachePut每次都会执行并将结果存入缓存。


@CacheEvict

标注在需要清楚缓存元素的类或方法上,可以指定的属性有value,key,condition,allEntries和beforeInnovation

allEntries: 是否需要清楚缓存中所有元素,默认false

beforeInnovation: 清除操作默认在对应方法执行之后,该属性为true时在方法执行前清除指定缓存


@Caching

可以在一个类或方法上标注多个Spring Cache注解,三个属性cacheable、put和evict分别指定前三种注解。

   @Caching(cacheable = @Cacheable("users"), 
	evict = { @CacheEvict("cache2"), @CacheEvict(value = "cache3", allEntries = true) })
   public User find(Integer id) {
      return null;
   }

</pre>自定义注解</p><p><pre name="code" class="java">@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Cacheable(value="users")
public @interface MyCacheable {
 
}

  @MyCacheable
   public User findById(Integer id) {
      System.out.println("find user by id: " + id);
      User user = new User();
      user.setId(id);
      user.setName("Name" + id);
      return user;
   }



Spring对Cache的支持

1、基于注解

    <cache:annotation-driven/>
cache标签有一个cache-manager属性指定当前所指定的Cache Manager的bean的名称,默认时cacheManager。

还有一个mode属性,可选proxy和aspectj,默认proxy

proxy:只有缓存方法被外部调用时才起作用;只有public方法上的cache注解才起作用

aspectj:没有以上两种限制


2、基于XML




配置CacheManager

CacheManager是Spring管理Cache的接口,spring本身提供了两种实现:一种时基于Java的CurrentHashMap,一种时基于第三方cache实现Ehcache

1、基于CurrentHashMap

 <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
      <property name="caches">
         <set>
            <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="xxx"/>
         </set>
      </property>
   </bean>


2、基于Ehcache

    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcacheManager"/>
    <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="ehcache-spring.xml"/>

ehcacheManager的默认位置是根路径下的ehcache.xml

   <!-- 定义CacheManager -->
   <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
      <!-- 指定配置文件的位置 -->
      <property name="configLocation" value="/WEB-INF/config/ehcache.xml"/>
      <!-- 指定新建的CacheManager的名称 -->
      <property name="cacheManagerName" value="cacheManagerName"/>
   </bean>


   <!-- 定义一个Ehcache -->
   <bean id="userCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
      <property name="cacheName" value="user"/>
      <property name="cacheManager" ref="cacheManager"/>
   </bean>



Ehcache

http://haohaoxuexi.iteye.com/blog/2112170

Ehcache是一个管理缓存的工具,可以缓存在内存里和硬盘上。核心是CacheManager,一个应用可以有多个CacheManager,一个CacheManager可以管理多个Cache,Cache内部保存一组Element,每个Element是一个<K, V>键值对。

特性: http://raychase.iteye.com/blog/1545906

1、快速轻量

6、应用持久化

7、监听器

8、开启JMX

9、分布式缓存


CacheManager负责Cache的创建、移除和访问。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章