Caching Solutions in Java
Caching Solutions in Java
By Aleksey Shevchenko
Go to page: 1 2 3 4 5 Next
Introduction
Data caching is a very important consideration for J2EE applications. Data caching
limits the number of remote invocations in distributed applications and improves
performance of web applications by reducing the number of calls to the persistent
data stores. Even though caching improves performance and makes your
architecture work, it can, in fact, complicate design and introduce such complexities
as concurrent code and cluster-wide synchronization.
Once it has been decided that data caching is an integral part of the architecture,
choosing the right caching solution can prove to be difficult. There is always an
option to implement a caching solution from scratch. This approach can have its
advantages, but will inevitably affect the project's cost and timeline. Another solution
is to choose one of the open-source caching products. When choosing a caching
solution, the following questions should be considered:
1. OSCache
This feature is useful when dealing with static HTML pages. The Page response can
be cached indefinitely in memory thus avoiding reprocessing of the page. OSCache
uses the URI and query parameters to form a unique key. This key is used to store
page content. HttpResponse caching is implemented as a ServletFilter. Thus, the
cache filter abstracts the API usage from the client. The configuration of the cache
filter is done in web.xml. By default, the Cache Filter holds the page response in
'Application' scope and refreshes the cache every one hour. These default values can
be changed.
In case of dynamic pages (JSPs), OSCache provides tags that surround the static
part in the page. Thus, only the static part of the page is cached.
All ORM tools map RDBMS entities to domain objects. OSCache can be used to cache
the domain objects returned by the ORM tool. This drastically reduces the number of
network trips to the DBMS server and expense associated with object creation. Most
ORM tools have a pluggable architecture for caching; in other words, OSCache can
be plugged into any ORM tool. The ORM tool manages the caching of domain objects
for the client.
OSCache can be configured for persistence cache. When the memory capacity is
reached, objects are evicted from the memory and stored on a hard disk. Objects are
evicted from memory based on the configured cache algorithm. However, caution
should be exercised when dealing with the hard disk cache.
Out-of-the box OSCache comes with LRU (Least recently used) and FIFO (First In
First Out) algorithms. Any of the two algorithms can be configured with OSCache.
However, any third-party algorithm can be configured with OSCache.
The cache API is relatively easy to use. An instance of 'GeneralCacheAdministrator' is
created and the cache administrator is used to add, update, and flush entries in the
cache.
2. EHCache
EHCache provides a feature to cache domain objects that map to database entities.
In fact, EHCache is the default cache for Hibernate. EHCache provides support for
memory and disk stores. EHCache provides LRU (Least Recently Used), LFU (Least
Frequently Used), FIFO (First In First Out) algorithms out-of-the box algorithms for
object eviction from memory. EHCache offers support for distributed caching. The
default implementation supports cache discovery via multicast or manual
configuration. Updates are delivered either asynchronously or synchronously via
custom RMI connections. Additional discovery or delivery schemes can be plugged in
by third parties.
• CacheManager
• Cache
• CacheConfiguration
• CacheStatistics