Skip to content

Instantly share code, notes, and snippets.

@nicmarti
Created March 1, 2013 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicmarti/5064066 to your computer and use it in GitHub Desktop.
Save nicmarti/5064066 to your computer and use it in GitHub Desktop.
My simple Redis client wrapper that uses Play2 configuration
package library
import org.sedis.Pool
import redis.clients.jedis.{JedisPoolConfig, JedisPool}
import play.api.Play.current
import org.apache.commons.pool.impl.GenericObjectPool
/**
* Redis connection wrapper.
* You can configure the server details in the application.conf with:
* <ul>
* <li>redis.host=localhost</li>
* <li>redis.port=6379</li>
* <li>redis.timeout=2000</li>
* <li>redis.password=the redis password if required</li>
* </ul>
*/
object Redis {
// Small Redis to store FolioCards, DealCards, FolioCriteria, Webuser
private lazy val host = current.configuration.getString("redis.host").getOrElse("localhost")
private lazy val port = current.configuration.getInt("redis.port").getOrElse(6379)
private lazy val timeout = current.configuration.getInt("redis.timeout").getOrElse(30000)
private lazy val redisPassword = current.configuration.getString("redis.password").getOrElse(null)
lazy val jedisPoolConfig = {
var pool = new JedisPoolConfig
pool.setMaxActive(64)
pool.setMinIdle(1)
pool.setMaxWait(5000)
pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK)
pool
}
def checkIfConnected = {
play.Logger.info("=== Redis ===")
play.Logger.info("Checking " + host + ":" + port)
checkPool(pool, host, port)
}
def checkPool(poolCtx: Pool, _host: String, _port: Int) = {
poolCtx.withClient {
implicit client =>
play.Logger.info("Checking\t\t" + _host + ":" + _port + "> " + client.ping())
}
}
lazy val pool = new Pool(new JedisPool(jedisPoolConfig, host, port, timeout, redisPassword))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment