EhCache缓存框架怎么在SpringBoot中使用-成都快上网建站

EhCache缓存框架怎么在SpringBoot中使用

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

泽普网站制作公司哪家好,找创新互联建站!从网页设计、网站建设、微信开发、APP开发、响应式网站开发等网站项目制作,到程序开发,运营维护。创新互联建站自2013年创立以来到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联建站

在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中使用
网页URL:http://kswjz.com/article/gseecg.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流