首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >一.使用Ehcache

一.使用Ehcache

作者头像
Yuyy
发布2022-06-28 19:45:32
发布2022-06-28 19:45:32
6190
举报

本文最后更新于 878 天前,其中的信息可能已经有所发展或是发生改变。

1. pom

代码语言:javascript
复制
<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>2.8.9</version>
</dependency>

2. 创建 Ehcache 的配置文件

路径src/main/resources/hcahe.xml

代码语言:javascript
复制
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
    <diskStore path="java.io.tmpdir"/>
    <!--maxElementsInMemory:内存中最大缓存对象数  
        maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大  
        eternal:Element是否永久有效,一但设置了,timeout将不起作用  
        overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中  
        timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大  
        timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大   
        diskPersistent:是否缓存虚拟机重启期数据  
        diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒  
        diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区  
        memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)
    -->

    <!--defaultCache:echcache的默认缓存策略  -->
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </defaultCache>
    <!-- 自定义缓存策略 -->
    <cache name="users"
           maxElementsInMemory="10000"
           eternal="false"
           timeToIdleSeconds="120"
           timeToLiveSeconds="120"
           maxElementsOnDisk="10000000"
           diskExpiryThreadIntervalSeconds="120"
           memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </cache>
</ehcache>

3. 修改application.yml

代码语言:javascript
复制
spring:
  cache:
    ehcache:
      config: ehcache.xml

4. 修改启动类

代码语言:javascript
复制
@SpringBootApplication()
@MapperScan("com.yuyy.springbootdemo.mapper")
@EnableCaching
public class SpringBootDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootDemoApplication.class, args);
    }
}

5. 修改业务层

代码语言:javascript
复制
/**
 * @author yuyy
 * @date 2020/1/21 11:33
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    //@Cacheable:对当前查询的对象做缓存处理
    //value:选择缓存策略,可以自定义
    //key:配置缓存对象的key
    @Cacheable(value = "users",key = "#param.a")
    public User findUser(int id, TestParam param) {
        System.out.println(param);
        return userMapper.findUser(id);
    }
}

6. 测试

代码语言:javascript
复制
/**
 * @author yuyy
 * @date 2020/1/21 11:58
 */

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes= SpringBootDemoApplication.class)
public class UserServiceImplTest {

    @Autowired
    private UserService userService;
    @Test
    void findUser() {
        System.out.println(null==userService);
        TestParam param = TestParam.builder().a(1).b(0).c(0).build();
        TestParam param1 = TestParam.builder().a(0).b(1).c(0).build();
        TestParam param2 = TestParam.builder().a(0).b(0).c(1).build();
        System.out.println(userService.findUser(6, param));
        System.out.println(userService.findUser(6, param1));
        System.out.println(userService.findUser(6, param2));
    }
}

7. 结果

  • 只执行了两次sql,因为第二次和第三次的key相同,所以第三次使用的是缓存里的

8. 清除缓存

  • UserServiceImpl
代码语言:javascript
复制
@Override
//清除缓存
//value:绑定缓存策略
@CacheEvict(value = "users",allEntries=true)
public void cacheEvict() {
}
  • Test
代码语言:javascript
复制
System.out.println(userService.findUser(6, param));
System.out.println(userService.findUser(6, param1));
userService.cacheEvict();
System.out.println(userService.findUser(6, param2));

Post Views: 392

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. pom
  • 2. 创建 Ehcache 的配置文件
  • 3. 修改application.yml
  • 4. 修改启动类
  • 5. 修改业务层
  • 6. 测试
  • 7. 结果
  • 8. 清除缓存
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档