Created
March 1, 2013 11:29
-
-
Save nicmarti/5064066 to your computer and use it in GitHub Desktop.
My simple Redis client wrapper that uses Play2 configuration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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