0
votes

I'm storing logged in User object in Play cache (using play.cache.Cache) like this.

User user = userRepo.findUserFromDB(id);
String uuid = java.util.UUID.randomUUID().toString();
Cache.set(uuid, user);
session("uuid", uuid);

I want to use this object in every view I render (to display information about logged in user in the footer template). One way would be to pass this object to views from every controller as an argument, but it's definitely not the way of doing it.

String uuid = session("uuid") 
User user = Cache.get(uuid);
return ok(
        dashboard.render(user, other_params)
    );

What would be the most elegant way of doing it to maximize code re-use?

1

1 Answers

0
votes

In my project I access the cache in the view template by calling

@play.api.cache.Cache.get(key)

But my cache is nothing but a String of JSON data so I'm not sure if it will work for you in your case of objects. Maybe try something like:

@play.api.cache.Cache.get(key).name

where name is the object's attribute and see if that works?

Also remember to @import play.api.Play.current at the top of the template if you're trying to access the cache in the view template.