Object session in playframework

How can I store an instance object of a foreach user session?

I have a class to model a complex algorithm. This algorithm is intended for phased launch. I need to create objects of this class for each user. Each user should be able to step by step promote their copy.

+6
session playframework
source share
4 answers

You can only store objects in the cache. For this, objects must be serialized. In a session, you can store the key (which should be a String) in Cache. Make sure your code still works if the object has been removed from the cache (same as the session timeout). This is explained at http://www.playframework.org/documentation/1.0.3/cache . Hope to solve your problem.

+6
source share

To save values โ€‹โ€‹in a session:

//first get the user 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() 
+4
source share

from the documentation for the game: 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 document http://www.playframework.org/documentation/1.1.1/controllers#session, you use the .getId () session 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); } 

Since this is a cache, not a session, you should take into account that the data may be inaccessible and have some value to extract from it again (in this case, the message model)

In any case, if you have enough memory, and this is due to a short interaction with the user, the data should be there, and in case you cannot redirect the user to the beginning of the wizard (you are talking about some kind of wizard page, right?)

Keep in mind that playing with it without being tied to shared access doesnโ€™t really have any sessiรณn at all, underneath it just processes it through cookies, so it can only accept lines of a limited size

0
source share

Here you can save the "objects" in the session. Basically, you serialize / deserialize objects in JSON and store them in a cookie.

fooobar.com/questions/463070 / ...

0
source share

All Articles