无需Redis,SpringBoot集成Ehcache实现缓存

前言

在平时做项目都要用到缓存,方便临时存储一些数据,加快访问速度。如果项目比较小,搭建redis服务,后期在维护上比较麻烦。今天分享一个SpringBoot集成Ehcache实现缓存的教程,适合中小项目中使用。

创新互联于2013年创立,先为克东等服务建站,克东等地企业,进行企业商务咨询服务。为克东企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

准备工作

1、maven中导入依赖



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



org.ehcache
ehcache
3.8.1


javax.cache
cache-api
1.1.1

2、启动类上增加缓存注解

@MapperScan("com.zhangls.ehcache.dao.**")
@SpringBootApplication
@EnableCaching
public class EhcacheApplication {
public static void main(String[] args) {
SpringApplication.run(EhcacheApplication.class, args);
}
}

3、配置Ehcache

在resources下增加ehcache.xml文件,配置如下:

        xmlns="http://www.ehcache.org/v3"
xmlns:jsr107="http://www.ehcache.org/v3/jsr107"
xsi:schemaLocation="
http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">







java.lang.String

com.zhangls.ehcache.entity.User


1



2000
100



4、application.yml中配置

spring:
cache:
jcache:
config: classpath:ehcache.xml

注意事项

1.Ehcache 会在一定的规则下会序列化后存储到硬盘上,因此缓存对象必须支持序列化。

public class User implements Serializable{}

2.Spring定义了缓存接口Cache和管理缓存控制器 CacheManager,路径为

import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

使用方法--手动管理方式

@Autowired
private CacheManager cacheManager;
@GetMapping("/addCache")
public String addCache() {
User user = new User();
user.setUsername("九天银河聊编程");
user.setAge(34);
Cache cache = cacheManager.getCache("UserCache");
cache.put("user", user);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
return sdf.format(now) + ": " + "保存成功";
}

@GetMapping("/getCache")
public String getCache() {
Cache cache = cacheManager.getCache("UserCache");
Cache.ValueWrapper res = cache.get("user");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
if (null != res) {
User user = (User) res.get();//这里获取 ehcache.xml 中 value-type 定义的类型,可以直接强转。
return sdf.format(now) + ": " + "姓名:" + user.getUsername() + ",年龄:" + user.getAge();
}
return sdf.format(now) + ": " + "没有找到缓存!";
}

运行结果

执行:127.0.0.1:8080/ehcache/addCache。

执行:127.0.0.1:8080/ehcache/getCache。

1分钟后执行:127.0.0.1:8080/ehcache/getCache,缓存失效。

使用方法--@Cacheable 注解方式

service代码:

@Service
public class ImPersonServiceImpl implements ImPersonService{
@Resource
private PersonMapper personMapper;
@Override
@Cacheable(cacheNames = "PersonCache", key = "#personId")
public ImPerson selectByPrimaryKey(String personId) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
System.out.println(sdf.format(now) + ": 未命中缓存,请求数据库");
return personMapper.selectByPrimaryKey(personId);
}
}

controller代码:

@GetMapping("/getCachePerson")
public ImPerson getCachePerson() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date start = new Date();
System.out.println(sdf.format(start) + ":执行开始------");
ImPerson person = imPersonService.selectByPrimaryKey("1");
Date end = new Date();
System.out.println(sdf.format(end) + ":执行结束------");
return person;
}

执行两次:127.0.0.1:8080/ehcache/getCachePerson。

控制台只打印一次SQL信息,说明第二次请求从缓存中获取。

@Cacheable属性说明

  • cacheNames/value :用来指定缓存组件的名字。
  • key :缓存数据时使用的 key,可以用它来指定。默认是使用方法参数的值。(这个 key 你可以使用 spEL 表达式来编写)。
  • keyGenerator :key 的生成器。 key 和 keyGenerator 二选一使用。
  • cacheManager :可以用来指定缓存管理器。从哪个缓存管理器里面获取缓存。
  • condition :可以用来指定符合条件的情况下才缓存,如下表示id>1的进行缓存。
@Cacheable(cacheNames = "PersonCache", condition  = "#id > 1")
  • unless :否定缓存。当 unless 指定的条件为 true ,方法的返回值就不会被缓存。当然你也可以获取到结果进行判断。(通过 #result 获取方法结果)。
  • sync :是否使用异步模式。

踩坑说明

Spring 缓存注解是基于Spring AOP切面,必须走代理才能生效。同类调用或者子类调用父类带有缓存注解的方法时属于内部调用,没有走代理,所以注解不会生效。所以在使用@Cacheable时,一定要放在在service的实现类中进行调用。

文章题目:无需Redis,SpringBoot集成Ehcache实现缓存
本文网址:http://www.hantingmc.com/qtweb/news31/141781.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联