说到在项目中使用缓存,相信很多人会到redis,当然这根据需求和整体项目的情况而定,但是有一个缓存在叫Ehcache,在我的一篇博客中已使用过Ehcache作为Mybatis的二级缓存使用过了
https://blog.csdn.net/qq_37432174/article/details/95796577
这里在SpringBoot使用SpringCache实现Ehcache缓存。
依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.6</version>
</dependency>
</dependencies>
ehcache.xml
在resources目录下,新建ehcache.xml,当然你可以取其他名称,但是你必须在application.properties中指出配置文件名
spring.cache.ehcache.config=classpath:zsl.xml
<!--
name:缓存名称。
maxElementsInMemory:缓存最大个数。
eternal:对象是否永久有效,一但设置了,timeout将不起作用。
timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。
diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
maxElementsOnDisk:硬盘最大缓存个数。
diskPersistent:是否缓存虚拟机重启期数据。
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
clearOnFlush:内存数量最大时是否清除。
diskStore 则表示临时缓存的硬盘目录。
-->
<ehcache>
<diskStore path="java.io.tmpdir/shiro-spring-sample"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>
<cache name="user"
maxElementsInMemory="10000"
eternal="true"
overflowToDisk="true"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="600"/>
</ehcache>
使用@EnableCaching在启动类上,开启缓存
@SpringBootApplication
@EnableCaching
public class Ehcache02Application {
public static void main(String[] args) {
SpringApplication.run(Ehcache02Application.class, args);
}
}
接下来的代码和我之前写的SpringBoot___使用Spring Cache实现Redis缓存是一样的
https://blog.csdn.net/qq_37432174/article/details/99607549
/**
* @author ZSL
* @ClassName KeyConfig
* @description
* @date 2019/8/12
*
* @CacheConfig(cacheNames = "c1"):可以加在类上也可以加在具体的方法上
*
* 加在类上表示:类中所有方法使用的缓存名称
*
* 加在方法上表示:该方法中使用的缓存名称
*/
@CacheConfig(cacheNames = "user")
@Component
public class KeyConfig {
@Autowired
private ChcaheService chcaheService;
/**
* 查询
* @param id
* @param name
* @return
*
* 默认情况下,缓存的 key 就是方法的参数,缓存的 value 就是方法的返回值
*
*当有多个参数时,默认就使用多个参数来做 key ,如果只需要其中某一个参数做 key ,
* 则可以在 @Cacheable 注解中,通过 key 属性来指定 key
*/
@Cacheable(key = "#id")
public User getUser(Integer id,String name){
User user = chcaheService.getUser(id, name);
System.out.println("getUser:"+user);
return user;
}
/**
* 使用自定义的key
*
* myKeyGenerator
*
* @param id
* @return
*/
@Cacheable(keyGenerator = "myKeyGenerator")
public User getUserById(Integer id) {
User user = new User();
user.setId(id);
user.setName("zsl");
System.out.println(user);
return user;
}
/**
* 新增
* @param user
* @return
*
* 当数据库中的数据更新后,缓存中的数据也要跟着更新,
* 使用该注解,可以将方法的返回值自动更新到已经存在的 key 上
*
*/
@CachePut(key = "#user.id")
public User updUser(User user){
System.out.println("updUser:"+user);
return user;
}
/**
* 删除
* @param id
*
* 当数据库中的数据删除后,相关的缓存数据也要自动清除,
* 该注解在使用的时候也可以配置按照某种条件删除( condition 属性)
* 或者配置清除所有缓存( allEntries 属性)
*/
@CacheEvict
public void delUser(Integer id){
System.out.println("delUser:"+id);
}
}
/**
* @author ZSL
* @ClassName MyKeyGenerator
* @description
* @date 2019/8/14
*
* 自定义key
*/
@Component
public class MyKeyGenerator implements KeyGenerator {
@Override
public Object generate(Object target, Method method, Object... params) {
return method.getName()+Arrays.toString(params);
}
}
pojo
/**
* @author ZSL
* @ClassName User
* @description
* @date 2019/8/12
*/
public class User implements Serializable {
private Integer id;
private String name;
public User() {
}
public User(Integer id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
controller
/**
* @author ZSL
* @ClassName CacheController
* @description
* @date 2019/8/12
*/
@RestController
public class CacheController {
@Autowired
KeyConfig keyConfig;
@GetMapping("/gu")
public void getUser(Integer id, String name){
keyConfig.getUser(id, name);
}
@GetMapping("/uu")
public void updUser(User user){
user.setId(1);
user.setName("zsl");
keyConfig.updUser(user);
}
@GetMapping("/du")
public void delUser(Integer id){
keyConfig.delUser(id);
}
}