JAVA 缓存管理

缓存管理    
https://blog.csdn.net/zl_momomo/article/details/80403564
原理,在spring中配置一个bean,
import org.springframework.cache.support.SimpleCacheManager;
该bean内存List<Cache>格式,
Cache 内实质为一个名为缓存名的map,key为拦截值,value为对应的缓存

 

@EnableCaching 通过此配置 开启缓存管理配置

@EnableCaching注解是spring framework中的注解驱动的缓存管理功能。自spring版本3.1起加入了该注解。如果你使用了这个注解,那么你就不需要在XML文件中配置cache manager了,等价于 <cache:annotation-driven/> 。能够在服务类方法上标注@Cacheable

@Configuration
@EnableCaching
public class CacheConfiguration {

  @Bean
  public CacheManager cacheManager() {
    SimpleCacheManager cacheManager = new SimpleCacheManager();
    List<Cache> cacheList = new ArrayList<>();
    //数据字典缓存
    cacheList.add(new ConcurrentMapCache(sampleCache1));
    //多语言缓存
    cacheList.add(new ConcurrentMapCache(sampleCache2));
    //系统参数
    cacheList.add(new ConcurrentMapCache(sampleCache3));

    cacheManager.setCaches(cacheList);
    return cacheManager;
  }
}


等价于
 

<beans>
     <cache:annotation-driven/>
     <bean id="book" class="Book"/>
     <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
         <property name="caches">
             <set>
                 <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
                     <property name="name" value="sampleCache1"/>

                    <property name="name" value="sampleCache2"/>

                    <property name="name" value="sampleCache3"/>
                 </bean>
             </set>
         </property>
     </bean>
 </beans>

 

@Cacheable(value = "dictionary", key = "#dictType", unless = "#result == null")

value,拦截,名为dictionary的缓存map.key为拦截,此map内,key与此先等的值,
若此map内有,则返回,没有则查一下,然后塞入

 

 

此处的缓存存于内存中,本质是个map,

还可以于redis等缓存配置在一起.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值