Maik Klein
Maik Klein

Reputation: 16148

Java Play2 - Smart cache

@Cached(key="homePage")
public static Result index() {
  return ok("Hello world");
}

The docs doesn't tell me much about smart caching. Is this really all I have to do?

What if the content changes? Does play automatically update the cache?

This seems to good to be true.

Update 1:

For some reason it does not work

 @Cached(key="homePage")
    public static Result index() {
      Logger.info("" + Cache.get("homePage"));
      return ok("Hello world");
    }

If I understood it correctly Logger.info("" + Cache.get("homePage")); should only get called once (if the cache is empty). But it gets called every time I refresh the page. Also the result is always null, so it doesn't even work.

Any ideas?

Update 2:

I tried to add the duradion like:

@Cached(key="homePage",duration=3000)

But it didn't help.

Upvotes: 2

Views: 1176

Answers (2)

Joel S
Joel S

Reputation: 516

@Cached seems to be broken in 2.0.4 and probably also for lower versions. Note that @With(CachedAction.class) has gone missing. It is fixed in current master though :)

Easy fix for now is to roll your own @Cached and CachedAnnotation. Just copy-and-paste from master and add to your project:

Upvotes: 0

Davz
Davz

Reputation: 1530

I have the feeling that the problem is due to the use of the @Cached annotation. In fact if you cache the value manually it works, but if you use the annotation (like described in the doc by the way- it doesn't seem to work.

The following piece of code can demonstrate it easily:

 @Cached(key="page1")
  public static Result page1() {
      java.util.Date d = Calendar.getInstance().getTime();
      return ok(page.render(d.toString()));
  }

  public static Result page2() {
      Result result = (Result) Cache.get("page2");
      if ( result == null ) {
          java.util.Date d = Calendar.getInstance().getTime();
          result = ok(page.render(d.toString()));
          Cache.set("page2", result);
      }   
      return result;
  }

With the following page

@(date: String)
@date

And routes

GET     /page1                      controllers.Application.page1()
GET     /page2                      controllers.Application.page2()

If you go to http://localhost:9000/page1 the date will change at every calls whereas it will be effectively be cached if you use http://localhost:9000/page2

It's some kind of workaround but it does the job.

Regarding your first question "What if the content changes? Does play automatically update the cache?", I think it's not the case and that you have to manually remove the entry from the cache (if you don't want to wait for the expiration date). For example:

  1. You put a value in the cache with an expiration time of 10 minutes
  2. Another business call makes the value in the cache obsolete and you want the user to be immediately informed so you have to remove the value from the cache immediately
  3. Next time the value won't be in the cache and will be recomputed with fresh data

It seems that the remove from cache will only be available on version 2.1 (ticket here) and that the workaround is to put something in the cache with the same key and a 1 second expiration.

Upvotes: 5

Related Questions