mumoshu/play2-memcached

Document Example of Using Elasticache

torbensky opened this issue · 1 comments

We are interested in using this plugin to connect to an Amazon Elasticache cluster. I noticed that a pull request for this feature was merged and the issue closed, but I am unsure how to configure a project to make use of this functionality. There doesn't appear to be anything in the documentation yet. Unfortunately I do not know Scala well as we are a Java shop, so going by the source code is a bit difficult.

We are especially interested in the cluster auto-discovery so we don't have to worry about maintaining a list of our cache instances and can auto-scale our cluster without having to redeploy the application.

It would be nice if the documentation would cover:

  • How to configure Build.scala for any required dependencies. Specifically, how to include the Elasticache client jar(s)
  • How to configure application.conf in this scenario. Specifically, it would be good to know how to configure the Elasticache endpoint for node auto-discovery.

Thanks!

This is a really old issue, but I found it while attempting to do the same thing and figure'd I'd write down the steps I used to use this module in my play 2.4 application.

Step 1. Setup a memcached cluster in your AWS account. It will provide you with a configuration endpoint like nameofcache.xxx.cfg.region.yyy.amazonaws.com:11211 this is what you'll use in your configuration later.

Step 2. Setup your build.sbt to include the appropriate libraries

libraryDependencies ++= Seq(
	cache,
	"com.github.mumoshu" %% "play2-memcached-play24" % "0.7.0" exclude("net.spy", "spymemcached"),
	"com.amazonaws" % "elasticache-java-cluster-client" % "1.1.1"
)

Step 3. configure your application's configuration file to disable the EhCache module and enable the Memcache one:

play.modules.enabled+="com.github.mumoshu.play2.memcached.MemcachedModule"
play.modules.disabled+="play.api.cache.EhCacheModule"
play.modules.cache.defaultCache=default
play.modules.cache.bindCaches=["db-cache", "user-cache", "session-cache"]

# Tell play2-memcached where your memcached host is located at
memcached.host="nameofcache.xxx.cfg.region.yyy.amazonaws.com:11211"

By using the host with cfg in the url and having the cluster client jar included, the auto-discovery will automatically be used.

Note that for the auto-discovery to work you'll need to be running your application in the AWS VPC. It is possible to use an SSH tunnel to connect to the AWS environment by modifying your hosts file, however when I did this for testing purposes I just swapped my cfg url out for a direct memcache host to avoid any errors from not having every node mapped in my local hosts file. The important trick that I use is that I connect to an ec2 istance that has access to the node, and forward my ports like so:

ssh -L 11211:nameofcache.xxx.0001.region.yyy.amazonaws.com:11211 -i ~/.ssh/ldskeypair.pem ec2-user@XX.XXX.XX.XX

and this matches up with my /etc/hosts file:

127.0.0.1 nameofcache.xxx.0001.region.yyy.amazonaws.com

and then I'm able to connect from my local application to a memcache instance running in AWS without any trouble. I hope this helps out anyone who runs into the same issue.