In one of my previous blogs, I gave an overview of Cache Loader+Writer as well as Cache entry listeners. The difference b/w a Cache Loader and Cache create/update entry listener is obvious and their roles are clearly differentiated
- a loader pulls the value from an external source
- populates the cache and
- the respective listener is then invoked
There is no question/doubt on whether one can be substituted for the other
Cache Writer and the Cache create/update entry listener are similar (at least at first glance) – both are invoked when an entry is created/updated in a specific cache
So the obvious Qs are
- why should you use one over the other?
- can we not use cache entry listener to integrate/sync up an external source when the a cache entry is updated/created ?
The important difference is
If the Cache Writer throws an exception, then the entry does not get stored in the cache (think of this as a rollback of sorts). But, in the case of an entry listener, an exception does not effect the value stored in the cache i.e. the created/updated cache entry remains untouched