Over a million developers have joined DZone.

Spring : Practical Use Of Spring Method Cache

DZone's Guide to

Spring : Practical Use Of Spring Method Cache

Free Resource
Here is practical use of Spring  method cache.Example is using Ecache with Spring cache annotation
(Please refer to this nice article about how to configure spring method cache - http://www.dzone.com/links/r/how_to_enable_a_method_cache_with_the_springframe.html)

We have a web-app for online air ticket booking  .This App gets  data from integrated partner airlines systems . 
For example each partner airlines exposes REST to know available flights for a date between Source and Destination .And they also expose REST to book a ticket .

Web Application use case is like that -

1.Use comes to our website ,searches flights available for a Date between a Source and Destination
2. then she selects a flight and books ticket .

Step 1 - is tricky as we have to show all flights available for that Date between source and destination and this Data has to come from each partner airlines system.That means if we have 100 airline partners we have to make 100 REST (1 REST call to each airline )calls and have to wait long enough for data
 to come from integrated airlines partner systems .This might take a long response time and user experience wont be nice .

Might be you have already noticed - Data here is Flights between a source and destination for a Date .Each Airline would configure  flights for a Date and would rarely change it .So Step 1 is classic case of method cache.

We can consider these  rules to populate flight data into cache -
1.Run a cron job to populate Flight Data into cache  for routes those have  high number of flights .
2.For routes having few flights we would populates cache when some web user first searches flight for that route .

For step 2 - User selects a Flights so we already know partner airline system's  url and directly make REST call to that system .[This is easy and direct one ]

let see  code now for a method cache -

public interface FlightSearchService {
   //If data is already there in cache it would take it from there otherwise make a REST call to partenr airlines   //systems     and then keeps it in cache for 
   //whole day as crone job would run in midnight

   @Cacheable(cacheName="flightCache", keyGeneratorName="flightSearch")
   public List
    getFlights(Date date,City source,City Destination);


Now lets write a cacheUpDate method for to update Flights Data into flightCache

	public interface ManagerFlightCache {
           //This method should be called by  some kind of crone job -say- it runs in midnight and collects all Flight  //data   for a month from all partner   	      
//		airlines 	  system 

           @TriggersRemove(cacheName="flightCache", removeAll=true)
           //this removeAll=true would first remove all cached data from flightCache
           public void  updateFlightCache(Date fromDate,Date  toDate);

Here is our keyGenerator for flight cache 

  //This KeyGenerator referred as "flightSearch" referred in @Cacheable 
  public class  MethodArgsConcatKeyGenerator implements CacheKeyGenerator
	    public Serializable generateKey(MethodInvocation methodInvocation) {
	        return  concatMethodArguments(MethodInvocation methodInvocation);	    }
        private String concatMethodArguments(MethodInvocation methodInvocation){
             StringBuilder key = new StringBuilder();
             for(Object arg : methodInvocation.getArguments()){
             return key.toString();


With cache being populated every midnight for routes having too many flights Data  -searching flights become real fast .If some one searches for flights beyond one month (cache is populated with Flights for one month) then it would make REST call to partner airlines  systems and then Data would be put in the cache .

During midnight crone job would run (it would call updateFlightCache(date1,date2) to remove data already present in cache ).Method would first invalidate cache (remove flight Data from cache ) and then collect Data for one month(for busy routes only) ,this way it would capture any changes occured in flights (more flights might be added by a airline or removed ). 


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}