2.0 Cache - @Caching annotation on Result/template @cache.Cache.getOrElse not working as documented

437 views
Skip to first unread message

Whittaker

unread,
Mar 18, 2012, 10:21:32 PM3/18/12
to play-fr...@googlegroups.com
Hello, I am having a problem with the @Caching annotation and the template tag @cache.Cache.getOrElse("cached-content", 3600) { some content } documented on http://www.playframework.org/documentation/2.0/JavaCache.

The @Caching annotation doesn't appear to work at all. No errors returned, but the result is never cached. I know the default cache plugin is working because adding code similar to the following works fine, my page/result is updated every 5 seconds:

     try {
            Result result = Cache.getOrElse("index", new Callable<Result>() {
                public Result call() {
                    int s = Calendar.getInstance().get(Calendar.SECOND);
                    return ok(views.html.test.render(Integer.toString(s)));
                }
            }, 5);
            return result;
        } catch (Exception e) {
            return internalServerError("um?");
        }

The @cache.Cache.getOrElse("cached-content", 3600) { some content } used in the scala template just renders an error. Perhaps it's just my lack of scala skills and knowledge of your framework.



peter hausel

unread,
Mar 19, 2012, 9:32:02 AM3/19/12
to play-fr...@googlegroups.com
are you referring to the @Cached annotation? As far as I can tell that one appears to be working as documented.

as for the template tag issue: If I understand your issue correctly, I would pass the required cached values to the template as arguments to avoid abstraction leakage (ie too much logic in the template).

If you rather want to go full frontal, you can use @{play.cache.Cache.get("mykey")} or the scala equivalent @{play.api.cache.Cache ...} 

HTH,
Peter

Whittaker

unread,
Mar 19, 2012, 11:27:48 AM3/19/12
to play-fr...@googlegroups.com
Yes, the @Cached annotation.

For example:

@Cached(key = "index", duration = 5)
    public static Result index() {
        long ms = Calendar.getInstance().getTimeInMillis();
        return ok(views.html.test.render(Long.toString(ms)));
    }

The time passed to the test template is not cached for 5 seconds. It always returns the current time. When I write code to manually cache the value similar to my original post it the value is cached. I was expecting the frame work to automatically cache the Result based on the @Cached annotation.


I agree with  your statement about the caching call in the template but I was expecting it to work as documented.

Thanks!
Reply all
Reply to author
Forward
0 new messages