SpringBoot学习要点记录(七)----ehcache学习

本文为学习记录,如有不对,还望不吝赐教。
参考
【1】https://blog.csdn.net/yeiweilan/article/details/95636556
【2】https://www.cnblogs.com/kingsonfu/p/10412295.html

一、介绍

一个Java缓存框架,从hibernate发展而来,快速简单,具有多种缓存策略。

二、使用

2.1.1 环境搭建

主要依赖

 <!--开启 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>

添加配置文件eccache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="myEncache">
    <!--
        diskStore:为缓存路径,ehcache分为内存和磁盘 2级,此属性定义磁盘的缓存位置
        user.home - 用户主目录
        user.dir - 用户当前工作目录
        java.io.tmpdir - 默认临时文件路径
    -->
    <diskStore path="D:/home/Tmp_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(最少访问次数)。
            FIFO,first in first out,这个是大家最熟的,先进先出。
            LFU, Less Frequently Used,就是上面例子中使用的策略,直白一点就是讲一直以来最少被使用的。如上面所讲,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。
            LRU,Least Recently Used,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
-->
    <defaultCache
            eternal="false"
            maxElementsInMemory="1000"
            overflowToDisk="false"
            diskPersistent="false"
            timeToIdleSeconds="0"
            timeToLiveSeconds="600"
            memoryStoreEvictionPolicy="LRU"
    />
    <cache
            name="users_test"
            eternal="false"
            maxElementsInMemory="100"
            overflowToDisk="false"
            diskPersistent="false"
            timeToIdleSeconds="0"
            timeToLiveSeconds="300"
            memoryStoreEvictionPolicy="LRU"
    />
</ehcache>

引入

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/bootdemo?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: root
  cache:
    #ehcache配置文件路径
    ehcache:
      config: classpath:/ehcache/ehcache.xml
    #指定缓存类型,可加可不加
#    type: ehcache
2.1.2 springboot支持的注解
  1. @Cacheable :表明所修饰的方法是可以缓存的,当第一次调用这个方法时,它的结果会被缓存下来,在缓存的有效时间内,以后访问这个方法都直接返回缓存结果,不再执行方法中的代码段。
    参数:
  • value:缓存位置名称,不能为空,就是ehcache.xml中声明的cache的name, 指明将值缓存到哪个Cache中
  • key:缓存的key,默认为空,既表示使用方法的参数类型及参数值作为key,支持SpEL,如果要引用参数值使用井号加参数名,如:#userId,一般来说,我们的更新操作只需要刷新缓存中某一个值,所以定义缓存的key值的方式就很重要,最好是能够唯一,因为这样可以准确的清除掉特定的缓存,而不会影响到其它缓存值 ,本例子中使用实体加冒号再加ID组合成键的名称,如"user:1"、"order:223123"等
  • condition:触发条件,只有满足条件的情况才会加入缓存,默认为空,既表示全部都加入缓存
  1. @CachePut: 执行代码,并将结果缓存。
  2. @CacheEvict: 清除缓存,参数如下
  • value:缓存位置名称,不能为空,同上
  • key:缓存的key,默认为空,同上
  • condition:触发条件,只有满足条件的情况才会清除缓存,默认为空,支持SpEL
  • allEntries:true表示清除value中的全部缓存,默认为false
2.1.3 项目中使用

1.启动类上加注解@EnableCaching引入


@SpringBootApplication
@MapperScan(basePackages = {"com.example.ehcache.mapper"})
//启用cache缓存
@EnableCaching
public class BootEhcacheApplication {
	public static void main(String[] args) {
		SpringApplication.run(BootEhcacheApplication.class, args);
	}
}

2.方法上加注解

package com.example.ehcache.service.impl;


import com.example.ehcache.entity.User;
import com.example.ehcache.mapper.UserMapper;
import com.example.ehcache.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    //使用ehcache配置的缓存名users_test
    private final String USER_CACHE_NAME = "users_test";

    @Override
    public List<User> listUser() {
        return userMapper.selectUserList();
    }

    @Override
    @Cacheable(value = USER_CACHE_NAME, key = "'user' + #id")
    public User selectUserById(Integer id) {
        return userMapper.selectUserById(id);
    }

    @Override
    @CacheEvict(value = USER_CACHE_NAME, key = "'user' + #id")
    public void delete(Integer id) {
        userMapper.delete(id);
    }

    @Override
    @CacheEvict(value = USER_CACHE_NAME, key = "'user' + #user.userId")
    public void update(User user) {
        userMapper.update(user);
    }
}

3.简单测试

	@Test
	public void contextLoads() throws InterruptedException {
		User user = userService.selectUserById(1);
		System.out.println("第一次调用:" + user);

		Thread.sleep(2000);
		User user2 = userService.selectUserById(1);
		System.out.println("2秒之后调用:" + user2);

		Thread.sleep(4000);
		User user3 = userService.selectUserById(1);
		System.out.println("再过4秒之后调用:" + user3);
	}

结果:
在这里插入图片描述

三、手动维护缓存数据

缓存注解来使用ehcache,但在实际应用中很不灵活。以下为简单测试,相当于手动放入缓存,在需要的时候取出。
注意CacheManager 的包

package com.example.ehcache.utils;

import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;

@Component
public class MyCacheManager {

	@Resource
	private CacheManager cacheManager;

	public void put(String cacheName, String key, Object value) {
		Cache cache = cacheManager.getCache(cacheName);
		cache.put(key,value);
	}

	public Object get(String cacheName, String key) {
		Cache cache = cacheManager.getCache(cacheName);
		return cache.get(String.valueOf(key), Object.class);
	}


	public void del(String cacheName,String key) {
		cacheManager.getCache(cacheName).put(key,null);
	}
}

测试

	@Test
	public void test2 (){
		List<User> userList = userService.listUser();
		myCacheManager.put("CONSTANT","userList",userList);
		List<User> cacheUserList = (List<User>) myCacheManager.get("CONSTANT","userList");
		System.out.println("userList集合:"+cacheUserList);
		myCacheManager.del("CONSTANT","userList");
		//清除后再次获取
		List<User> cacheUserList2 = (List<User>) myCacheManager.get("CONSTANT","userList");
		System.out.println("userList集合:"+cacheUserList2);
	}

测试结果

在这里插入图片描述
测试demo代码:https://gitee.com/laputa219/springboot_demo/tree/master/boot_ehcache

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值