Skip to content

Commit

Permalink
Adding support for Amazon Elasticache (http://aws.amazon.com/elastica…
Browse files Browse the repository at this point in the history
…che/).  To use, add your elasticache configuration endpoint to your conf as endpointelasticache.config.endpoint={config.endpoint}.  For example, elasticache.config.endpoint="mycachename.asdfjk.cfg.use1.cache.amazonaws.com:11211".
  • Loading branch information
kamatsuoka committed Mar 13, 2013
1 parent 32235ed commit cf8fac1
Showing 1 changed file with 28 additions and 25 deletions.
Expand Up @@ -37,35 +37,38 @@ class MemcachedPlugin(app: Application) extends CachePlugin {
lazy val client = {
System.setProperty("net.spy.log.LoggerImpl", "com.github.mumoshu.play2.memcached.Slf4JLogger")

lazy val singleHost = app.configuration.getString("memcached.host").map(AddrUtil.getAddresses)
lazy val multipleHosts = app.configuration.getString("memcached.1.host").map { _ =>
def accumulate(nb: Int): String = {
app.configuration.getString("memcached." + nb + ".host").map { h => h + " " + accumulate(nb + 1) }.getOrElse("")
app.configuration.getString("elasticache.config.endpoint").map { endpoint =>
new MemcachedClient(AddrUtil.getAddresses(endpoint))
}.getOrElse {
lazy val singleHost = app.configuration.getString("memcached.host").map(AddrUtil.getAddresses)
lazy val multipleHosts = app.configuration.getString("memcached.1.host").map { _ =>
def accumulate(nb: Int): String = {
app.configuration.getString("memcached." + nb + ".host").map { h => h + " " + accumulate(nb + 1) }.getOrElse("")
}
AddrUtil.getAddresses(accumulate(1))
}
AddrUtil.getAddresses(accumulate(1))
}

val addrs = singleHost.orElse(multipleHosts)
.getOrElse(throw new RuntimeException("Bad configuration for memcached: missing host(s)"))

app.configuration.getString("memcached.user").map { memcacheUser =>
val memcachePassword = app.configuration.getString("memcached.password").getOrElse {
throw new RuntimeException("Bad configuration for memcached: missing password")
}
val addrs = singleHost.orElse(multipleHosts)
.getOrElse(throw new RuntimeException("Bad configuration for memcached: missing host(s)"))

// Use plain SASL to connect to memcached
val ad = new AuthDescriptor(Array("PLAIN"),
new PlainCallbackHandler(memcacheUser, memcachePassword))
val cf = new ConnectionFactoryBuilder()
.setProtocol(ConnectionFactoryBuilder.Protocol.BINARY)
.setAuthDescriptor(ad)
.build()
app.configuration.getString("memcached.user").map { memcacheUser =>
val memcachePassword = app.configuration.getString("memcached.password").getOrElse {
throw new RuntimeException("Bad configuration for memcached: missing password")
}

new MemcachedClient(cf, addrs)
}.getOrElse {
new MemcachedClient(addrs)
// Use plain SASL to connect to memcached
val ad = new AuthDescriptor(Array("PLAIN"),
new PlainCallbackHandler(memcacheUser, memcachePassword))
val cf = new ConnectionFactoryBuilder()
.setProtocol(ConnectionFactoryBuilder.Protocol.BINARY)
.setAuthDescriptor(ad)
.build()

new MemcachedClient(cf, addrs)
}.getOrElse {
new MemcachedClient(addrs)
}
}

}

import java.io._
Expand Down Expand Up @@ -118,7 +121,7 @@ class MemcachedPlugin(app: Application) extends CachePlugin {
}
)
} catch {
case e =>
case e: Throwable =>
logger.error("An error has occured while getting the value from memcached" , e)
future.cancel(false)
None
Expand Down

0 comments on commit cf8fac1

Please sign in to comment.