前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot使用ehcache缓存

springboot使用ehcache缓存

作者头像
吟风者
发布2019-07-25 16:52:01
1.7K0
发布2019-07-25 16:52:01
举报
文章被收录于专栏:吟风者吟风者

整合ehcache

本文部分步骤继承于springboot使用cache缓存,如果有不清楚的,请移驾springboot使用cache缓存 ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持RESTSOAP api等特点。

  1. 导入依赖 整合ehcache必须要导入它的依赖。
代码语言:javascript
复制
<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
  1. yml配置

需要说明的是config:classpath:/ehcache.xml可以不用写,因为默认就是这个路径。但ehcache.xml必须有。

代码语言:javascript
复制
spring:
  cache:
    type: ehcache
    ehcache:
      config: classpath:/config/ehcache.xml
  1. ehcache.xml

resources目录下新建config文件夹,在文件夹中建立ehcache.xml文件。

代码语言:javascript
复制
<ehcache>

    <!--
        磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存
        path:指定在硬盘上存储对象的路径
        path可以配置的目录有:
            user.home(用户的家目录)
            user.dir(用户当前的工作目录)
            java.io.tmpdir(默认的临时目录)
            ehcache.disk.store.dir(ehcache的配置目录)
            绝对路径(如:d:\\ehcache)
        查看路径方法:String tmpDir = System.getProperty("java.io.tmpdir");
     -->
    <diskStore path="java.io.tmpdir" />

    <!--
        defaultCache:默认的缓存配置信息,如果不加特殊说明,则所有对象按照此配置项处理
        maxElementsInMemory:设置了缓存的上限,最多存储多少个记录对象
        eternal:代表对象是否永不过期 (指定true则下面两项配置需为0无限期)
        timeToIdleSeconds:最大的发呆时间 /秒
        timeToLiveSeconds:最大的存活时间 /秒
        overflowToDisk:是否允许对象被写入到磁盘
        说明:下列配置自缓存建立起600秒(10分钟)有效 。
        在有效的600秒(10分钟)内,如果连续120秒(2分钟)未访问缓存,则缓存失效。
        就算有访问,也只会存活600秒。
     -->
    <defaultCache maxElementsInMemory="10000" eternal="false"
                  timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="true" />

    <cache name="myCache" maxElementsInMemory="10000" eternal="false"
                  timeToIdleSeconds="120" timeToLiveSeconds="600" overflowToDisk="true" />

</ehcache>
  1. 使用缓存 @CacheConfig(cacheNames={“myCache”})设置ehcache的名称,这个名称必须在ehcache.xml已配置。
代码语言:javascript
复制
import com.example.ehcache.dao.PersonRepository;
import com.example.ehcache.entity.Person;
import com.example.ehcache.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * @author 李振
 * @date 2018/12/27
 *
 */
@Service
@CacheConfig(cacheNames = {"myCache"})
public class DemoServiceImpl implements DemoService {
    @Autowired
    private PersonRepository personRepository;
    /**
     * 注意:如果没有指定key,则方法参数作为key保存到缓存中
     */
    /**
     * @param person
     * @return
     * @CachePut缓存新增的或更新的数据到缓存,其中缓存的名称为people,数据的key是person的id
     */
    @CachePut(key = "#person.id")
    @Override
    public Person save(Person person) {
        Person p = personRepository.save(person);
        System.out.println("为id,key为:" + p.getId() + "数据做了缓存");
        return p;
    }

    /**
     * @param id
     * @CacheEvict从缓存people中删除key为id的数据
     */
    @CacheEvict
    @Override
    public void remove(Integer id) {
        System.out.println("删除了id,key为" + id + "的数据缓存");
        personRepository.delete(id);
    }

    /**
     * @param person
     * @return
     * @Cacheable缓存key为person的id数据到缓存people中
     */
    @Cacheable(key = "#person.id")
    @Override
    public Person findOne(Person person) {
        Person p = personRepository.findOne(person.getId());
        System.out.println("为id,key为:" + p.getId() + "数据做了缓存");
        return p;
    }
}

整合完毕!

别忘了在启动类开启缓存!

源码地址:https://gitee.com/LiZhendd/demo

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.12.27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 整合ehcache
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档