Chapter 13. Caching data

 

This chapter covers

  • Enabling declarative caching
  • Caching with Ehcache, Redis, and GemFire
  • Annotation-oriented caching

Have you ever had someone ask you a question and then, moments after you reply, ask you the same thing again? Often, I’m asked this type of question by my children:

  • “Can I have some candy?”
  • “What time is it?”
  • “Are we there yet?”
  • “Can I have some candy?”

In many ways, the components of the applications we write are the same way. Stateless components tend to scale better, but they also tend to ask the same question over and over again. Because they’re stateless, they discard any answer they were given once their current task is complete, and they have to ask the question again the next time that same answer is needed.

Sometimes it takes a little while to fetch or calculate the answer to the question being asked. Maybe you must fetch data from the database, invoke a remote service, or perform a complex calculation. That’s time and resources spent arriving at the answer.

If that answer isn’t likely to change frequently (or at all), then it’s wasteful to go through the same channel to fetch it again. Moreover, doing so can and likely will have a negative impact on the performance of your application. Instead of asking the same question over and over, only to arrive at the same answer each time, it makes sense to ask once and remember that answer when it’s needed later.

13.1. Enabling cache support

13.2. Annotating methods for caching

13.3. Declaring caching in XML

13.4. Summary

sitemap