ismorodin / ehcache-spring-annotations

Automatically exported from code.google.com/p/ehcache-spring-annotations
0 stars 0 forks source link

Update cached object in a cache without retrieve the whole list #41

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
This is just what I have been thinking and want to put out for discussion.

Let's assume I am caching a method result and the method gets a list of cars 
from the database...

@Cacheable(cacheName="carsCache")
List<Car> getCars();

I know that when I have a new Car created, I can do TriggerRemove to make sure 
the cache gets refreshed.

@TriggersRemove(cacheName="carsCache", removeAll=true)
Car addCar();

But by doing so, all my cars in that List need to be retrieved from the 
database again to update the cache (unless my understanding is wrong...)

But would it be nice that I can only add the newly generated Car into the List 
instead of do a total refresh? Especially, when I have a 10,000 cars already in 
my list, retrieving all cars from database and initializing each one of them 
will be much more expense...

In terms of the annotation, it could be something like a 
@UpdateCachedObjects({cacheName="carsCache"})
Car addCar();

and it will take the return object of the method, and add it to the cached 
object list.

Still will need a way to make the update work. Maybe,
@UpdateCachedObjects({cacheName="carsCache", objectIdField="id"})
Car updateCar();

Then it will update the car in the list if the id of the car matches. Since the 
car gets updated, the hashcode of Car object won't be the same...

Will this be useful? Up for discussion...

Thanks,

Jiang

Original issue reported on code.google.com by h0c...@gmail.com on 30 Jul 2010 at 4:18

GoogleCodeExporter commented 9 years ago
Agree this is valid request, in fact the current @PartialCacheKey example in 
the wiki alludes to this functionality. By virtue of the updateWather method, 
it certainly fooled me...

The problem is I really need this function.

Original comment by ashleyb....@gmail.com on 20 Aug 2010 at 2:39

GoogleCodeExporter commented 9 years ago
The only reasonable way to do this is via having your application code 
manipulate the Ehcache instance directly. The way the framework is designed 
having a method like:

@Cacheable(cacheName="carsCache")
List<Car> getCars();

Will result in a single cache entry in carsCache where the value stored is 
List<Car>

Having an annotation return all the values in the cache doesn't make sense as 
there would be no point to ever calling the actual method implementation, you'd 
always just list the cache values.

There is nothing stopping an application that uses these annotations from also 
directly accessing the Ehcache targeted by the annotation.

The current combination of @Cacheable and @TriggersRemove allows for an object 
to be cached on access and then removed from the cache based on another method 
call. If you ever want the List of all members of the cache you should inject 
the Ehcache instance into an application bean and access it directly to get all 
the values stored in the cache.

Original comment by eric.dalquist on 20 Aug 2010 at 3:56

GoogleCodeExporter commented 9 years ago
Thanks for the prompt response. I'll go with your suggestion since manipulating 
ehcache directly would be a bit self-defeating.

Original comment by ashleyb....@gmail.com on 20 Aug 2010 at 5:05