天天看点

SpringBoot与ehcache整合

EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。

  ehcache提供了多种缓存策略,主要分为内存和磁盘两级,所以无需担心容量问题。

  spring-boot是一个快速的集成框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。

项目结构:

SpringBoot与ehcache整合

添加依赖库:

<!-- ehcache 缓存 -->
<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
</dependency>
           

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">

    <!--diskStore:为缓存路径,ehcache分为内存和磁盘两级,此属性定义磁盘的缓存位置。 -->
    <!-- defaultCache:默认缓存策略,当ehcache找不到定义的缓存时,则使用这个缓存策略。只能定义一个。 -->
    <!--name:缓存名称。 -->
    <!--maxElementsInMemory:缓存最大数目 -->
    <!--maxElementsOnDisk:硬盘最大缓存个数。 -->
    <!--eternal:对象是否永久有效,一但设置了,timeout将不起作用。 -->
    <!--overflowToDisk:是否保存到磁盘,当系统当机时 -->
    <!-- timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。 -->
    <!--timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.也就是对象存活时间无穷大。 -->
    <!--diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between 
        restarts of the Virtual Machine. The default value is false. -->
    <!--diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。 -->
    <!--diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。 -->
    <!-- memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。 -->
    <!--clearOnFlush:内存数量最大时是否清除。 -->
    <!--memoryStoreEvictionPolicy:可选策略有:LRU(最近最少使用,默认策略)、FIFO(先进先出)、LFU(最少访问次数)。 -->

    <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>
           

启动缓存配置:

@SpringBootApplication
//启用缓存注解
@EnableCaching
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
           

User.java

public class User {
    private int id;
    private String uuid;
    private String name;
    private String password;
    //setter和getter。。。
}
           

UserMapper.java

@Mapper
public interface UserMapper {
    void delete(String uuid);
    User update(User user);
    User findByUuid(String uuid);
    int save(@Param("user") User user);

}
           

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="com.dx.mapper.UserMapper">
    <resultMap id="UserResultMap" type="com.dx.entity.User">
        <id property="id" column="id" />
        <result property="uuid" column="uuid" />
        <result property="name" column="name" />
        <result property="password" column="password" />
    </resultMap>
    <insert id="save">
        INSERT INTO users(name, password,uuid)
        VALUES
        (#{user.name}, #{user.password}, #{user.uuid})
    </insert>
    <select id="findByUuid" resultType="com.dx.entity.User">
        SELECT * FROM users WHERE
        uuid = #{uuid}
    </select>
    <delete id="delete">
        DELETE FROM users WHERE uuid = #{uuid}
    </delete>
</mapper>
           

UserService.java

@Service

public class UserService {

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

@Autowired
private UserMapper userMapper;

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

// 更新用户数据
@CachePut(value = DEMO_CACHE_NAME, key = "'user_'+#user.getUuid()")
public User update(User user) throws CacheException {
    User user1 = userMapper.findByUuid(user.getUuid());
    if (null == user1) {
        throw new CacheException("Not Find");
    }
    user1.setName(user.getName());
    return user1;
}

// 查找用户数据
@Cacheable(value = DEMO_CACHE_NAME, key = "'user_'+#uuid")
public User findByUuid(String uuid) {
    // 若找不到缓存将打印出提示语句
    System.err.println("没有走缓存!" + uuid);
    return userMapper.findByUuid(uuid);
}

// 保存用户数据
@CacheEvict(value = DEMO_CACHE_NAME, key = CACHE_KEY)
public int save(User user) {
    return userMapper.save(user);
}
           

}

EhcacheController.java
           

@RestController

public class EhcacheController {

private static final Logger logger = LoggerFactory

.getLogger(EhcacheController.class);

@Autowired
private UserService userService;

@RequestMapping("/encache")
public String EhcacheTest() throws Exception {
    logger.debug("进行Encache缓存测试");
    logger.info("====生成第一个用户====");
    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("abc");
    user1.setUuid(uuid1);
    user1.setPassword("2222");
    if (userService.save(user1) == 0) {
        throw new Exception("用户对象插入数据库失败");
    }

    // 第一次查询
    System.out.println(userService.findByUuid(user1.getUuid()));
    // 通过缓存查询
    System.out.println(userService.findByUuid(user1.getUuid()));
    System.out.println("====修改数据====");
    User user2 = new User();
    user2.setName("abc-update");
    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";
}
           

}

测试:
           

没有走缓存!00bd112a499443218fa2d9cfa90fd373

[email protected]

[email protected]

====修改数据====

[email protected]

[email protected]

“`

通过结果可知:只有第一次查询需要从数据查询数据,若表中数据更新,则需从新从库中查询,否则只需从缓存中查询,大大提高效率。

继续阅读