springboot 缓存技术之 ehcache

1、pom文件引入ehcache

<!-- Spring Boot 缓存支持启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- Ehcache 坐标 -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>

2、复制jar包中ehcache-failsafe.xml 文件放在src/main/resources下。名字随意起

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">


<diskStore path="java.io.tmpdir"/>
  <!--默认配置 -->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache>
  <!--自定义配置 name唯一标识 具体属性有兴趣的可自查,就不详细说明了 -->
    <cache name="student"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</cache>
</ehcache>
并在 application.properties 中配置路径
spring.cache.ehcache.config=classpath:ehcache.xml
3、目录结构

4、使用

//启动器

package com.zjx;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
//配置应用是否要缓存
@EnableCaching
public class SpringbootehcacheApplication {

public static void main(String[] args) {
SpringApplication.run(SpringbootehcacheApplication.class, args);
}

}
//service
package com.zjx.service;

import com.zjx.mapper.StudentMapper;
import com.zjx.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {
@Autowired
StudentMapper studentMapper;
  //@CacheEvict 清除指定value中的缓存 。 必须指定要清除的对象。否则不起作用 一般在新增 删除 更新用
@CacheEvict(value="student",allEntries = true)
public void add(Student student){

this.studentMapper.add(student);
}

public List<Student> findByName(Student student){

return this.studentMapper.findByName(student);
}

@CacheEvict(value="student",allEntries = true)
public void del(Student student){

this.studentMapper.delete(student);
}
  //@Cacheable (value)指定使用默认缓存配置或自定义配置。
@Cacheable(value = "student")
public List<Student> allStudent(){

return this.studentMapper.findAll();
}


}
5、测试用例
package com.zjx;

import com.zjx.pojo.Student;
import com.zjx.service.StudentService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootehcacheApplicationTests {

@Autowired
StudentService studentService;
@Test
public void contextLoads() {
}

@Test
public void getStudent(){
//第一次
System.out.println(this.studentService.allStudent().size());

this.studentService.add(new Student("aa","24","男","华阳大道"));
this.studentService.del(new Student("cc","","",""));
this.studentService.del(new Student("bb","","",""));
this.studentService.del(new Student("aa","","",""));

//第二次
System.out.println(this.studentService.allStudent().size());
}

}
以上是自学总结。有借鉴前辈帖子望见谅。不足之处请多多指教 
 


猜你喜欢

转载自www.cnblogs.com/yixingzhou/p/11139605.html