广告
返回顶部
首页 > 资讯 > 后端开发 > Python >springboot ehcache 配置使用方法代码详解
  • 662
分享到

springboot ehcache 配置使用方法代码详解

2024-04-02 19:04:59 662人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

EhCache是一个比较成熟的Java缓存框架,最早从hibernate发展而来, 是进程中的缓存系统,它提供了用内存,磁盘文件存储,以及分布式存储方式等多种灵活的cache管理方案

EhCache是一个比较成熟的Java缓存框架,最早从hibernate发展而来, 是进程中的缓存系统,它提供了用内存,磁盘文件存储,以及分布式存储方式等多种灵活的cache管理方案,快速简单。

SpringBoot对ehcache的使用非常支持,所以在springboot中只需做些配置就可使用,且使用方式也简易。

下面通过本文给大家介绍springboot ehcache 配置使用方法,具体内容如下所示:

1. pom 引入依赖


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

2.resources 目录下直接放个文件ehcache.xml


<ehcache xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
         updateCheck="false">

    <diskStore path="java.io.tmpdir"/>

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

3.在Service层 方法上加上注解

@CacheEvict(value="menucache", allEntries=true) ,更新缓存

@Cacheable(key="'menu-'+#parentId",value="menucache") 读取缓存,"'menu-'+#parentId" 通配符,也可以直接写死字符串

menucache 对应 上面 xmlname="menucache"



	@CacheEvict(value="menucache", allEntries=true)
	public void deleteMenuById(String MENU_ID) throws Exception{
		this.cleanRedis();
		menuMapper.deleteMenuById(MENU_ID);
	}

	
	@Cacheable(key="'menu-'+#parentId",value="menucache")
	public List<Menu> listSubMenuByParentId(String parentId) throws Exception {
		return menuMapper.listSubMenuByParentId(parentId);
	}

到此这篇关于springboot ehcache 配置使用方法代码详解的文章就介绍到这了,更多相关springboot ehcache 配置使用内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: springboot ehcache 配置使用方法代码详解

本文链接: https://www.lsjlt.com/news/129194.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作