playframework - redis缓存

接着上一篇的play framework 使用教程

使用教程

build.sbt 添加依赖

libraryDependencies += play.sbt.PlayImport.cacheApi
libraryDependencies += "com.github.karelcemus" %% "play-redis" % "2.4.0"

添加redis配置 application.conf

play.modules {
  # By default, Play will load any class called Module that is defined
  # in the root package (the "app" directory), or you can define them
  # explicitly below.
  # If there are any built-in modules that you want to enable, you can list them here.
  #enabled += my.application.Module
  enabled += play.api.cache.redis.RedisCacheModule
  # If there are any built-in modules that you want to disable, you can list them here.
  #disabled += ""
}
play.cache {
  # If you want to bind several caches, you can bind the individually
  #bindCaches = ["db-cache", "user-cache", "session-cache"]
  redis {
    host = 127.0.0.1
    port = 6379
    database = 0
    password = root
  }
}

使用方法

@Singleton
class HomeController @Inject()(cache: CacheApi, cc: ControllerComponents)(implicit assetsFinder: AssetsFinder)
  extends AbstractController(cc) {

  def index = Action {
    cache.set("username", "大猪")
    println(cache.get[String]("username"))

查看redis数据

最后

后面会慢慢放出play framework的实战系列的文章、由于使用的是新版本、需要慢慢调研可行性方案。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值