Skip to content

Commit

Permalink
Fix #320
Browse files Browse the repository at this point in the history
Make the `key` parameter of the `@Cached` annotation optional

Signed-off-by: Clement Escoffier <clement.escoffier@gmail.com>
  • Loading branch information
cescoffier committed Sep 1, 2014
1 parent 3883a7b commit 5ce50ee
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
Expand Up @@ -34,12 +34,12 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface Cached {
/**
* The cache key to store the result in.
* The cache key to store the result in. Default to the request's URI.
*/
String key();
String key() default "";

/**
* The duration the action should be cached for (in second). Defaults to 0.
* The duration the action should be cached for (in second). Defaults to 0 corresponding to 365 days.
*/
int duration() default 0;
}
Expand Up @@ -19,6 +19,7 @@
*/
package org.wisdom.cache.ehcache;

import com.google.common.base.Strings;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Provides;
Expand Down Expand Up @@ -62,9 +63,16 @@ public Result call(Cached configuration, RequestContext context) throws Exceptio
boolean nocache =
HeaderNames.NOCACHE_VALUE.equalsIgnoreCase(context.context().header(HeaderNames.CACHE_CONTROL));

String key;
if (Strings.isNullOrEmpty(configuration.key())) {
key = context.request().uri();
} else {
key = configuration.key();
}

Result result = null;
if (!nocache) {
result = (Result) cache.get(configuration.key());
result = (Result) cache.get(key);
}

if (result == null) {
Expand All @@ -78,9 +86,11 @@ public Result call(Cached configuration, RequestContext context) throws Exceptio
} else {
duration = Duration.standardSeconds(configuration.duration());
}
cache.set(configuration.key(), result, duration);
LoggerFactory.getLogger(this.getClass()).info("Caching result of " + context.request().uri() + " for " +
configuration.duration() + " seconds");


cache.set(key, result, duration);
LoggerFactory.getLogger(this.getClass()).info("Caching result of {} for {} seconds (key:{})",
context.request().uri(), configuration.duration(), key);

return result;
}
Expand Down
Expand Up @@ -66,6 +66,38 @@ public void testCaching() throws Exception {
verify(interceptor.cache, times(2)).get("key");
}

@Test
public void testCachingWithoutKey() throws Exception {
CachedActionInterceptor interceptor = new CachedActionInterceptor();
interceptor.cache = mock(Cache.class);
Cached cached = mock(Cached.class);
when(cached.duration()).thenReturn(10);
when(cached.key()).thenReturn("");

RequestContext context = mock(RequestContext.class);
final Request request = mock(Request.class);
when(request.uri()).thenReturn("/my/url?withquery");
when(context.request()).thenReturn(request);
Context ctx = mock(Context.class);
when(context.context()).thenReturn(ctx);
when(context.context().header(anyString())).thenReturn(null);
final Result r = Results.ok("Result");
when(context.proceed()).thenReturn(r);

Result result = interceptor.call(cached, context);
assertThat(result.getRenderable().<String>content()).isEqualTo("Result");
assertThat(result).isEqualTo(r);
// Check that the result was put in cache.
verify(interceptor.cache, times(1)).get("/my/url?withquery");
verify(interceptor.cache, times(1)).set("/my/url?withquery", r, Duration.standardSeconds(10));

when(interceptor.cache.get("key")).thenReturn(r);
result = interceptor.call(cached, context);
assertThat(result).isEqualTo(r);

verify(interceptor.cache, times(2)).get("/my/url?withquery");
}

@Test
public void testCachingNoCache() throws Exception {
CachedActionInterceptor interceptor = new CachedActionInterceptor();
Expand Down

0 comments on commit 5ce50ee

Please sign in to comment.