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.

43 comments:

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 ?

Thanks
Jay

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

Unknown said...

It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
Android Training in Chennai
Ios Training in Chennai

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

BULK SMS PLANS said...

Very Nice article ! great job. Thanks for sharing.
Promote your business brands with bulk SMS marketing, contact us at +917404900081
Bulk SMS Service provider in UAE
Bulk SMS Service provider in U.S.
Bulk SMS Service provider in U.K.
Bulk sms Service provider in India

Bulk sms service provider in Australia
Bulk sms service provider in Indonesia
Bulk sms service provider in Canada
Bulk sms Service provider in Kuwait

Chris Hemsworth said...

The article is so informative. This is more helpful for our
selenium training in chennai
selenium online courses best selenium online training
selenium testing training
selenium classes
Thanks for sharing.

zuan said...

Thanks for sharing an informative blog keep rocking bring more details.I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I will learn much new stuff right here! Good luck for the next!
Web Designing Training Institute in Chennai | web design training class in chennai | web designing course in chennai with placement
Mobile Application Development Courses in chennai
Data Science Training in Chennai | Data Science courses in Chennai
Professional packers and movers in chennai | PDY Packers | Household Goods Shifting
Web Designing Training Institute in Chennai | Web Designing courses in Chennai
Google ads services | Google Ads Management agency
Web Designing Course in Chennai | Web Designing Training in Chennai

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

datasciencecourse said...

data science course bangalore is the best data science course

priya said...

I have been reading for the past two days about your blogs and topics, still on fetching! Wondering about your words on each line was massively effective.
php online training in chennai
php programming center in chennai
php class in chennnai
php certification course
php developer training institution chennai
php training in chennnai
php mysql course in chennai
php institute in chennnai
php course in chennnai
php training with placement in chennnai
php developer course

priya said...

Nice post. Thanks for sharing! I want people to know just how good this information is in your article. It’s interesting content and Great work.
appium online training
appium training centres in chennai
best appium training institute in chennnai
apppium course
mobile appium in chennnai
mobile training in chennnai
appium training institute in chennnai

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...

Pooja said...

This is a very nice article. Thanks for Sharing Freejobalert information.

priyash said...

wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries.
Data science Interview Questions
Data Science Course

Mithun said...

Thanks for the informative article About Java. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.
Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

merlin said...

The given information very impressed for me really so nice content.
Selenium Training in chennai | Selenium Training in anna nagar | Selenium Training in omr | Selenium Training in porur | Selenium Training in tambaram | Selenium Training in velachery

CloudLearn ERP said...

I have the same thoughts on much of this material. I am glad I'm not the only person who thinks this way. You have really written an excellent quality article here. Thank you very much.
Best Data Science training in Mumbai

Data Science training in Mumbai

datasciencecourse said...

Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!

Correlation vs Covariance

priyash said...

Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Correlation vs Covariance
Simple linear regression

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



Anirban Ghosh said...

Great article like this require readers to think as they read. I took my time when going through the points made in this article. I agree with much this information.
SAP training in Kolkata
SAP training Kolkata
Best SAP training in Kolkata
SAP course in Kolkata
SAP training institute Kolkata

Ziarulunirea.ro said...

Very interesting blog. Many blogs I see these days do not really provide anything that attracts others, but believe me the way you interact is literally awesome.You can also check my articles as well.

Data Science In Banglore With Placements
Data Science Course In Bangalore
Data Science Training In Bangalore
Best Data Science Courses In Bangalore
Data Science Institute In Bangalore

Thank you..

EXCELR said...


Very interesting blog Thank you for sharing such a nice and interesting blog and really very helpful article.
Data Science Course in Hyderabad

priyanka said...

Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Correlation vs Covariance
Simple linear regression
data science interview questions

Anirban Ghosh said...

I am astounded by the fact how you have described the entire scenario of the topic. It contains judiciously sound advices.
SAP training in Kolkata
SAP training Kolkata
Best SAP training in Kolkata
SAP course in Kolkata

CloudLearn ERP said...

With so much overstated negative criticism of the corporate culture in the media, it is indeed bracing to have an upbeat, positive report on the good things that are happening. Wish to read some more from you!
Data Science training in Mumbai
Data Science course in Mumbai
SAP training in Mumbai

priyash said...

Amazing Article ! I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Correlation vs Covariance
Simple Linear Regression
data science interview questions
KNN Algorithm
Logistic Regression explained

priyanka said...

very well explained. I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Logistic Regression explained
Correlation vs Covariance
Simple Linear Regression
data science interview questions
KNN Algorithm
Bag of Words Python

Best Training Institute said...

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


Spring training in bangalore
Spring trainers in bangalore



data scientist said...

Fantastic blog extremely good well enjoyed with the incredible informative content which surely activates the learners to gain the enough knowledge. Which in turn makes the readers to explore themselves and involve deeply in to the subject. Wish you to dispatch the similar content successively in future as well.
Data Science Training in Raipur

Mallela said...

Thanks for posting the best information and the blog is very helpful.data science interview questions and answers

Maneesha said...

This is a great post. I like this topic.This site has lots of advantage.I found many interesting things from this site. It helps me in many ways.Thanks for posting this again.
data scientist course in hyderabad

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

Data Science Course in Varanasi - 360DigiTMG said...

Really wonderful blog completely enjoyed reading and learning to gain the vast knowledge. Eventually, this blog helps in developing certain skills which in turn helpful in implementing those skills. Thanking the blogger for delivering such a beautiful content and keep posting the contents in upcoming days.

data science institute in bangalore

Aishwariya said...

Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday.
I want to encourage that you continue your great posts.AWS certification course in Chennai

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

data scientist course said...

Wonderful blog post. This is absolute magic from you! I have never seen a more wonderful post than this one. You've really made my day today with this. I hope you keep this up!
data scientist training and placement in hyderabad

Mallela said...

Thanks for posting the best information and the blog is very important.digital marketing institute in hyderabad

data science said...

I was just examining through the web looking for certain information and ran over your blog.It shows how well you understand this subject. Bookmarked this page, will return for extra. data science course in vadodara

Mallela said...

Thanks for posting the best information and the blog is very important.artificial intelligence course in hyderabad