RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
EhCache缓存框架怎么在SpringBoot中使用-创新互联

EhCache缓存框架怎么在Spring Boot中使用?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

成都服务器托管,创新互联提供包括服务器租用、德阳服务器托管、带宽租用、云主机、机柜租用、主机租用托管、CDN网站加速、域名注册等业务的一体化完整服务。电话咨询:13518219792

在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
// 启动定时器
@EnableScheduling
open 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

写入硬盘

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

EhCache缓存框架怎么在Spring Boot中使用

看完上述内容,你们掌握EhCache缓存框架怎么在Spring Boot中使用的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


分享名称:EhCache缓存框架怎么在SpringBoot中使用-创新互联
转载来于:http://sczitong.cn/article/gicoh.html