barroco
barroco

Reputation: 3118

object session in playframework

How I can store an instance object foreach user session?

I have a class to modeling a complex algorithm. This algorithm is designed to run step-by-step. I need to instantiate objects of this class for each user. Each user should be able to advance step by step their instance.

Upvotes: 5

Views: 7858

Answers (4)

Jay Q.
Jay Q.

Reputation: 4999

Here's how you can save "objects" in a session. Basically, you serialize/deserialize objects to JSON and store it in the cookie.

https://stackoverflow.com/a/12032315/82976

Upvotes: 0

opensas
opensas

Reputation: 63525

from play documentation: http://www.playframework.org/documentation/1.1.1/cache

Play has a cache library and will use Memcached when used in a distributed environment.

If you don’t configure Memcached, Play will use a standalone cache that stores data in the JVM heap. Caching data in the JVM application breaks the “share nothing” assumption made by Play: you can’t run your application on several servers, and expect the application to behave consistently. Each application instance will have a different copy of the data.

You can put any object in the cache, as in the following example (in this example from the doc http://www.playframework.org/documentation/1.1.1/controllers#session, you use session.getId() to save messages for each user)

public static void index() {
    List messages = Cache.get(session.getId() + "-messages", List.class);
    if(messages == null) {
        // Cache miss
        messages = Message.findByUser(session.get("user"));
        Cache.set(session.getId() + "-messages", messages, "30mn");
    }
    render(messages);
}

Because it's a cache, and not a session, you have to take into account that the data might no longer be available, and have some mean to retrieve it once again from somehere (the Message model, in this case)

Anyway, if you have enough memory and it involves a short interaction with the user the data should be there, and in case it's not you can redirect the user to the beginning of the wizard (you are talking about some kind of wizard page, right?)

Have in mind that play, with it's stateless share-nothing approach, really have no sessión at all, underneath it just handles it through cookies, that's why it can only accept strings of limited size

Upvotes: 0

niels
niels

Reputation: 7309

You can only store the objects in the Cache. The objects must be serializable for this. In the session you can store a key (which must be a String) to the Cache. Make sure that your code still works if the object was removed from the cache (same as a session-timeout). It's explained in http://www.playframework.org/documentation/1.0.3/cache. Hope that solve your problem.

Upvotes: 6

plunchete
plunchete

Reputation: 486

To store values in the session:

//first get the user's session
//if your class extends play.mvc.Controller you can access directly to the session object
Session session = Scope.Session.current();
//to store values into the session
session.put("name", object);

If you want to invalidate / clear the session object

session.clear()

Upvotes: 5

Related Questions