Refreshing Caches Automatically??

1,562 views
Skip to first unread message

Faraday

unread,
Mar 11, 2011, 4:42:11 PM3/11/11
to Ehcache Spring Annotations
Hi Eric,

I know that refreshing a cache automatically is not supported (http://
groups.google.com/group/ehcache-spring-annotations/browse_thread/
thread/3205c6b90747c2fe/545c07c4db7a2cee?lnk=raot#545c07c4db7a2cee).

I was wondering what you would have thought the best method of doing
this would be? I am using spring annotations and I want a cache to
automatically refresh when it hits then end of a given period. I'm
sure that there must be a simpler way than using dynamic threading &
timers!

Also, just to chip in on the above thread, I would assume that the
refresh mechanism should have its own parameter. So if I set
refreshtime=600 it will refresh every ten minutes, resetting the TTL.
(This prevents there being no data in the cache). Alternatively just
refresh when it hits the TTL (I prefer the first option).

Thanks
F.

Faraday

unread,
Mar 11, 2011, 4:42:50 PM3/11/11
to Ehcache Spring Annotations
Hi ALL!!!! <-- Sorry :)

Eric Dalquist

unread,
Mar 12, 2011, 8:57:46 AM3/12/11
to ehcache-sprin...@googlegroups.com
We've talked about adding a new @CacheableUpdate annotation. This would allow update methods to be annotated and cache object being updated. As for the automatic refresh, it really is outside of the scope of our current code primarily due to the complexity involved in implementing it correctly. Without much design thought at a minimum you'd need:
- A thread pool to scheduled periodic refreshes from, a simple Timer wouldn't be sufficient beyond one or two objects being refreshed.
- A new cache key generator that holds a reference to the original method arguments, part of the danger here is it would be very easy for a developer to accidentally change something in the arguments by reference and then the refresh breaks, it isn't actually refreshing and existing object but getting a new one.
- Some way to prune the number of elements from the cache. The naive implementation is a bit dangerous as it would try to refresh every object ever added to the cache. How do things actually expire from the cache in that case?

These cache annotations are designed for on-demand caching of method invocations. It really sounds like you want an in-memory mirror of your backend store. To do that correctly this is not the code for you. You need to be writing code that uses a thread pool and interacts with an Ehcache instance directly, or if you truly never want things to be missing don't bother with the extra overhead in a cache, just use a ConcurrentMap.

-Eric

Vijay Sharma

unread,
Mar 12, 2011, 9:06:02 AM3/12/11
to ehcache-sprin...@googlegroups.com, Eric Dalquist
Hi,

It's a fairly complex application and caching seems fit for purpose.  I just don't like the idea of the cache expiring and the "user" having to wait for the data to be refreshed.

This is my understanding:
I can populate a cache with data
So long as the same parameters are made for the request call the cache has not expired then cached data will be returned.
If the data in the cache expires it will not be refreshed until the method is called again.
There is currently no mechanism for either automatically refreshing a cache or linking methods to cache expiry.

Linking methods to cache expiry I think I might be wrong about, if I am could you point me at an example?

Also, feel free to call me an idiot for misunderstanding this stuff, I'm sure I'm making it more complex than it needs to be! :)

Thanks
Reply all
Reply to author
Forward
0 new messages