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的實戰系列的文章、由於使用的是新版本、需要慢慢調研可行性方案。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章