刀刀网
您的当前位置:首页Springboot整合ehcache缓存

Springboot整合ehcache缓存

来源:刀刀网
Springboot整合ehcache缓存

EhCache是⼀个⽐较成熟的Java缓存框架,最早从hibernate发展⽽来, 是进程中的缓存系统,它提供了⽤内存,磁盘⽂件存储,以及分布式存储⽅式等多种灵活的cache管理⽅案,快速简单。

Springboot对ehcache的使⽤⾮常⽀持,所以在Springboot中只需做些配置就可使⽤,且使⽤⽅式也简易。在你的项⽬上配置以下⼏步即可使⽤⾸先,⽼规矩,pom.xml加依赖;

org.springframework.boot spring-boot-starter-cache

net.sf.ehcache ehcache

第⼆步,创建ehcache.xml配置⽂件

位置:classpath⽬录下,即src/main/resources/ehcache.xml

⽂件内容开发的时候可参考第⼀步导⼊的jar包,具体在哪呢,看下⾯:再看代码:

maxElementsInMemory=\"10000\" eternal=\"false\"

timeToIdleSeconds=\"120\" timeToLiveSeconds=\"120\"

maxElementsOnDisk=\"10000000\"

diskExpiryThreadIntervalSeconds=\"120\" memoryStoreEvictionPolicy=\"LRU\">

maxElementsInMemory=\"10000\" eternal=\"false\"

timeToIdleSeconds=\"120\" timeToLiveSeconds=\"120\"

maxElementsOnDisk=\"10000000\"

diskExpiryThreadIntervalSeconds=\"120\" memoryStoreEvictionPolicy=\"LRU\">

 说明:

这个是磁盘存储路径,当内存缓存满了的时候,就会往这⾥⾯放,java.io.tmdir是操作系统缓存的临时⽬录,不同操作系统缓存⽬录不⼀样。maxElementsInMemory 内存缓存中最多可以存放的元素数量,若放⼊Cache中的元素超过这个数值,则有以下两种情况 1)若overflowToDisk=true,则会将Cache中多出的元素放⼊磁盘⽂件中

2)若overflowToDisk=false,则根据memoryStoreEvictionPolicy策略替换Cache中原有的元素overflowToDisk 内存不⾜时,是否启⽤磁盘缓存eternal 缓存中对象是否永久有效

timeToIdleSeconds 缓存数据在失效前的允许闲置时间(单位:秒),仅当eternal=false时使⽤,默认值是0表⽰可闲置时间⽆穷⼤,若超过这个时间没有访问此Cache中的某个元素,那么此元素将被从Cache中清除timeToLiveSeconds 缓存数据的总的存活时间(单位:秒),仅当eternal=false时使⽤,从创建开始计时,失效结束。maxElementsOnDisk 磁盘缓存中最多可以存放的元素数量,0表⽰⽆穷⼤diskExpiryThreadIntervalSeconds 磁盘缓存的清理线程运⾏间隔,默认是120秒

memoryStoreEvictionPolicy 内存存储与释放策略,即达到maxElementsInMemory时,Ehcache会根据指定策略清理内存 共有三种策略,分别为LRU(最近最少使⽤)、LFU(最常⽤的)、FIFO(先进先出)另外,defaultCache是默认缓存⽅式,cache是⾃定义的缓存⽅式,⾃⾏设置name

第三步,在Springboot配置⽂件中把ehcache.xml配置进去;即在application.properties中加⼊以下配置代码spring.cache.ehcache.config=ehcache.xml

 

第三步结束,ehcache在Springboot中就配置完成了,下⾯就是怎么在Springboot中使⽤

第四步,在启动类前加上@EnableCaching注解;这样的话,启动类启动时会去启动缓存启动器。

@SpringBootApplication

@MapperScan(\"com.yunxing.mapper\")@EnableCachingpublic class app {

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

}}

第五步,实体类实现可序列化接⼝Serializable;由于需要实体类⽀持缓存中的磁盘存储,所以需要实体类实现可序列化接⼝

public class user implements Serializable{ private Integer id; private String name; private Integer age; 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; }

public Integer getAge() { return age; }

public void setAge(Integer age) { this.age = age; } }

  

第六步,使⽤@Cacheable把数据存进缓存,下⾯就是专门把⽅法返回值存⼊缓存

@Service

@Transactional //事务,表⽰该类下所有的都受事务控制public class userServiceImpl implements userService { @Autowired

private userMapper usermapper; @Override

@Cacheable(value=\"users\")

public user selectUserById(int id) {

user user=this.usermapper.selectUserById(id);

System.out.println(\"1111111111111111111111111\"); return user; }}

 说明: @Cacheable可以标记在⼀个⽅法上,也可以标记在⼀个类上,当标记在⼀个⽅法上时表⽰该⽅法是⽀持缓存的,当标记在⼀个类上时则表⽰该类所有的⽅法都是⽀持缓存的。对于⼀个⽀持缓存的⽅法,Spring会在其被调⽤后将其返回值缓存起来,以保证下次利⽤同样的参数来执⾏该⽅法时可以直接从缓存中获取结果,⽽不需要再次执⾏该⽅法。Spring在缓存⽅法的返回值时是以键值对进⾏缓存的,值就是⽅法的返回结果。 @Cacheable可以指定三个属性,value、key和condition。

value属性指定cache的名称(即选择ehcache.xml中哪种缓存⽅式存储)

key属性是⽤来指定Spring缓存⽅法的返回结果时对应的key的。该属性⽀持SpringEL表达式。当我们没有指定该属性时,Spring将使⽤默认策略⽣成key。我们也直接使⽤“#参数名”或者“#p参数index”。下⾯是⼏个使⽤参数作为key的⽰例

@Cacheable(value=\"users\ public User find(Integer id) { returnnull; }

@Cacheable(value=\"users\ public User find(Integer id) { returnnull; }

@Cacheable(value=\"users\ public User find(User user) { returnnull; }

@Cacheable(value=\"users\ public User find(User user) { returnnull;

}

最后,使⽤@CacheEvict清除缓存;

@CacheEvict(value=\"users\public void saveUsers(Users users) {this.usersRepository.save(users);}

  说明:@CacheEvict是⽤来标注在需要清除缓存元素的⽅法或类上的。当标记在⼀个类上时表⽰其中所有的⽅法的执⾏都会触发缓存的清除操作。@CacheEvict可以指定的属性有value、key、condition、allEntries和beforeInvocation。

其中value、key和condition的语义与@Cacheable对应的属性类似;allEntries是boolean类型,表⽰是否需要清除缓存中的所有元素。默认为false,表⽰不需要。

当指定了allEntries为true时,Spring Cache将忽略指定的key。有的时候我们需要Cache⼀下清除所有的元素,这⽐⼀个⼀个清除元素更有效率。

因篇幅问题不能全部显示,请点此查看更多更全内容