title: JPA background cache refresh
body: <p>We have a high performance Java  (J2SE) middleware app where latency is of utmost importance.  It uses some standing data held in a legacy database, where a legacy application may occaisionally modify the data.  Because of the latency requirement, we are planning to Cache the standing data, utilising JPA with Hibernate and maybe a cache provider such as Ehcache.  </p>
  
  <p>However, when the standing data is updated (by the legacy app) we need to be notified of this as soon as possible.  I was thinking of setting an expiry on the cache, but then cache will not be refreshed until the next request for the data is made by the application - at which point the latency will be effected due to the database re-read.  </p>
  
  <p>Ideally we need the cache to return a stale value, and in the background the cache is updated/refreshed with the latest value from the database at regular intervals.</p>
  
  <p>Is this possible with Ehcache?  I have seen SelfPopulatingCache and CacheLoader, but this seems like I'm doing a lot of work (i'd have to hand write code for each entity).  Also, does anyone have an example of a CacheLoader implementation?  I was hoping for an async refresh option on the Query Cache.</p>
  
  <p>Are there any other technologies out there that could provide a solution?  We aren't bound by JPA provider or cache provider.  </p>
  
  <p>Could Spring @Cacheable provide a solution?  I've seen <a href="http://code.google.com/p/ehcache-spring-annotations/wiki/UsingCacheable" rel="noreferrer">spring ehcache cachable</a> mentions self-populating-cache-scope, but its not clear to me what this means.</p>
  
  <p>Thanks in advance.</p>