缓存管理
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等缓存配置在一起.