Spring Cache

General Purpose is to provide a cache api with differents providers :

  • simple hashmap
  • ehcache
  • oscache

Use Case

Typical use case for this API could be the following : you have an functionnal API which create objects by using a requirement on a DAO plexus components. Before asking the DAO layer (webServices, Jdbc access ...), you want to ask if the data exists in a cache if yes the cached value is return if not the DAO layer is used and the result put in the cache for future use.

Test Case Sample

Look at he test case AbstractTestWineService in the module plexus-cache-test. A service called WineService explains this. The default implementation has two requirements :

  • WineDao (DAO layer)
  • CacheBuilder

CacheBuilder can build a cache for the bean you ask (in the sample it's Wine)

cacheBuilder.getCache( Wine.class )            
This will use the following algorithm to provide the Cache implementation :
  • search the Spring Cache with roleHint Wine.class.getName()
  • if not found : lookup the Spring component Cache with name cache#default
  • if not found : return NoCacheCache implementation in order to prevent NPE