cfredis is a ColdFusion client for Redis. It acts as a wrapper for Jedis, a Java client for Redis. It has been tested on ColdFusion 8, ColdFusion 9, and ColdFusion 10.
cfredis requires Jedis and Apache Commons Pool, specifically jedis-2.1.0.jar and commons-pool-1.6-bin.tar.gz.
Within the commons-pool-1.6-bin.tar.gz
archive, you will find commons-pool-1.6.jar
. Copy jedis-2.1.0.jar
and commons-pool-1.6.jar
to _cfroot_/lib
and restart ColdFusion. You may also use JavaLoader to include jedis-2.1.0.jar
and commons-pool-1.6.jar
.
Copy src/cfc/cfredis.cfc to wherever you store your CFCs or clone the cfredis repository into your webroot.
Place the following initialization code in the OnApplicationStart
method in Application.cfc
, in Application.cfm
, or in OnRequestStart.cfm
:
<cfscript>
local.redisHost = "localhost"; // redis server hostname or ip address
local.redisPort = 6379; // redis server ip address
// Configure connection pool
local.jedisPoolConfig = CreateObject("java", "redis.clients.jedis.JedisPoolConfig");
local.jedisPoolConfig.init();
local.jedisPoolConfig.testOnBorrow = false;
local.jedisPoolConfig.testOnReturn = false;
local.jedisPoolConfig.testWhileIdle = true;
local.jedisPoolConfig.maxActive = 100;
local.jedisPoolConfig.maxIdle = 5;
local.jedisPoolConfig.numTestsPerEvictionRun = 10;
local.jedisPoolConfig.timeBetweenEvictionRunsMillis = 10000;
local.jedisPoolConfig.maxWait = 3000;
local.jedisPool = CreateObject("java", "redis.clients.jedis.JedisPool");
local.jedisPool.init(local.jedisPoolConfig, local.redisHost, local.redisPort);
// The "cfc.cfredis" component name will change depending on where you put cfredis
local.redis = CreateObject("component", "cfc.cfredis").init();
local.redis.connectionPool = local.jedisPool;
</cfscript>
<cflock scope="Application" type="exclusive" timeout="10">
<cfset application.redis = local.redis />
</cflock>
Use the application.redis
object to execute Redis commands:
<cfset application.redis.set("your:key:name", "key value") />
<cfset value = application.redis.get("your:key:name") />
I've included a number of examples using cfredis.
cfredis implements all of the Redis methods implemented in redis.clients.jedis.Jedis with the following changes:
-
ltrim
has been renamed to_ltrim
to avoid conflicts with the built-in CF functionLTrim()
-
The following overloaded Jedis methods have been combined to singular CF methods:
sort
zinterstore
zrangeByScore
zrangeByScoreWithScores
zrevrangeByScore
zrevrangeByScoreWithScores
zunionstore
-
Transactions and Pipelining are not yet supported
If you have any problems with cfredis, please submit an issue:
https://github.com/MWers/cfredis/issues
If you'd like to help to make cfredis better, please fork this project and submit a pull request. A great place to start would be in creating MXUnit tests. They can be based on the Jedis JUnit tests here:
https://github.com/xetorthio/jedis/tree/master/src/test/java/redis/clients/jedis/tests
Thanks!