Skip to content

simonedeponti/play26-lettuce

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

play26-lettuce

Build Status

A Redis cache plugin for Play 2.6+, based on lettuce (https://lettuce.io/).

It does support Scala (2.12 and 2.11) and Java AsyncCacheApi and SyncCacheApi.

It supports SSL, and therefore is suitable for use with Azure Redis Cache.

Usage

Add dependency to sbt (along with the resolver to BinTray):

libraryDependencies += "com.github.simonedeponti" %% "play26-lettuce" % "1.0.0"

resolvers ++= Seq(
  "simonedeponti-bintray" at "https://dl.bintray.com/simonedeponti/maven"
)

Enable the module:

# Enable redis cache plugin
play.modules.enabled += "com.github.simondeponti.play26lettuce.LettuceModule"

Then (optionally) enable Kryo serialization, by adding:

libraryDependencies += "com.twitter" %% "chill-akka" % "0.9.3"
libraryDependencies += "com.twitter" % "chill-java" % "0.9.3"

And then adding in you configuration something like this:

akka {
  actor {
    kryo {
      idstrategy = default
      resolve-subclasses = true
    }
    serializers {
      java = "akka.serialization.JavaSerializer"
      kryo = "com.twitter.chill.akka.AkkaSerializer"
    }

    serialization-bindings {
      "scala.Product" = kryo
      "scala.None" = kryo
      "scala.Some" = kryo
      "scala.collection.Seq" = kryo
      "scala.collection.Map" = kryo
      "my.custom.Class" = kryo
      "java.io.Serializable" = kryo
    }
  }
}

Configuration

After doing all of the above, one must set the redis endpoint for each cache by specifying lettuce.$cachename.url:

play.cache {
  defaultCache = "default"
  bindCaches = []
}

lettuce.default.url = "redis://localhost/0"

If you need additional caches, put them into bindCaches and then create the corresponding lettuce.$cacheName.url config key.

Never specify the exact same URL for two caches: please use different databases to separate them

Correct example:

play.cache {
  defaultCache = "default"
  bindCaches = ["users"]
}

lettuce.default.url = "redis://localhost/0"
lettuce.users.url = "redis://localhost/1"

Wrong example (the two caches might have key conflicts, and will throw an error upon start):

play.cache {
  defaultCache = "default"
  bindCaches = ["users"]
}

lettuce.default.url = "redis://localhost/0"
lettuce.users.url = "redis://localhost/0"  # Same URL, everything explodes in a ball of fire.

Optionally, for each cache, a timeout can be set for syncronous wrappers:

lettuce.default.syncTimeout = 3s

History

v1.0.0

  • Added multiset/multiget support in custom interface (dominics)
  • No configuration silently exports no bindings (dominics)
  • Better support for multiple caches without using KEYS command but mandating separated databases for different caches

v0.2.3

  • Timeout for sync wrappers is configurable

v0.2.2

  • More robust serialization

v0.2.1

  • Cross building scala 2.12 & 2.11

v0.2.0

  • Added support for Scala sync API
  • Added support for Java API (sync & async)

v0.1

  • Initial version