Skip to content

Commit

Permalink
Cache tests + cache tag
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumebort committed Sep 30, 2010
1 parent fa3160a commit 300ae7b
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 1 deletion.
2 changes: 1 addition & 1 deletion framework/src/play/cache/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public static void stop() {
* Utility that check that an object is serializable.
*/
static void checkSerializable(Object value) {
if(!(value instanceof Serializable)) {
if(value != null && !(value instanceof Serializable)) {
throw new CacheException("Cannot cache a non-serializable value of type " + value.getClass().getName(), new NotSerializableException(value.getClass().getName()));
}
}
Expand Down
17 changes: 17 additions & 0 deletions framework/src/play/templates/FastTags.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import play.cache.Cache;
import play.data.validation.Error;
import play.data.validation.Validation;
import play.exceptions.TagInternalException;
Expand All @@ -26,6 +27,22 @@
*/
public class FastTags {

public static void _cache(Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
String key = args.get("arg").toString();
String duration = null;
if(args.containsKey("for")) {
duration = args.get("for").toString();
}
Object cached = Cache.get(key);
if(cached != null) {
out.print(cached);
return;
}
String result = JavaExtensions.toString(body);
Cache.set(key, result, duration);
out.print(result);
}

public static void _verbatim(Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
out.println(JavaExtensions.toString(body));
}
Expand Down
24 changes: 24 additions & 0 deletions samples-and-tests/just-test-cases/app/controllers/UseCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package controllers;

import play.*;
import play.mvc.*;
import play.cache.*;

import java.util.*;

import models.*;

public class UseCache extends Controller {

public static void index() {
render();
}

@CacheFor("2s")
public static void getDate() {
Date a = new Date();
render(a);
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<strong id="a">${a}</strong>
17 changes: 17 additions & 0 deletions samples-and-tests/just-test-cases/app/views/UseCache/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<h1>Try cache</h1>

<p id="yop">
#{cache 'yop', for:'2s'}
${new java.util.Date()}
#{/cache}
</p>

<p id="kiki">
#{cache 'kiki'}
${new java.util.Date()}
#{/cache}
</p>

#{cache 'p'}
#{render 'Application/a.html' /}
#{/cache}
29 changes: 29 additions & 0 deletions samples-and-tests/just-test-cases/test/cache.test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#{selenium 'Test the cache tag'}

open('@{UseCache.index()}')

storeText('yop', 'firstDate')
storeText('kiki', 'secondDate')

open('@{UseCache.index()}')
assertText('yop', '$[firstDate]')
assertText('kiki', '$[secondDate]')

pause(3000)

open('@{UseCache.index()}')
assertNotText('yop', '$[firstDate]')
assertText('kiki', '$[secondDate]')

open('@{UseCache.getDate()}')
storeText('a', 'aDate')

open('@{UseCache.getDate()}')
assertText('a', '$[aDate]')

pause(3000)

open('@{UseCache.getDate()}')
assertNotText('a', '$[aDate]')

#{/selenium}

0 comments on commit 300ae7b

Please sign in to comment.