spring 缓存(spring自带Cache)(入门)源码解读

spring自带的缓存类有两个基础类:Cache(org.springframework.cache.Cache)类,CacheManager(org.springframework.cache.CacheManager)类;

一(核心):

Cache类:定义了缓存常用的操作;

CacheManager类:spring 核心的缓存管理类,spring通过接口CacheManager来管理Cache(缓存);

二:

ConcurrentMapCacheFactoryBean(org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean):

    工厂类,通过此类可以创建Cache对象;

SimpleCacheManager(org.springframework.cache.support.SimpleCacheManager):

    继承自CacheManager,可以用来管理Cache(还有其它类继承自CacheManager类,有相同的功能);

实例:

------------------------------------------------------------------------------------------------------------

   通过以上四个基础类,就可以实现缓存的配置,配置如下:

   <!--缓存配置-->

   <!--启用缓存注解功能-->

   <cache:annotation-driven cache-manager="cacheManger"/>

  <!--spring自己的基于java.util.concurrent.ConcurrentHashMap实现的缓存管理器(该功能是从Spring3.1开始提供)-->

   <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">

     <property name="caches">

        <set>

          <bean name="myCache" class="org.springframework.cache.concurrent.ConcurrenMapCacheFactoryBean"/>

          <bean name="myCache1"class=" org.springframework.cache.concurrent.ConcurrenMapCacheFactoryBean"/>

        </set>

      </property>  

    </bean>

------------------------------------------------------------------------------------------------------------

service层的使用可以翻看http://www.cnblogs.com/zqsky/p/5867878.html。

注意:

-------------------------------------------注意1----------------------------------------------------------

<cache:annotation-driven/>只会去寻找定义在同一个ApplicationContext下的@Cacheable等缓存注解。

-------------------------------------------注意2---------------------------------------------------------

学会缓存的基本使用只是入门,各种使用技巧只有在项目中亲身体验才算真正学会。

原文地址:https://www.cnblogs.com/zqsky/p/5882513.html