Skip to content

Commit

Permalink
Adding finally block with simplified close, upgrading jedis to 2.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
coestre committed Dec 9, 2016
1 parent 72e3eb4 commit 78e7881
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 82 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ class MyService {

Release Notes Grails 3.x
=============
* 2.0.5 - released 12/08/2016 - Upgrading to Jedis 2.9.0. Putting close on the resource in withRedis block.
* 2.0.4 - released 02/26/2016 - Upgrading to Jedis 2.8.0 and Gson 2.6.1. Official name for package to `org.grails.plugins:redis`. Moved bintray location. *Breaking Change*
* 2.0.3 - released 02/01/2016 - Attempting to cleaup the code and naming for plugin/package
* 2.0.2 - released 06/25/2015 - Adding support for external configuration of sentinels
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ ext {
gradleWrapperVersion = project.gradleWrapperVersion
}

version "2.0.4"
version "2.0.5"
group "org.grails.plugins"
sourceCompatibility = 1.7
targetCompatibility = 1.7
Expand Down Expand Up @@ -66,7 +66,7 @@ dependencies {

console "org.grails:grails-console"

compile 'redis.clients:jedis:2.8.0'
compile 'redis.clients:jedis:2.9.0'
compile 'com.google.code.gson:gson:2.6.1'
}

Expand Down
3 changes: 2 additions & 1 deletion grails-app/conf/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ spring:
grails:
redis:
poolConfig:
maxIdle: 10
maxIdle: 1
maxTotal: 10
doesnotexist: true
111 changes: 56 additions & 55 deletions src/main/groovy/grails/plugins/redis/RedisService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ class RedisService {

boolean transactional = false

RedisService withConnection(String connectionName){
if(grailsApplication.mainContext.containsBean("redisService${connectionName.capitalize()}")){
return (RedisService)grailsApplication.mainContext.getBean("redisService${connectionName.capitalize()}")
RedisService withConnection(String connectionName) {
if (grailsApplication.mainContext.containsBean("redisService${connectionName.capitalize()}")) {
return (RedisService) grailsApplication.mainContext.getBean("redisService${connectionName.capitalize()}")
}
if (log.errorEnabled) log.error("Connection with name redisService${connectionName.capitalize()} could not be found, returning default redis instead")
return this
}

def withPipeline(Closure closure, Boolean returnAll=false) {
def withPipeline(Closure closure, Boolean returnAll = false) {
withRedis { Jedis redis ->
Pipeline pipeline = redis.pipelined()
closure(pipeline)
Expand All @@ -55,8 +55,7 @@ class RedisService {
Pipeline pipeline = redis.pipelined()
clos(pipeline)
returnAll ? pipeline.syncAndReturnAll() : pipeline.sync()
}
else {
} else {
return clos()
}

Expand All @@ -68,7 +67,7 @@ class RedisService {
Transaction transaction = redis.multi()
try {
closure(transaction)
} catch(Exception exception) {
} catch (Exception exception) {
transaction.discard()
throw exception
}
Expand Down Expand Up @@ -99,15 +98,15 @@ class RedisService {
def withRedis(Closure closure) {
Jedis redis = redisPool.resource
try {
def ret = closure(redis)
redisPool.returnResource(redis)
return ret
} catch(JedisConnectionException jce) {
redisPool.returnBrokenResource(redis)
return closure(redis)
} catch (JedisConnectionException jce) {
throw jce
} catch(Exception e) {
redisPool.returnResource(redis)
} catch (Exception e) {
throw e
} finally {
if (redis) {
redis.close()
}
}
}

Expand All @@ -119,24 +118,22 @@ class RedisService {
def withOptionalRedis(Closure clos) {
Jedis redis
try {
redis = redisPool.resource
redis = redisPool.resource
}
catch (JedisConnectionException jce) {
log.info('Unreachable redis store trying to retrieve redis resource. Please check redis server and/or config!')
}

try {
def ret = clos(redis)
if (redis) redisPool.returnResource(redis)
ret
}
catch (JedisConnectionException jce) {
return clos(redis)
} catch (JedisConnectionException jce) {
log.error('Unreachable redis store trying to return redis pool resource. Please check redis server and/or config!', jce)
if (redis) redisPool.returnBrokenResource(redis)
}
catch (Throwable t) {
if (redis) redisPool.returnResource(redis)
} catch (Throwable t) {
throw t
} finally {
if (redis) {
redis.close()
}
}
}

Expand All @@ -151,12 +148,12 @@ class RedisService {
if (redis) return redis.get(key)
}

if(!result) {
if (!result) {
if (log.debugEnabled) log.debug "cache miss: $key"
result = closure()
if(result) withOptionalRedis { Jedis redis ->
if (result) withOptionalRedis { Jedis redis ->
if (redis) {
if(options?.expire) {
if (options?.expire) {
redis.setex(key, options.expire, result as String)
} else {
redis.set(key, result as String)
Expand All @@ -178,13 +175,13 @@ class RedisService {
if (redis) return redis.hgetAll(key)
}

if(!hash) {
if (!hash) {
if (log.debugEnabled) log.debug "cache miss: $key"
hash = closure()
if(hash) withOptionalRedis { Jedis redis ->
if (hash) withOptionalRedis { Jedis redis ->
if (redis) {
redis.hmset(key, hash)
if(options?.expire) redis.expire(key, options.expire)
if (options?.expire) redis.expire(key, options.expire)
}
}
} else {
Expand All @@ -205,13 +202,13 @@ class RedisService {
if (redis) return redis.hget(key, field)
}

if(!result) {
if (!result) {
if (log.debugEnabled) log.debug "cache miss: $key.$field"
result = closure()
if(result) withOptionalRedis { Jedis redis ->
if (result) withOptionalRedis { Jedis redis ->
if (redis) {
redis.hset(key, field, result as String)
if(options?.expire && redis.ttl(key) == NO_EXPIRATION_TTL) redis.expire(key, options.expire)
if (options?.expire && redis.ttl(key) == NO_EXPIRATION_TTL) redis.expire(key, options.expire)
}
}
} else {
Expand All @@ -232,13 +229,13 @@ class RedisService {
if (redis) redis.zscore(key, member)
}

if(!score) {
if (!score) {
if (log.debugEnabled) log.debug "cache miss: $key.$member"
score = closure()
if(score) withOptionalRedis { Jedis redis ->
if (score) withOptionalRedis { Jedis redis ->
if (redis) {
redis.zadd(key, score, member)
if(options?.expire && redis.ttl(key) == NO_EXPIRATION_TTL) redis.expire(key, options.expire)
if (options?.expire && redis.ttl(key) == NO_EXPIRATION_TTL) redis.expire(key, options.expire)
}
}
} else {
Expand All @@ -253,7 +250,7 @@ class RedisService {

List memoizeDomainList(Class domainClass, String key, Map options = [:], Closure closure) {
List<Long> idList = getIdListFor(key)
if(idList) return hydrateDomainObjectsFrom(domainClass, idList)
if (idList) return hydrateDomainObjectsFrom(domainClass, idList)

def domainList = withOptionalRedis { Jedis redis ->
closure(redis)
Expand All @@ -271,7 +268,7 @@ class RedisService {
// used when we just want the list of Ids back rather than hydrated objects
List<Long> memoizeDomainIdList(Class domainClass, String key, Map options = [:], Closure closure) {
List<Long> idList = getIdListFor(key)
if(idList) return idList
if (idList) return idList

def domainList = closure()

Expand All @@ -285,7 +282,7 @@ class RedisService {
if (redis) return redis.lrange(key, 0, -1)
}

if(idList) {
if (idList) {
if (log.debugEnabled) log.debug "$key cache hit, returning ${idList.size()} ids"
List<Long> idLongList = idList*.toLong()
return idLongList
Expand All @@ -296,16 +293,16 @@ class RedisService {
if (log.debugEnabled) log.debug "$key cache miss, memoizing ${domainList?.size() ?: 0} ids"
withOptionalPipeline { pipeline ->
if (pipeline) {
for(domain in domainList) {
for (domain in domainList) {
pipeline.rpush(key, domain.id as String)
}
if(expire) pipeline.expire(key, expire)
if (expire) pipeline.expire(key, expire)
}
}
}

protected List hydrateDomainObjectsFrom(Class domainClass, List<Long> idList) {
if(domainClass && idList) {
if (domainClass && idList) {
//return domainClass.findAllByIdInList(idList, [cache: true])
return idList.collect { id -> domainClass.load(id) }
}
Expand All @@ -322,16 +319,16 @@ class RedisService {
Long domainId = withOptionalRedis { redis ->
redis?.get(key)?.toLong()
}
if(!domainId) domainId = persistDomainId(closure()?.id as Long, key, options.expire)
if (!domainId) domainId = persistDomainId(closure()?.id as Long, key, options.expire)
domainClass.load(domainId)
}

Long persistDomainId(Long domainId, String key, Integer expire) {
if(domainId) {
if (domainId) {
withOptionalPipeline { pipeline ->
if (pipeline) {
pipeline.set(key, domainId.toString())
if(expire) pipeline.expire(key, expire)
if (expire) pipeline.expire(key, expire)
}
}
}
Expand All @@ -351,7 +348,7 @@ class RedisService {
gson.toJson(original)
}

gson.fromJson((String)memoizedJson, clazz)
gson.fromJson((String) memoizedJson, clazz)
}

// deletes all keys matching a pattern (see redis "keys" documentation for more)
Expand All @@ -362,7 +359,7 @@ class RedisService {
if (log.infoEnabled) log.info("Cleaning all redis keys with pattern [${keyPattern}]")
withRedis { Jedis redis ->
String[] keys = redis.keys(keyPattern)
if(keys) redis.del(keys)
if (keys) redis.del(keys)
}
}

Expand All @@ -375,13 +372,15 @@ class RedisService {
if (redis) return redis.lrange(key, 0, -1)
}

if(!list) {
if (!list) {
if (log.debugEnabled) log.debug "cache miss: $key"
list = closure()
if(list) withOptionalPipeline { pipeline ->
if (list) withOptionalPipeline { pipeline ->
if (pipeline) {
for(obj in list) { pipeline.rpush(key, obj) }
if(options?.expire) pipeline.expire(key, options.expire)
for (obj in list) {
pipeline.rpush(key, obj)
}
if (options?.expire) pipeline.expire(key, options.expire)
}
}
} else {
Expand All @@ -399,13 +398,15 @@ class RedisService {
if (redis) return redis.smembers(key)
}

if(!set) {
if (!set) {
if (log.debugEnabled) log.debug "cache miss: $key"
set = closure()
if(set) withOptionalPipeline { pipeline ->
if (set) withOptionalPipeline { pipeline ->
if (pipeline) {
for(obj in set) { pipeline.sadd(key, obj) }
if(options?.expire) pipeline.expire(key, options.expire)
for (obj in set) {
pipeline.sadd(key, obj)
}
if (options?.expire) pipeline.expire(key, options.expire)
}
}
} else {
Expand Down
Loading

0 comments on commit 78e7881

Please sign in to comment.