DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Scaling DevOps With NGINX Caching: Reducing Latency and Backend Load
  • Develop a Reverse Proxy With Caching in Go
  • Scaling in Practice: Caching and Rate-Limiting With Redis and Next.js
  • Enhanced Query Caching Mechanism in Hibernate 6.3.0

Trending

  • Grafana Loki Fundamentals and Architecture
  • Java's Quiet Revolution: Thriving in the Serverless Kubernetes Era
  • How To Develop a Truly Performant Mobile Application in 2025: A Case for Android
  • Fixing Common Oracle Database Problems
  1. DZone
  2. Data Engineering
  3. Data
  4. Cache Scope with EHCache

Cache Scope with EHCache

By 
Alan Cassar user avatar
Alan Cassar
·
Oct. 04, 13 · Interview
Likes (1)
Comment
Save
Tweet
Share
13.9K Views

Join the DZone community and get the full member experience.

Join For Free

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:

<spring:bean id="cacheManager" name="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/>

<spring:bean id="cache" name="cache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
    <spring:property name="cacheManager" ref="cacheManager"/>
    <spring:property name="cacheName" value="dbCache"/>
    <spring:property name="maxElementsInMemory" value="10000"/>
    <spring:property name="eternal" value="false"/>
    <spring:property name="timeToIdle" value="120"/>
    <spring:property name="timeToLive" value="120"/>
    <spring:property name="overflowToDisk" value="true"/>
    <spring:property name="maxElementsOnDisk" value="10000000"/>
    <spring:property name="diskPersistent" value="false"/>
    <spring:property name="diskExpiryThreadIntervalSeconds" value="120"/>
    <spring:property name="memoryStoreEvictionPolicy" value="LRU"/>
</spring:bean>

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:

package com.ricston.cache;

import java.io.Serializable;

import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;

import org.mule.api.store.ObjectStore;
import org.mule.api.store.ObjectStoreException;

public class EhcacheObjectStore<T extends Serializable> implements ObjectStore<T> {

  private Ehcache cache;

	@Override
	public synchronized boolean contains(Serializable key) throws ObjectStoreException {
		return cache.isKeyInCache(key);
	}

	@Override
	public synchronized void store(Serializable key, T value) throws ObjectStoreException {
		Element element = new Element(key, value);
		cache.put(element);
	}

	@SuppressWarnings("unchecked")
	@Override
	public synchronized T retrieve(Serializable key) throws ObjectStoreException {

		Element element = cache.get(key);
		if (element == null)
		{
			return null;
		}
		return (T) element.getValue();
	}

	@Override
	public synchronized T remove(Serializable key) throws ObjectStoreException {
		T value = retrieve(key);
		cache.remove(key);
		return value;
	}

	@Override
	public boolean isPersistent() {
		return false;
	}

	public Ehcache getCache() {
		return cache;
	}

	public void setCache(Ehcache cache) {
		this.cache = cache;
	}

}

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.

<ee:object-store-caching-strategy name="cachingStrategy" doc:name="cachingStrategy">
    <custom-object-store class="com.ricston.cache.EhcacheObjectStore">
        <spring:property name="cache" ref="cache"/>
    </custom-object-store>
</ee:object-store-caching-strategy>

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.




Cache (computing) Ehcache

Published at DZone with permission of Alan Cassar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Scaling DevOps With NGINX Caching: Reducing Latency and Backend Load
  • Develop a Reverse Proxy With Caching in Go
  • Scaling in Practice: Caching and Rate-Limiting With Redis and Next.js
  • Enhanced Query Caching Mechanism in Hibernate 6.3.0

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!