Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sharing Ehcache between two webapps

I have 2 webapps, an admin webapp, and the actual application webapp itself. Both share access to a mysql store and Ehcache is used to cache the user details to prevent a user lookup hit to the backend per REST API request.

The problem I have is that the admin app can be used (rarely I will add) to update the users credentials, lets say the password or username, or change some authorisation level etc. Now when this happens, I need the user cache to be invalidated, or cleared, so that the main webapp can then hit the db upon a user lookup to get the new users details into the cache.

This currently happens, but the clearing of the cache is not visible to the main client webapp.

How do I share a ehcache between two webapps (hosted both in jetty or tomcat)?? I am currently using Jetty but plan to switch to Tomcat.

I am using Spring 3, Jersey, Hibernate, MySQL and Google Ehcache annotations.

thanks for any assistance.

like image 681
user983022 Avatar asked Nov 26 '11 00:11

user983022


2 Answers

You have several options here:

  • You can use JGroups to join your applications into one notification group, and then notify about particular cache entry update/invalidation. So upon receiving such notification your main web app will update its local EhCache entry.
  • You can add Terracotta distributed cache as EhCache implementation (it actually does not change your code, since it just plugs behind EhCache API). Then your EhCache becomes distributed, so that changes made in your admin web app are visible to your actual web app. Note: Terracotta requires commercial licence.
  • Instead of using EhCache you can use MemCached that allows
    having one common cache among multiple apps. So changes made in your admin web app are visible to your actual web app.See MemCached
    tutorial
like image 77
user1697575 Avatar answered Oct 22 '22 23:10

user1697575


Ehcache has a REST API that you could deploy as a webapp. That way, both the admin and the application webapp could share a cache.

like image 25
kevinpeterson Avatar answered Oct 22 '22 22:10

kevinpeterson