spring的cache的注解方式的简单实用(具体步骤)

     1、启用注解模式

如果使用的是注解类,需要在注解类上添加@EnableCaching,如果是使用的配置文件

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:cache="http://www.springframework.org/schema/cache"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/cache

http://www.springframework.org/schema/cache/spring-cache.xsd

">

    <cache:annotation-driven /> <!-- 启用缓存 -->

   <bean id="cacheManager"

                 class="org.springframework.cache.concurrent.ConcurrentMapCacheManager" />

</beans>

2、配置CacheManager
Spring 3.1 内置了五个缓存管理器实现,如下:

        *SimpleCacheManager

        *NoOpCacheManger

        *ConcurrentMapCacheManager

         *CompositeCacheManger

        *EhCacheCacheManger

       Spring Data提供了两个缓存管理器:

             *RedisCacheManager(来自于Spring Data Redis项目)

             *GmfireCacheManager(来自于Spring Data GemFire项目)

在这里使用EhCacheCacheManger来实现缓存服务
配置缓存管理器实例,可以自行转换为xml中的配置

@Configuration

@EnableCaching//开启声明式缓存支持

public class RootConfig {

/**

* 注册实现CacheManager的bean

* EhCache的CacheManager要被注入到Spring的EhCacheCacheManager之中

* @param cacheManager 由下面的EhCacheManagerFactoryBean提供

* @return

*/

     @Bean

     public EhCacheCacheManager cacheCacheManager(CacheManager cacheManager){

            return new EhCacheCacheManager(cacheManager);

     }

 

       @Bean

        public EhCacheManagerFactoryBean ehcache(){

                 EhCacheManagerFactoryBean ehCacheManagerFactoryBean =

                        new EhCacheManagerFactoryBean();

                  ehCacheManagerFactoryBean.setConfigLocation(   new ClassPathResource("ehcahe.xml的配置文件所在地址")  );

                  return ehCacheManagerFactoryBean;

      }

}

   说明:Spring提供了EhCacheManagerFactoryBean来生成EhCache的CacheManager的方法。方法ehcache会创建并返回一个EhCacheManagerFactoryBean的实例。因为他是一个工厂bean(也就是说他实现了Spring的FactoryBean接口),所以在spring应用上下文中并不是EhCacheManagerFactoryBean 的示例,而是CacheManager的一个实例,因此适合注入到cacheCacheManager之中。

3、ehcache.xml文件的基本配置(具体配置可百度)

<ehcache>

      <cache name="spittleCache"

           maxBytesLocalHeap="50m" ---最大的堆存储为50m

            timeToLiveSeconds="100" ---存活时间100秒

   </ehcache>


4、spring 对Cache的注解支持

  Spring 提供了四个注解来声明缓存规则

         *@Cacheable 表明Spring在调用方法之前,首先应该在缓存中查找方法的返回值。如果这个值能够找到,就会返回缓存的的值,否则的话,这个方法就会被调用,返回值会放到缓存之中。

        *@CachePut :表明Spring应该将方法的返回值放到缓存中。在方法的调用前不会检查缓存,方法始终都会被调用。

        *@CacheEvict : 表明Spring应该在缓存中移除一个或多个条目。

        *@Caching : 这是一个分组的注解,能够同时应用多个其他的缓存注解。


5、简单示例

        @Cacheable(value="缓存空间的名称,xml中配置的" , key = "#spittle.id")--spittle是参数里面的spittle,如果不设置,就以参数作为key

        Spittle save(Spittle spittle);


  条件化缓存:

       @Cacheable(value="spittleCache" , unless="#result.message.contains('NoCache')")//当条件为true时,不保存对象

       Spittle findOne(Long id);

 

 移除缓存条目:

     @CacheEvict("spittleCache")

      void remove(Long spittled);

PS:总结比较粗糙,勿怪,主要备忘,有问题,留言,大家交流

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值