栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

Spring Boot:简单使用EhCache缓存框架

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot:简单使用EhCache缓存框架

我的环境是Gradle + Kotlin + Spring Boot,这里介绍EhCache缓存框架在Spring Boot上的简单应用。

在build.gradle文件添加依赖

compile("org.springframework.boot:spring-boot-starter-cache")compile("net.sf.ehcache:ehcache")

修改Application的配置,增加@EnableCaching配置

@MapperScan("com.xxx.xxx.dao")@SpringBootApplication(scanbasePackages= arrayOf("com.xxx.xxx"))// 启用缓存注解@EnableCaching// 启动定时器@EnableSchedulingopen class MyApplication {}fun main(args: Array) {
    SpringApplication.run(MyApplication::class.java, *args)
}

在resources添加文件ehcache.xml


    

    

    
使用

需要持久化的类需要实现Serializable序列化接口,不然无法写入硬盘

class User : Serializable {
    var id: Int = 0
    var name: String? = null

    constructor()
    
    constructor(id: Int, name: String?) {        this.id = id
        this.name = name
    }
}
// 获取缓存实例val userCache = CacheManager.getInstance().getCache("userCache")// 写入缓存val element = Element("1000", User(1000,"Wiki"))
userCache.put(element)// 读取缓存val user = userCache.get("1000").objectValue as User
写入硬盘

只要增加就可以写入文件,重启服务数据也不会丢失。

image.png



作者:ImWiki
链接:https://www.jianshu.com/p/3e35009ad3b9

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/235549.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号