本文源码:GitHub·点这里 || GitEE·点这里java
EhCache是一个纯Java的进程内缓存框架,具备快速、上手简单等特色,是Hibernate中默认的缓存提供方。git
Hibernate三级缓存机制简介:github
一级缓存:基于Session级别分配一块缓存空间,缓存访问的对象信息。Session关闭后会自动清除缓存。spring
二级缓存:是SessionFactory对象缓存,能够被建立出的多个 Session 对象共享,二级缓存默认是关闭的,若是要使用须要手动开启,而且依赖EhCache组件。sql
三级缓存:查询缓存,配置开启该缓存的状况下,重复使用一个sql查询某个范围内的数据,会进行缓存。数据库
Ehcache:直接在Jvm虚拟机中缓存,速度快,效率高,不适合处理大规模缓存数据,在分布式环境下,缓存数据共享操做复杂;segmentfault
Redis:做为独立的缓存中间件,在分布式缓存系统中很是好用,缓存数据共享,有效支撑大量数据缓存,支持哨兵模式,或者集群模式的高可用成熟方案;跨域
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency>
基础配置缓存
spring: cache: ehcache: config: classpath:ehcache.xml
启动类注解架构
@EnableCaching @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args) ; } }
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> <!-- 操做系统缓存的临时目录,内存满后写入该目录 --> <diskStore path="java.io.tmpdir"/> <defaultCache maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </defaultCache> <cache name="userEntity" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </cache> </ehcache>
配置参数说明
maxElementsOnDisk:磁盘缓存中最多能够存放的元素数量;
eternal:缓存中对象是否永久有效;
timeToIdleSeconds:当eternal=false时使用,缓存数据有效期(单位:秒),时间段内没有访问该元素,将被清除;
timeToLiveSeconds:缓存数据的存活时间;
maxElementsInMemory:内存中最多能够存放的元素数量,overflowToDisk=true,则会将Cache中多出的元素放入磁盘文件中,若overflowToDisk=false,则根据memoryStoreEvictionPolicy策略替换Cache中原有的元素;
diskExpiryThreadIntervalSeconds:磁盘缓存的清理线程运行间隔;
memoryStoreEvictionPolicy:缓存释放策略,LRU会优先清理最少使用的缓存;
localTempSwap:持久化策略,当堆内存或者非堆内存里面的元素已经满了的时候,将其中的元素临时的存放在磁盘上,重启后就会消失;
@Service public class CacheService { private static final Logger LOGGER = LoggerFactory.getLogger(CacheService.class); @Resource private UserMapper userMapper ; @Cacheable(value="userEntity") // 在缓存有效期内,首次查询才访问数据库 public UserEntity getById (Integer id){ // 经过日志,标识方法是否执行 LOGGER.info("getById..."+id); return userMapper.selectById(id) ; } @CacheEvict(value="userEntity",key = "#id") //该ID数据更新,清空该ID缓存 public void updateUser(Integer id) { UserEntity user = new UserEntity() ; user.setId(id); user.setUserName("myCache"); userMapper.updateById(user); } }
@Cacheable:注解标记在一个方法上,也能够标记在一个类上,标记在一个方法上表示该方法支持缓存,该方法被调用后将其返回值缓存起来,下次一样的请求参数执行该方法时能够直接从缓存中获取结果,而不须要再次执行该方法。
@CacheEvict:注解标记在须要清除缓存元素的方法或类上的,当标记在一个类上时表示其中全部的方法的执行都会触发缓存的清除操做,而且能够按照指定属性清除。
GitHub·地址 https://github.com/cicadasmile/middle-ware-parent GitEE·地址 https://gitee.com/cicadasmile/middle-ware-parent
推荐阅读:SpringBoot进阶系列