马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

SpringBoot系列:Spring Boot集成Spring Cache,使用EhCache

[复制链接]
查看1926 | 回复0 | 2019-10-24 09:48:08 | 显示全部楼层 |阅读模式

前面的章节,解说了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache、RedisCache、ConcurrentMapCache等。

这一节我们来看看Spring Cache使用EhCache。

一、EhCache使用演示

EhCache是一个纯Java的历程内缓存框架,具有快速、精悍等特点,Hibernate中的默认Cache就是使用的EhCache。

本章节示例是在Spring Boot集成Spring Cache的源码底子上举行改造。源码地址:https://github.com/imyanger/springboot-project/tree/master/p20-springboot-cache

使用EhCache作为缓存,我们先引入相干依靠。

  1. <code><dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <!--ehcache依靠-->
  6. <dependency>
  7. <groupId>net.sf.ehcache</groupId>
  8. <artifactId>ehcache</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-cache</artifactId>
  13. </dependency></code>
复制代码

然后创建EhCache的配置文件ehcache.xml。

  1. <code><?xml version="1.0" encoding="UTF-8"?>
  2. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
  4. updateCheck="false">
  5. <!--
  6. 磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的假造内存
  7. path:指定在硬盘上存储对象的路径
  8. path可以配置的目录有:
  9. user.home(用户的家目录)
  10. user.dir(用户当前的工作目录)
  11. java.io.tmpdir(默认的暂时目录)
  12. ehcache.disk.store.dir(ehcache的配置目录)
  13. 绝对路径(如:d:\\ehcache)
  14. 检察路径方法:String tmpDir = System.getProperty("java.io.tmpdir");
  15. -->
  16. <diskStore path="java.io.tmpdir" />
  17. <!--
  18. defaultCache:默认的缓存配置信息,假如不加特殊阐明,则所有对象按照此配置项处置惩罚
  19. maxElementsInMemory:设置了缓存的上限,最多存储多少个纪录对象
  20. eternal:代表对象是否永不外期 (指定true则下面两项配置需为0无限期)
  21. timeToIdleSeconds:最大的发呆时间 /秒
  22. timeToLiveSeconds:最大的存活时间 /秒
  23. overflowToDisk:是否答应对象被写入到磁盘
  24. 阐明:下列配置自缓存建立起600秒(10分钟)有效 。
  25. 在有效的600秒(10分钟)内,假如连续120秒(2分钟)未访问缓存,则缓存失效。
  26. 就算有访问,也只会存活600秒。
  27. -->
  28. <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="600"
  29. timeToLiveSeconds="600" overflowToDisk="true" />
  30. <cache name="cache" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120"
  31. timeToLiveSeconds="600" overflowToDisk="true" />
  32. </ehcache></code>
复制代码

然后SpringBoot配置文件中,指明缓存类型并声明Ehcache配置文件的位置。

  1. <code>server:
  2. port: 10900
  3. spring:
  4. profiles:
  5. active: dev
  6. cache:
  7. type: ehcache
  8. ehcache:
  9. config: classpath:/ehcache.xml</code>
复制代码

这样就可以开始使用Ehcache了,测试代码与Spring Boot集成Spring Cache同等。

SpringBoot启动类,@EnableCaching开启Spring Cache缓存功能。

  1. <code>@EnableCaching
  2. @SpringBootApplication
  3. public class SpringbootApplication {
  4. public static void main(String[] args) {
  5. String tmpDir = System.getProperty("java.io.tmpdir");
  6. System.out.println("暂时路径:" + tmpDir);
  7. SpringApplication.run(SpringbootApplication.class, args);
  8. }
  9. }</code>
复制代码

CacheApi接口调用类,方便调用举行测试。

  1. <code>@RestController
  2. @RequestMapping("cache")
  3. public class CacheApi {
  4. @Autowired
  5. private CacheService cacheService;
  6. @GetMapping("get")
  7. public User get(@RequestParam int id){
  8. return cacheService.get(id);
  9. }
  10. @PostMapping("set")
  11. public User set(@RequestParam int id, @RequestParam String code, @RequestParam String name){
  12. User u = new User(code, name);
  13. return cacheService.set(id, u);
  14. }
  15. @DeleteMapping("del")
  16. public void del(@RequestParam int id){
  17. cacheService.del(id);
  18. }
  19. }</code>
复制代码

CacheService缓存业务处置惩罚类,添加缓存,更新缓存和删除。

  1. <code>@Slf4j
  2. @Service
  3. public class CacheService {
  4. private Map<Integer, User> dataMap = new HashMap <Integer, User>(){
  5. {
  6. for (int i = 1; i < 100 ; i++) {
  7. User u = new User("code" + i, "name" + i);
  8. put(i, u);
  9. }
  10. }
  11. };
  12. // 获取数据
  13. @Cacheable(value = "cache", key = "&#39;user:&#39; + #id")
  14. public User get(int id){
  15. log.info("通过id{}查询获取", id);
  16. return dataMap.get(id);
  17. }
  18. // 更新数据
  19. @CachePut(value = "cache", key = "&#39;user:&#39; + #id")
  20. public User set(int id, User u){
  21. log.info("更新id{}数据", id);
  22. dataMap.put(id, u);
  23. return u;
  24. }
  25. //删除数据
  26. @CacheEvict(value = "cache", key = "&#39;user:&#39; + #id")
  27. public void del(int id){
  28. log.info("删除id{}数据", id);
  29. dataMap.remove(id);
  30. }
  31. }</code>
复制代码

源码地址:https://github.com/imyanger/springboot-project/tree/master/p22-springboot-cache-ehcache







来源:https://www.cnblogs.com/imyanger/p/11729480.html
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则