2019独角兽企业重金招聘Python工程师标准>>>

1,SpringBoot版本

2.0.3.RELEASE

①,pom.xml

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.3.RELEASE</version><relativePath /> <!-- lookup parent from repository --></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache</artifactId></dependency></dependencies>

②,application.properties

spring.cache.ehcache.config=classpath:/ehcache.xml

③,启用缓存注解

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);}
}

④,springboot为我们注入的缓存先关类

@Configuration
@ConditionalOnClass({ Cache.class, EhCacheCacheManager.class })
@ConditionalOnMissingBean(org.springframework.cache.CacheManager.class)
@Conditional({ CacheCondition.class,EhCacheCacheConfiguration.ConfigAvailableCondition.class })
class EhCacheCacheConfiguration {private final CacheProperties cacheProperties;private final CacheManagerCustomizers customizers;EhCacheCacheConfiguration(CacheProperties cacheProperties,CacheManagerCustomizers customizers) {this.cacheProperties = cacheProperties;this.customizers = customizers;}
//缓存管理器@Beanpublic EhCacheCacheManager cacheManager(CacheManager ehCacheCacheManager) {return this.customizers.customize(new EhCacheCacheManager(ehCacheCacheManager));}@Bean@ConditionalOnMissingBeanpublic CacheManager ehCacheCacheManager() {Resource location = this.cacheProperties.resolveConfigLocation(this.cacheProperties.getEhcache().getConfig());if (location != null) {return EhCacheManagerUtils.buildCacheManager(location);}return EhCacheManagerUtils.buildCacheManager();}static class ConfigAvailableCondition extends ResourceCondition {ConfigAvailableCondition() {super("EhCache", "spring.cache.ehcache.config", "classpath:/ehcache.xml");}}}

⑤,类路径下的ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"><!--将缓存保存到硬盘的那个位置 --><diskStore path="d:/ehcache/"></diskStore><!-- <diskStore path="java.io.tmpdir"></diskStore>java.io.tmpdir取值:操作系统不同 这个系统属性所表示的目录也不同On Windows: java.io.tmpdir:[C:\Users\liyhu\AppData\Local\Temp\]On Solaris: java.io.tmpdir:[/var/tmp/]On Linux: java.io.tmpdir: [/tmp]On Mac OS X: java.io.tmpdir: [/tmp]--><!--指定缓存名 --><cache name="home"eternal="false"maxEntriesLocalHeap="0"timeToIdleSeconds="200"diskPersistent="true"/><!-- eternal:true表示对象永不过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds属性,默认为false --><!-- maxEntriesLocalHeap:堆内存中最大缓存对象数,0没有限制 --><!-- timeToIdleSeconds: 设定允许对象处于空闲状态的最长时间,以秒为单位。当对象自从最近一次被访问后,如果处于空闲状态的时间超过了timeToIdleSeconds属性值,这个对象就会过期,EHCache将把它从缓存中清空。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地处于空闲状态 --><!-- diskPersistent:是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false。 --><!-- memoryStoreEvictionPolicy:如果内存中数据超过内存限制,向磁盘缓存时的策略。默认值LRU,可选FIFO、LFU。--></ehcache>

2,使用缓存

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache.ValueWrapper;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Service;import com.example.demo.bean.Person;@Service
@CacheConfig(cacheNames = "home")
public class HomeService {
//注入缓存管理器@AutowiredEhCacheCacheManager cacheManager;
//使用注解缓存,key默认为入参@Cacheable()public String find(Person person) {person.getAge();System.out.println("service 进来了。。。");return "service find====";}//手动使用缓存public String get(Integer age) {
//查找指定缓存homeorg.springframework.cache.Cache cache = cacheManager.getCache("home");
//根据key查找是否有缓存该值ValueWrapper val = cache.get(age);if(val!=null) {System.out.println("有缓存。。。");return val.get().toString();}else {System.out.println("没有缓存。。。");cache.put(age, age);return age.toString();}}

转载于:https://my.oschina.net/u/3574106/blog/1835380

SpringBoot简单使用ehcache相关推荐

  1. 【SpringBoot】27、SpringBoot中整合Ehcache实现热点数据缓存

    EhCache 是一个纯 Java 的进程内缓存框架,具有快速.精干等特点,是 Hibernate 中默认的 CacheProvider.用惯了 Redis,很多人可能已经忘记了还有 EhCache ...

  2. SpringBoot中使用Ehcache的详细教程

    本都缓存能做什么? 数据缓存在jvm中,大幅提升性能 为什么要用本地缓存? 相对于IO操作,速度快,效率高 相对于Redis,Redis是一种优秀的分布式缓存实现,受限于网卡等原因,远水救不了近火 本 ...

  3. 【SpringBoot】在SpringBoot中使用Ehcache

    SpringBoot提供了对缓存的支持,通过在启动类中添加@EnableCaching注解自动化配置合适的缓存管理器(CacheManager),Spring Boot根据下面的顺序去侦测缓存提供者: ...

  4. SpringSecurity使用SpringBoot简单使用

    SpringSecurity使用SpringBoot简单使用(一) 先来回顾下最开始我们不用框架时是怎么做认证授权的, 1.不用框架认证流程 1.客户端发送请求,controller层将请求对象封装成 ...

  5. SpringBoot 简单实现仿CAS单点登录系统

    SpringBoot 简单实现仿CAS单点登录系统 新境界开源开源SSO项目介绍 新境界开源SSO项目实现原理大致如下: 新境界开源SSO项目登录流程介绍 新境界开源SSO项目授权登录流程介绍 新境界 ...

  6. SpringBoot简单整合沙箱支付

    SpringBoot简单整合沙箱支付 操作步骤: 下载官方demo 配置 AlipayConfig相关信息 SpringBoot项目创建 解压官方demo,导入项目 业务代码编写 测试运行 相关注意事 ...

  7. Springboot简单练手的记账本

    Springboot简单练手的记账本 昨天看雷哥的教程写了个简单的记账本练练手,没有把笔记整理下来放在博客上,今天补上.言归正传,进入正题. 老规矩,我们还是先看看项目的目录结构,以及登陆界面 每个包 ...

  8. Springboot简单应用

    一直用SpringMVC+Spring开发,虽然用了这么久,但对里面繁琐的配置还是很头疼,这种情况改用Springboot,无疑是个很好的选择.废话不多说,直接介绍自己如何使用的,在这之前还是有必要介 ...

  9. Shiro和SpringBoot简单集成

    Shiro是一种简单的安全框架,可以用来处理系统的登录和权限问题. 本篇记录一下Spring Boot和Shiro集成,并使用Jwt Token进行无状态登录的简单例子. 参考Demo地址,此Demo ...

最新文章

  1. 怎么用cmb运行c语言文档,关于化学质量平衡(CMB)受体模型应用中若干技术问题的研究-环境科学专业论文.docx...
  2. [翻译] ASP.NET Core 2.2 正式版发布
  3. k8s ConfigMap使用示例:以volume或变量形式挂载到pod中
  4. [LeetCode] NO. 8 String to Integer (atoi)
  5. 收下这份实操案例,还怕不会用Jmeter接口测试工具?!
  6. Python成长笔记 - 基础篇 (七)python面向对象
  7. Python实现Matlab绘制散点图
  8. linux下静态库、动态库总结
  9. Android 之解析XML文件
  10. 思科路由器连接电脑配置
  11. 虚拟机安装pycharm
  12. 普顿外汇告诉大家如何在外汇市场保持良好的心态?
  13. Java超详细基础知识
  14. 涂色问题 阿里编程机试题目
  15. python壁纸高清图片_详解Python静态网页爬取获取高清壁纸
  16. 50岁程序员还奋战在一线,软件测试能干到多少岁?有年龄限制吗?
  17. 项目经理:我是如何进行项目进度管理的
  18. 银行计算机安全工作会议记录,如何发表课题相关论文_横向课题成果能发论文吗...
  19. 自学软件测试需要多久?怎么自学软件测试?自学软件测试可以找到工作吗?--请看我是怎么走过来的!绝对干货!
  20. Python爬虫笔记——关于【时间】的模块

热门文章

  1. 全球及中国皮肤癌药物行业深度研究及项目可行性调研报告2022-2027年
  2. 中国农民丰收节交易会新闻发布会倡导功能农业·农业大健康
  3. hive sql 学习笔记
  4. ZOJ - 3872 Beauty of Array
  5. 动态创建的 CEdit 被限制长度,增加 ES_AUTOHSCROLL 属性;被无法Tab激活焦点,增加 WS_TABSTOP 属性(转)...
  6. UVA 146 ID Codes
  7. Ext JS 4.1.1 RC2发布
  8. Android学习笔记(三):Andriod程序框架
  9. 目的港无人提货的法律风险及风险承担
  10. 配置化的版本更新引导怎么做?