Skip to content

Session, Flash Sessions and Cache

pramode edited this page Jun 25, 2011 · 1 revision

Sessions

Example:

 public static void test() {
      System.out.println("session="+session.getId());
      if (session.get("key1") == null)
        session.put("key1", "val1");
      else
        System.out.println("key1=" + session.get("key1"));
 
 }   

The session is maintained as an HTTP cookie, PLAY_SESSION. Cookies are signed with a secret key.

Flash scope

Information stored in the "Flash" cookie is available in the next request only.

Example:

     public static void test() {
      if (flash.get("key1") == null)
        flash.put("key1", "val1");
      else
        System.out.println("key1=" + flash.get("key1"));
     }   

JVM Cache

The Play Cache is a server-side entity (it is stored in the JVM heap). The cache content is transient.

Example:

   public static void test() {
     String msg = Cache.get("foo", String.class);
     if (msg == null) {
        Cache.set("foo", "Hello,world", "30mn");
     } else {
        System.out.println(msg);
     }   
   }   
Clone this wiki locally