Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache evict not working with a custom key #51

Merged
merged 4 commits into from
May 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions grails-app/controllers/com/demo/DemoController.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ class DemoController {
render "Result: ${result}"
}

def cacheEvictAndGet(String key) {
basicCachingService.getDataEvict(key)
def result = basicCachingService.getData(key)
render "Result: ${result}"
}

def cacheEvictAllAndGet(String key) {
basicCachingService.getDataEvictAll()
def result = basicCachingService.getData(key)
render "Result: ${result}"
}

def cacheClearAndGet(String key) {
grailsCacheAdminService.clearCache('basic')
def result = basicCachingService.getData(key)
render "Result: ${result}"
}

def blockCache(int counter) {
[counter: counter]
}
Expand Down
8 changes: 8 additions & 0 deletions grails-app/services/com/demo/BasicCachingService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ class BasicCachingService {
"** ${value} **"
}

@CacheEvict(value = 'basic', key = { key } )
def getDataEvict(String key) {
}

@CacheEvict(value = 'basic', allEntries = true)
def getDataEvictAll() {
}

@Cacheable(value = 'basic', condition = { x < 10 })
def multiply(int x, int y) {
conditionalInvocationCounter++
Expand Down
96 changes: 94 additions & 2 deletions src/ast/groovy/grails/plugin/cache/CustomCacheKeyGenerator.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class CustomCacheKeyGenerator implements KeyGenerator, GrailsCacheKeyGenerator {
}

@SuppressWarnings("serial")
@EqualsAndHashCode
private static final class CacheKey implements Serializable {
final String targetClassName
final String targetMethodName
Expand All @@ -53,6 +52,53 @@ class CustomCacheKeyGenerator implements KeyGenerator, GrailsCacheKeyGenerator {
this.targetObjectHashCode = targetObjectHashCode
this.simpleKey = simpleKey
}
@Override
int hashCode() {
final int prime = 31
int result = 1
result = prime * result
+ ((simpleKey == null) ? 0 : simpleKey.hashCode())
result = prime * result
+ ((targetClassName == null) ? 0 : targetClassName
.hashCode())
result = prime * result
+ ((targetMethodName == null) ? 0 : targetMethodName
.hashCode())
result = prime * result + targetObjectHashCode
return result
}
@Override
boolean equals(Object obj) {
if (this.is(obj))
return true
if (obj == null)
return false
if (getClass() != obj.getClass())
return false
CacheKey other = (CacheKey) obj
if (simpleKey == null) {
if (other.simpleKey != null)
return false
} else if (!simpleKey.equals(other.simpleKey))
return false
else if ( simpleKey.equals(other.simpleKey) && !(simpleKey instanceof Map && ((Map)simpleKey).size() == 0 ) ) {
return true // equal if simpleKey is identical but not an empty map
}

if (targetClassName == null) {
if (other.targetClassName != null)
return false
} else if (!targetClassName.equals(other.targetClassName))
return false
if (targetMethodName == null) {
if (other.targetMethodName != null)
return false
} else if (!targetMethodName.equals(other.targetMethodName))
return false
if (targetObjectHashCode != other.targetObjectHashCode)
return false
return true
}
}

Object generate(Object target, Method method, Object... params) {
Expand All @@ -77,7 +123,6 @@ class CustomCacheKeyGenerator implements KeyGenerator, GrailsCacheKeyGenerator {
}


@EqualsAndHashCode
@CompileStatic
private static class TemporaryGrailsCacheKey implements Serializable {
final String targetClassName
Expand All @@ -92,6 +137,53 @@ class CustomCacheKeyGenerator implements KeyGenerator, GrailsCacheKeyGenerator {
this.targetObjectHashCode = targetObjectHashCode
this.simpleKey = simpleKey
}
@Override
int hashCode() {
final int prime = 31
int result = 1
result = prime * result
+ ((simpleKey == null) ? 0 : simpleKey.hashCode())
result = prime * result
+ ((targetClassName == null) ? 0 : targetClassName
.hashCode())
result = prime * result
+ ((targetMethodName == null) ? 0 : targetMethodName
.hashCode())
result = prime * result + targetObjectHashCode
return result
}
@Override
boolean equals(Object obj) {
if (this.is(obj))
return true
if (obj == null)
return false
if (getClass() != obj.getClass())
return false
TemporaryGrailsCacheKey other = (TemporaryGrailsCacheKey) obj
if (simpleKey == null) {
if (other.simpleKey != null)
return false
} else if (!simpleKey.equals(other.simpleKey))
return false
else if ( simpleKey.equals(other.simpleKey) && !(simpleKey instanceof Map && ((Map)simpleKey).size() == 0 ) ) {
return true // equal if simpleKey is identical but not an empty map
}

if (targetClassName == null) {
if (other.targetClassName != null)
return false
} else if (!targetClassName.equals(other.targetClassName))
return false
if (targetMethodName == null) {
if (other.targetMethodName != null)
return false
} else if (!targetMethodName.equals(other.targetMethodName))
return false
if (targetObjectHashCode != other.targetObjectHashCode)
return false
return true
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.demo

import geb.spock.GebSpec
import grails.test.mixin.integration.Integration
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.cache.CacheManager

@Integration
class CachingServiceIntegrationSpec extends GebSpec {
Expand Down Expand Up @@ -201,4 +203,49 @@ class CachingServiceIntegrationSpec extends GebSpec {
then:
browser.driver.pageSource.contains 'Result: ** Thin Lizzy'
}

void 'test basic cache evict key service'() {
when:
go '/demo/cachePut?key=band&value=Thin+Lizzy'

then:
browser.driver.pageSource.contains 'Result: ** Thin Lizzy **'

when:
go '/demo/cacheEvictAndGet?key=band'

then:
browser.driver.pageSource.contains 'Result: null'
}

void 'test basic cache evict all service'() {
when:
go '/demo/cachePut?key=band&value=Thin+Lizzy'

then:
browser.driver.pageSource.contains 'Result: ** Thin Lizzy **'

when:
go '/demo/cacheEvictAllAndGet?key=band'

then:
browser.driver.pageSource.contains 'Result: null'
}

void 'test basic cache clear service'() {
when:
go '/demo/cachePut?key=band&value=Thin+Lizzy'

then:
browser.driver.pageSource.contains 'Result: ** Thin Lizzy **'

when:
go '/demo/cacheClearAndGet?key=band'

then:
browser.driver.pageSource.contains 'Result: null'
}



}