Related Documentation : Integrations : Using Ehcache with JDBC : Adding JDBC caching to DAO/DAL
Adding JDBC caching to DAO/DAL
If your application already has a Data Access Object/Data Access Layer (DAO/DAL), this is a natural place to add caching. To add caching, follow these steps:
*Identify methods which can be cached.
*Instantiate a cache and add a member variable to your DAO to hold a reference to it.
*Put and get values from the cache.
Identifying methods which can be cached
Normally, you will want to cache the following kinds of method calls:
*Any method which retrieves entities by an Id.
*Any queries which can be tolerate some inconsistent or out of date data.
Example methods that are commonly cacheable:
public V getById(final K id);
public Collection findXXX(...);
Instantiate a cache and add a member variable
Your DAO is probably already being managed by Spring or Guice, so simply add a setter method to your DAO such as setCache(Cache cache). Configure the cache as a bean in your Spring or Guice config, and then use the frameworks injection methodology to inject an instance of the cache.
If you are not using a DI framework such as Spring or Guice, then you will need to instantiate the cache during the bootstrap of your application. As your DAO layer is being instantiated, pass the cache instance to it.
Put and get values from the cache
Now that your DAO/DAL has a cache reference, you can start to use it. You will want to consider using the cache using one of two standard cache access patterns:
*cache-aside
*cache-as-sor
You can read more about these in “Cache Usage Patterns” in the Ehcache Developer Guide.
Copyright © 2010-2015 Software AG, Darmstadt, Germany.

Product Logo |   Feedback