当前位置: 移动技术网 > IT编程>开发语言>Java > 详解SpringBoot缓存的实例代码(EhCache 2.x 篇)

详解SpringBoot缓存的实例代码(EhCache 2.x 篇)

2019年07月22日  | 移动技术网IT编程  | 我要评论

本篇介绍了springboot 缓存(ehcache 2.x 篇),分享给大家,具体如下:

springboot 缓存

在 spring boot中,通过@enablecaching注解自动化配置合适的缓存管理器(cachemanager),spring boot根据下面的顺序去侦测缓存提供者:

  • generic
  • jcache (jsr-107)
  • ehcache 2.x
  • hazelcast
  • infinispan
  • redis
  • guava
  • simple

关于 spring boot 的缓存机制:

高速缓存抽象不提供实际存储,并且依赖于由org.springframework.cache.cache和org.springframework.cache.cachemanager接口实现的抽象。 spring boot根据实现自动配置合适的cachemanager,只要缓存支持通过@enablecaching注释启用即可。

spring boot 配置 ehcache 2.x

官方文档上对于注解缓存的介绍资料非常之少,往往需要我们自己去了解相应的缓存提供者。我这里主要介绍的是 ehcache .

引入依赖

在pom.xml文件中引入以下依赖

   <!--开启 cache 缓存-->
   <dependency>
     <groupid>org.springframework.boot</groupid>
     <artifactid>spring-boot-starter-cache</artifactid>
   </dependency>
   <!-- ehcache 缓存 -->
   <dependency>
     <groupid>net.sf.ehcache</groupid>
     <artifactid>ehcache</artifactid>
   </dependency>

引入配置文件 ehcache.xml

resource文件夹下创建文件ehcache.xml,并进行配置:

<?xml version="1.0" encoding="utf-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
     xsi:nonamespaceschemalocation="http://ehcache.org/ehcache.xsd"
     updatecheck="false">
  <defaultcache
      eternal="false"
      maxelementsinmemory="1000"
      overflowtodisk="false"
      diskpersistent="false"
      timetoidleseconds="0"
      timetoliveseconds="600"
      memorystoreevictionpolicy="lru" />

  <!-- 这里的 users 缓存空间是为了下面的 demo 做准备 -->
  <cache
      name="users"
      eternal="false"
      maxelementsinmemory="100"
      overflowtodisk="false"
      diskpersistent="false"
      timetoidleseconds="0"
      timetoliveseconds="300"
      memorystoreevictionpolicy="lru" />
</ehcache>

ehcache.xml 文件配置详解

部分资料来源于网络

  1. diskstore:为缓存路径,ehcache分为内存和磁盘两级,此属性定义磁盘的缓存位置。
  2. defaultcache:默认缓存策略,当ehcache找不到定义的缓存时,则使用这个缓存策略。只能定义一个。
  3. name:缓存名称。
  4. maxelementsinmemory:缓存最大数目
  5. maxelementsondisk:硬盘最大缓存个数。
  6. eternal:对象是否永久有效,一但设置了,timeout将不起作用。
  7. overflowtodisk:是否保存到磁盘,当系统当机时
  8. timetoidleseconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
  9. timetoliveseconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
  10. diskpersistent:是否缓存虚拟机重启期数据 whether the disk store persists between restarts of the virtual machine. the default value is false.diskspoolbuffersizemb:这个参数设置diskstore(磁盘缓存)的缓存区大小。默认是30mb。每个cache都应该有自己的一个缓冲区。
  11. diskexpirythreadintervalseconds:磁盘失效线程运行时间间隔,默认是120秒。
  12. memorystoreevictionpolicy:当达到maxelementsinmemory限制时,ehcache将会根据指定的策略去清理内存。默认策略是lru(最近最少使用)。你可以设置为fifo(先进先出)或是lfu(较少使用)。
  13. clearonflush:内存数量最大时是否清除。
  14. memorystoreevictionpolicy:可选策略有:lru(最近最少使用,默认策略)、fifo(先进先出)、lfu(最少访问次数)。

fifo,first in first out,先进先出。

lfu, less frequently used,一直以来最少被使用的。如上面所讲,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。

lru,least recently used,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。

在主类加上启动注解

在 spring boot 主类加上开启缓存的注解@enablecaching

demo : springboot + ehcache

搭建 spring boot 工程

我搭建了一个普通的 springboot 工程,配置了 druid+mysql。

并在数据库中创建了 users 表,各字段如下:

字段名 属性
id bigint
uuid varchar
name varchar
age int

用户实体类

user.java

public class user {

  private long id;
  private string uuid;
  private string name;
  private integer age;

  //省略 get、set 及 tostring 方法
}

用户数据库操作接口

userdao.java

@mapper
public interface userdao{

  void delete(string uuid);

  user update(user user);

  user findbyuuid(string uuid);

  int save(@param("user") user user);
}

用户操作mapper文件

usermapper.xml

<?xml version="1.0" encoding="utf-8" ?>
<!doctype mapper
    public "-//mybatis.org//dtd mapper 3.0//en"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="qg.fangrui.boot.dao.userdao">
  <!--目的:为dao接口方法提供sql语句-->

  <!--映射实体对象-->
  <resultmap id="userresultmap" type="qg.fangrui.boot.model.user">
    <id property="id" column="id" />
    <result property="uuid" column="uuid" />
    <result property="name" column="name" />
    <result property="age" column="age" />
  </resultmap>


  <insert id="save">
    insert into users(name, age, uuid)
    values (#{user.name}, #{user.age}, #{user.uuid})
  </insert>

  <select id="findbyuuid" resulttype="user">
    select * from users where uuid = #{uuid}
  </select>

  <delete id="delete">
    delete from users where uuid = #{uuid}
  </delete>

</mapper>

用户操作 service 层

一般情况下,我们在sercive层进行对缓存的操作。先介绍 ehcache 在 spring 中的注解:在支持 spring cache 的环境下,

  1. @cacheable : spring在每次执行前都会检查cache中是否存在相同key的缓存元素,如果存在就不再执行该方法,而是直接从缓存中获取结果进行返回,否则才会执行并将返回结果存入指定的缓存中。
  2. @cacheevict : 清除缓存。
  3. @cacheput : @cacheput也可以声明一个方法支持缓存功能。使用@cacheput标注的方法在执行前不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法,并将执行结果以键值对的形式存入指定的缓存中。
  4. 这三个方法中都有两个主要的属性:value 指的是 ehcache.xml 中的缓存策略空间;key 指的是缓存的标识,同时可以用 # 来引用参数。

userservice.java

@service
public class userservice {

  //这里的单引号不能少,否则会报错,被识别是一个对象
  private static final string cache_key = "'user'";
  private static final string demo_cache_name = "users";

  @autowired
  private userdao userdao;

  //删除用户数据
  @cacheevict(value = demo_cache_name,key = "'user_'+#uuid")//这是清除缓存
  public void delete(string uuid){
    userdao.delete(uuid);
  }

  //更新用户数据
  @cacheput(value = demo_cache_name,key = "'user_'+#user.getuuid()")
  public user update(user user) throws cacheexception{
    user user1 = userdao.findbyuuid(user.getuuid());
    if (null == user1){
      throw new cacheexception("not find");
    }
    user1.setage(user.getage());
    user1.setname(user.getname());
    return user1;
  }

  //查找用户数据
  @cacheable(value=demo_cache_name,key="'user_'+#uuid")
  public user findbyuuid(string uuid){
    //若找不到缓存将打印出提示语句
    system.err.println("没有走缓存!"+uuid);
    return userdao.findbyuuid(uuid);
  }

  //保存用户数据
  @cacheevict(value=demo_cache_name,key=cache_key)
  public int save(user user){
    return userdao.save(user);
  }
}

controller 类

最后我们创建一个 controller 来访问我们的缓存。因为我的 springboot 处于 debug 模式,会将所有的数据库操作打印出来,这样子缓存作用就可一目了然了。

ehcachecontroller.java

@restcontroller
public class ehcachecontroller {

  private static final logger logger = loggerfactory.getlogger(ehcachecontroller.class);

  @autowired
  private userservice userservice;

  @requestmapping("/encache")
  public string ehcachetest(){
    logger.debug("进行encache缓存测试");
    system.out.println("====生成第一个用户====");
    user user1 = new user();
    //生成第一个用户的唯一标识符 uuid
    string u1_uuid = uuid.randomuuid().tostring();
    //去掉 uuid 的 - 符号
    string uuid1 = u1_uuid.substring(0,8)+u1_uuid.substring(9,13)+u1_uuid.substring(14,18)+u1_uuid.substring(19,23)+u1_uuid.substring(24);
    user1.setname("张三");
    user1.setage(18);
    user1.setuuid(uuid1);
    if (userservice.save(user1) == 0){
      throw new jdbcexception("用户对象插入数据库失败");
    }

    //第一次查询
    system.out.println(userservice.findbyuuid(user1.getuuid()));
    //通过缓存查询
    system.out.println(userservice.findbyuuid(user1.getuuid()));

    system.out.println("====修改数据====");
    user user2 = new user();
    user2.setname("李四-update");
    user2.setage(22);
    user2.setid(user1.getid());
    user2.setuuid(user1.getuuid());
    try {
      system.out.println(userservice.update(user2));
    } catch (cacheexception e){
      e.printstacktrace();
    }

    system.out.println(userservice.findbyuuid(user2.getuuid()));
    return "success";
  }
}

测试

启动 springboot 工程,访问 http://www.lhsxpumps.com/_localhost:8080/encache ,并查看控制台打印信息:

由控制台,我们可以清楚到看到,第一次查询用户信息时,工程将用户信息存入缓存中;在第二次查询时,无需访问数据库直接从缓存中获取用户信息。

个人参考项目:

个人参考项目:https://github.com/funrily/springboot-study/tree/master/%e6%a1%88%e4%be%8b9

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网