playframework - redis缓存

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

playframework - redis缓存_第1张图片

使用教程

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的实战系列的文章、由于使用的是新版本、需要慢慢调研可行性方案。

你可能感兴趣的:(playframework)