Cache Scope with EHCache

In another blog post we explained how you can use a new feature of Mule 3.3 to cache data in your Mule flows. Here we look at how to configure Mule to use EHCache to handle the caching part, rather than storing the data in the default InMemoryObjectStore.

Let’s get going. First let’s start by saying that there are millions of different ways to do this… We’ve taken the route of configuring everything through Spring. So just because you configured your EHCache differently, does not mean it’s wrong or ours is better. We prefer this way since Mule integrates very nicely with Spring. Now we have settled that, let’s make a list of what we need to do:

  1. Define cache manager
  2. Define cache factory bean
  3. Create a custom object store
  4. Define a Mule caching strategy

The first job is to define a cache manager and cache factory bean. Spring provides two very handy classes for this, specific for EHCache, EhCacheManagerFactoryBean and EhCacheFactoryBean. The cache manager just needs to be defined. However on the cache factory bean, you can configure all EHCache details. Such as; time to live, time to idle, when to overflow on disk, the eviction policy, and much more. For more information, you can check the API here or the EHCache website. Also from the cache factory bean, you need to refer back to the cache manager. An example is shown in the following gist:

Once the cache and the cache manager are configured, we need to define a custom object store that uses EHCache to store and retrieve the data. This is very easy to do, we just need to create a new class that implements the standard Mule’s ObjectStore interface, and use EHCache to do the operations. A working custom EHCache object store is shown in the following gist:

As you can clearly see, this object store encapsulates an Ehcache instance. This should be set before we start using this object store. As you can imagine, we will do this through Spring.

The next step is to configure a caching strategy which uses our brand new EHCache object store.

The caching strategy using our custom object store, and in the object store, we are using Spring to inject the cache defined earlier in this blog post.

The rest in Mule can be exactly the same as in the other blog post we explained before.

So here we have shown you how we can use EHCache as the caching engine for the cache scopes provided by Mule 3.3. A reason why you would do this is that with EHCache, you have a very good and proven caching product with a ton of settings that you can exploit and tune for your application.

As a side note, if you have issues with EHCache classloading in Mule, place the EHCache jars inside $MULE_HOME/lib/user rather than in your application.

Enjoy.

If you enjoyed this post, make sure you subscribe to our RSS feed!
This entry was posted in Mule. Bookmark the permalink.

7 Responses to Cache Scope with EHCache

  1. Vamsi says:

    Hi,
    Could you please share the code in .zip file to play it with arround

  2. Alan Cassar says:

    Hi Vamsi,
    you can copy and paste the code directly from GitHub to your Mule project. Unfortunately we do not provide zip files for each blog post as it is a lot of maintenance to keep up with the different Mule versions.

    Thanks

  3. Hari Pandalai says:

    Allan,
    Very informative article. I have ehcache-2.8.1 jar in my class path but when I deploy the app I am getting an error java.lang.ClassNotFoundException: net.sf.ehcache.CacheManager I am using Mule Studio 3.4.1

    Any help would be greatly appreciated.

    Regards,
    Hari

  4. Alan Cassar says:

    Hi Hari,

    There are known classloading issues with EHCache, and as mentioned in the blog, for Mule standalone, you can do the following:

    “As a side note, if you have issues with EHCache classloading in Mule, place the EHCache jars inside $MULE_HOME/lib/user rather than in your application.”

    For MuleStudio, you can follow this same procedure, but instead of copying the EHCache jars to $MULE_HOME/lib/user, copy them to $MULE_STUDIO_INSTALL_DIR/plugins/org.mule.tooling.server.$version/mule/lib/user

  5. Fixie says:

    If i have more than one cache in one mule app, should i create more than one object-store-caching-strategy too ?

  6. Alan Cassar says:

    Hi Fixie,

    If you need to use different instances of EhCache, I believe that the only way to do it is to use different object-store-caching-strategy.

  7. LJ says:

    Thanks a lot Alan Cassar. This article was very helpful + I faced classloading issue on my Mac machine where in Windows, I didn’t face any issue. I searched a lot for a solution and finally I found it here.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Add video comment