Cache is a component that transparently stores data so that future requests for that data can be served faster. The data that is stored within a cache might be values that have been computed earlier or duplicates of original values that are stored elsewhere. If requested data is contained in the cache (cache hit), this request can be served by simply reading the cache, which is comparatively faster. Otherwise (cache miss), the data has to be recomputed or fetched from its original storage location, which is comparatively slower. Hence, the greater the number of requests that can be served from the cache, the faster the overall system performance becomes.
“Cache” is a specialized form of computer memory.
A cache stores recently-used information in a place where it can be accessed extremely fast.
To store data locally in order to speed up subsequent retrievals.
One of the new features added in Spring Framework in its version 3.1 is its support for transparently adding caching into an existing Spring application. Similar to transaction management, it allows developers to incorporate consistent use of various caching solution available in market with minimal or no impact on the source code.
In this chapter we will first go through various caching concepts and why it should be used in your application. We will then delve deep into as to how we can do this in your Spring application. In these sections we will cover the various nitty-gritties involved in configuring caching for your application. We will also see Spring’s support for other third party cache libraries available today and then finally wrap the chapter with the concept of distributed caches to some extent.
After reading this chapter you should have a clear idea of the cache abstraction provided by the Spring framework and also have a very clear idea of using caching in your existing and new application build on Spring Framework.
Caching has been one of the methods employed to speed up an application and relieve its workload. Its usefulness is particularly apparent today with the rise of social web applications, visited by thousands of people at the same time. Moreover caching should be something orthogonal, having minimal or nil side-effects to the business logic that a developer writes. There are many cache libraries in Java world, both commercial and open source.
Caching is an essential tool for scaling web applications. It avoid disk access, I/O, CPU power and network traffic – primarily to the backend relational database in modern web applications.
Application without Caching Layer
An application that doesn’t have a caching layer has the following known problems:
- Every call creates new objects in CPU memory and Java heap.
- Duplicate objects in Java heap due to repeated database calls.
- All data comes from database which involves network call and is very expensive in nature.
- Due to network involvement in various steps in the overall execution, response times get affected.
- A single call might in turn call multiple data sources to get all required data, in which case it becomes more expensive and more time consuming.
- Due to high amount of objects in Java heap, it could trigger Java garbage collection which is very expensive.
- There is an additional requirement of CPU in doing the marshalling of data from database to the format required in the application layer.
Figure 10-1 shows a typical application without caching layer. It shows applications interacting with more than one data source to retrieve all the required data in your application. It also shows interaction of application directly with various data sources which are very expensive and also time intensive.
Figure 10-1. Application interactions without caching layer
Page Visitors: 10818