Skip to content

Commit

Permalink
Merge pull request #36 from grails-plugins/34_service_missing_memoize…
Browse files Browse the repository at this point in the history
…Object

Issue #34 service missing memoize object
  • Loading branch information
tednaleid committed May 29, 2014
2 parents 394c91a + 297f46c commit 7a8e91c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
2 changes: 1 addition & 1 deletion application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Grails Metadata file
#Wed Sep 25 16:10:07 PDT 2013
app.grails.version=2.3.7
app.grails.version=2.3.8
app.name=grails-redis
21 changes: 16 additions & 5 deletions grails-app/services/grails/plugin/redis/RedisService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package grails.plugin.redis

import com.google.gson.Gson
import redis.clients.jedis.Jedis
import redis.clients.jedis.Pipeline
import redis.clients.jedis.Transaction
Expand Down Expand Up @@ -321,10 +322,6 @@ class RedisService {
domainClass.load(domainId)
}

// Long persistDomainId(Object domainInstance, String key, Map options) {
// return persistDomainId(domainInstance?.id as Long, key, options.expire)
// }

Long persistDomainId(Long domainId, String key, Integer expire) {
if(domainId) {
withOptionalPipeline { pipeline ->
Expand All @@ -334,10 +331,24 @@ class RedisService {
}
}
}
// if (domainId) withRedis { Jedis redis -> redis.set(key, domainId.toString()) }
domainId
}

def memoizeObject(Class clazz, String key, Integer expire, Closure closure) {
memoizeObject(clazz, key, [expire: expire], closure)
}

def memoizeObject(Class clazz, String key, Map options = [:], Closure closure) {
Gson gson = new Gson()

String memoizedJson = memoize(key, options) { ->
def original = closure()
gson.toJson(original)
}

gson.fromJson(memoizedJson, clazz)
}

// deletes all keys matching a pattern (see redis "keys" documentation for more)
// OK for low traffic methods, but expensive compared to other redis commands
// perf test before relying on this rather than storing your own set of keys to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,24 @@ import java.util.Map;
import org.codehaus.groovy.ast.ASTNode
import org.codehaus.groovy.ast.ClassHelper;
import org.codehaus.groovy.ast.ClassNode
import org.codehaus.groovy.ast.FieldNode;
import org.codehaus.groovy.ast.MethodNode
import org.codehaus.groovy.ast.PropertyNode;
import org.codehaus.groovy.ast.VariableScope;
import org.codehaus.groovy.ast.builder.AstBuilder;
import org.codehaus.groovy.ast.expr.ArgumentListExpression
import org.codehaus.groovy.ast.expr.ClassExpression;
import org.codehaus.groovy.ast.expr.ClosureExpression
import org.codehaus.groovy.ast.expr.ConstantExpression
import org.codehaus.groovy.ast.expr.ConstructorCallExpression;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.expr.MethodCallExpression
import org.codehaus.groovy.ast.expr.VariableExpression
import org.codehaus.groovy.control.CompilePhase
import org.codehaus.groovy.control.SourceUnit
import org.codehaus.groovy.ast.stmt.BlockStatement
import org.codehaus.groovy.ast.stmt.ExpressionStatement
import org.codehaus.groovy.ast.stmt.ReturnStatement
import org.codehaus.groovy.ast.stmt.Statement
import org.codehaus.groovy.classgen.VariableScopeVisitor

import com.google.gson.Gson

import grails.plugin.redis.RedisService
import static org.springframework.asm.Opcodes.ACC_PRIVATE
import static org.springframework.asm.Opcodes.ACC_PUBLIC

import org.codehaus.groovy.transform.GroovyASTTransformation

Expand Down Expand Up @@ -132,7 +124,6 @@ class MemoizeObjectASTTransformation extends AbstractMemoizeASTTransformation {
if(!validateMemoizeProperties(astNodes, sourceUnit, keyString, expire, clazz)) {
return
}
//***************************************************************************

memoizeProperties.put(KEY, keyString)
memoizeProperties.put(CLAZZ, clazz)
Expand Down
37 changes: 37 additions & 0 deletions test/integration/grails/plugin/redis/RedisServiceTests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,43 @@ class RedisServiceTests {
assert NO_EXPIRATION_TTL < redisService.ttl("mykey")
}

@Test
public void testMemoizeObject_simpleMapOfStrings() {
Map<String, String> map = [foo: "bar", baz: "qux"]

def calledCount = 0
def cacheMissClosure = {
calledCount += 1
map
}

def cacheMissValue = redisService.memoizeObject(Map.class, "mykey", cacheMissClosure)

assert 1 == calledCount
assert "bar" == cacheMissValue.foo
assert "qux" == cacheMissValue.baz
assert NO_EXPIRATION_TTL == redisService.ttl("mykey")

def cacheHitValue = redisService.memoizeObject(Map.class, "mykey", cacheMissClosure)

assert 1 == calledCount
assert "bar" == cacheHitValue.foo
assert "qux" == cacheHitValue.baz
}


@Test
public void testMemoizeObject_withTTL() {
Map<String, String> map = [foo: "bar", baz: "qux"]
assert 0 > redisService.ttl("mykey")

def cacheMissValue = redisService.memoizeObject(Map.class, "mykey", 60) { -> map }

assert "bar" == cacheMissValue.foo
assert "qux" == cacheMissValue.baz
assert NO_EXPIRATION_TTL < redisService.ttl("mykey")
}


@Test
public void testDeleteKeysWithPattern() {
Expand Down
2 changes: 1 addition & 1 deletion test/projects/default/application.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Grails Metadata file
#Sat May 10 13:49:46 CDT 2014
app.grails.version=2.3.7
app.grails.version=2.3.8
app.name=default
app.version=0.1

0 comments on commit 7a8e91c

Please sign in to comment.