Sunday, June 29, 2014

Flexibility with Spring's cache abstraction

This blog post tries to demonstrate how easily you can switch the caching provider if you are using the caching abstraction from Spring framework without modifying your business logic. As an example, let's consider an expensive operation, like calling the facebook graph API to get the website of a company. This operation we could speed up with caching. If you would like to jump right ahead to the code have a look at my github profile

With the @Cacheable annotation we demarcate the method which is calling an expensive remote call. On the very first time the method will be executed and the result will be put into the pages cache. Repetitive calls of the method with the same parameter will not execute the method, instead the result will be the cached value.
In this simple example the service is exposed via a Spring MVC controller as seen below, where we also measure how long it takes to call the service method.

To build and run the example, issue the following commands in a terminal:

The last command will start up an embedded tomcat instance using Spring Boot.
Now, in another terminal let's call the service couple of times with the same name.

As you can see above, the lookup took similar amount of time on each client invocation. This is because the caching is not activated, it was just declared. In order to activate caching you need a caching provider. The following code snippet configures EhCache as a caching provider for our facebook lookup service.

In order to enable caching for our facebook lookup service with EhCache as caching provider, we activate the ehcache Spring profile:

And again in another terminal when calling the service couple of times with the same name it is visible that at first time it took more than half a second, however the subsequent calls were near instantaneous.

Later on, we might want to scale out our service by starting more than one tomcat instance. In this case we might want to have a distributed cache, where a result cached on one node will be also available transparently on other nodes. The following code snippet contains a configuration for Hazelcast using as a distributed cache.

Run the following two commands in separate terminals, enabling caching with Hazelcast as a provider by activating the hazelcast profile.

This will start up two tomcat instances one on 8081 the other on 8082 port. And as shown below we have added a distributed cache as a caching provider without changing our business logic.

In the sample project the interested reader could check out a configuration for Redis to be used as caching provider.


Jay said...

Very useful article.

Is it possible to have all the 3 fields (username, password, verification code) in single page ? If all 3 combinations are correct then set the Authorities on SecurityContext else throw exceptions. I am able to implement till user / password combination. But I want to put a small piece of java code for the OTP (2factor authentication). But not sure where to put this piece of Java code. Should I use filters ?


Unknown said...

Informative article indeed,

Spring cache abstraction has enabled to separate application's business logic from caching. Thus enabling programmer to improve applications performance along with the ease of switching caching solution to the optimum one.

However a generic solution for caching abstraction has limited caching features provided by caching vendors.
E.g. TayzGrid, a distributed caching solution by Alachisoft also provides spring caching solution. But its feature set has been limited in spring caching. However along with multiple caching topologies, it provides other caching features for spring through configuration files.

-Sameer Shah

sandeep saxena said...

I got lot of ideas after reading this. Share more as similar to this. Thank you for shared this.
C C++ Training in Chennai
c c++ classes
C C++ Training in OMR
C C++ Training in Adyar
JMeter Training in Chennai
Appium Training in Chennai
javascript training in chennai
core java training in chennai

priya rajesh said...

Great collection and thanks for sharing this info with us. Waiting for more like this.
Data Science Training Institute in Chennai
Data Science Classes in Chennai
Data Science course in Chennai
R Programming Training in Chennai
Data Analytics Training in Chennai
Machine Learning course in Chennai
Machine Learning Training in Velachery
Data Science course in Anna Nagar

Sivanandhana Girish said...

Excellent blog!!!Thanks for sharing. Keep doing more.
Spoken English Classes in Chennai
Best Spoken English Classes in Chennai
IELTS Coaching in Chennai
IELTS Coaching Centre in Chennai
English Speaking Classes in Mumbai
English Speaking Course in Mumbai
IELTS Classes in Mumbai
IELTS Coaching in Mumbai

TNK Design Desk said...

This is an amazing blog, thank you so much for sharing such valuable information with us.
Visit for best website design and SEO services at- Website Development Company in India
best website design services in gurgaon
best web design company in gurgaon
best website design in gurgaon
website design services in gurgaon
website design service in gurgaon
best website designing company in gurgaon
website designing services in gurgaon
web design company in gurgaon
best website designing company in india
top website designing company in india
best web design company in gurgaon
best web designing services in gurgaon
best web design services in gurgaon
website designing in gurgaon
website designing company in gurgaon
website design in gurgaon
graphic designing company in gurgaon
website company in gurgaon
website design company in gurgaon
web design services in gurgaon
best website design company in gurgaon
website company in gurgaon
Website design Company in gurgaon
best website designing services in gurgaon
best web design in gurgaon
website designing company in gurgaon
website development company in gurgaon
web development company in gurgaon
website design company
website designing services

Anurag Srivastava said...

Thanks for sharing an informative blog keep rocking bring more details....each type of qualification, type of Sarkari Job, such as central govt jobs, state govt jobs, district-level sarkari jobs and even contract govt jobs are available, for which aspirants can stay informed...

subha said...

this is really nice to read..informative post is very good to read..thanks a lot good luck guys.
Ai & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai

latchu kannan said...

I really enjoyed a lot by reading your post thank you so much for providing this information.

AngularJS training in chennai | AngularJS training in anna nagar | AngularJS training in omr | AngularJS training in porur | AngularJS training in tambaram | AngularJS training in velachery

Best Training Institute said...

It's really good. Thank you for your information. Keep Blogging!.

Spring training in bangalore
Spring trainers in bangalore

Ramesh Sampangi said...

Such a awesome article, I have never seen till now. Thanks for sharing this article. I wish you post some more articles quickly. Thanks alot!

AI Patasala-Artificial Intelligence Course in Hyderabad

AI Patasala-Best Data Science course in Hyderabad

AI Patasala-Best Machine Learning course in Hyderabad

Maneesha said...

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post.
data scientist training and placement

360DigiTMG said...

This is a smart blog. I mean it. You have so much knowledge about this issue, and so much passion. You also know how to make people rally behind it, obviously from the responses.
data analytics courses in hyderabad with placements

Data Science said...

Amazingly by and large very interesting post. I was looking for such an information and thoroughly enjoyed examining this one. Keep posting. An obligation of appreciation is all together for sharing.
data science course in gwalior

Ramesh Sampangi said...

Really nice information and informative content. I bookmarked your site for further blogs.
Online Data Science Course in Hyderabad

Amit Kumar TRT said...

Therefore dissertation web-sites as a result of online to set-up safe and sound ostensibly taped in the website.

Engineering Jobs | bcom 1st year result 2021 name wise

Links For You said...

Very secure message. I basically staggered concerning your blog and wanted to broadcast that i've in all actuality cherished concentrating on your blog entries. Any gaudiness i'll buy in on your feed and I slant you plug again rapidly. tremendous gratitude for the valuable information. Spyhunter 5 Crack

Syedhaseebulhassan said...

Love failure quotes are for the one who failed in love. Love failure makes you realize the best moments you have lived or are living with Love Failure Quotes

Laila Thakur said...

Excellent Article!!! I like the helpful information you provide in your article.davv ba 3rd year result name wise

Anonymous said...

It had me eager to get into the text.
Edraw Max Patch