Mule
Mule

Reputation: 878

Play 2.5+ and CacheAPI

I am using Play 2.5.4. I just started using the CacheAPI to store the currently logged in user because it is needed multiple times over a page request. I added this object to cache, via cache.set(). I have this working just fine.

The problem is, if I update this user object to have different permissions or roles from an administrator's account, I need to invalidate that cache object for that user so that user can get the proper permissions or roles.

How can I accomplish updating the cache for a user? How can I do that across multiple web servers?

Thanks for any help!

Upvotes: 1

Views: 663

Answers (1)

Salem
Salem

Reputation: 12986

How can I accomplish updating the cache for a user?

You just add it again. Supposing you are using the user id as the key

cache.set(user.getId(), user);

How can I do that across multiple web servers?

Now this is a completely different question. Play by default uses EhCache as the cache provider, and Play Cache api does not create a distributed cache by default.

I don't have much knowlegde with EhCache, but I think it supports a distributed cache (ie a cache between multiple servers) in a different product, and it also supports replication. So you may want to investigate that first and see if it suit you.

You can also create your implementation of the Cache API, and use EhCache/Terracota/Memcached/HazelCast to provide the setup you need.

Another option is for you to use something like Akka Clustering/PubSub extension and deal with the cache invalidation (ie: invalidate a cache in server 1 --> broadcast message for every server to invalidate that cache also). Unless you are comfortable with Akka and PubSub extension this probably will be difficult to implement.

As a last resort, if you can afford that, you can just set the cache with some short expiration time (ex: 1 minute). This is the easiest way if you can afford that your application sees an outdated object for the expiration time, at most

Upvotes: 1

Related Questions