I'm using Spring cache API and was stuck with a problem: I have a Dao class with CRUD operations and all I want to do is to cache a no-argument method which returns me a Map of objects (Key - id, Value - entity)
class Dao implements IDao<Entity>{
public Map<Integer, Entity> getAllEntities(){ /* retreiving from DB */ }
public Entity getEntityByKey(Object key) { ... }
public void insert(Entity entity){...}
public void update(Entity entity){...}
public void delete(Entity entity){...}
}
Can anybody tell me how exactly (and correctly) can I cache getAllEntities() method to get entities, cache getEntityByKey to get Entity by key and also to be able to update cache when I perform create, update or delete operations ? And is it possible to use cacheable version of the method getAllEntities after update (using operations insert, update, delete) ?
try this
class Dao implements IDao<Entity>{
@Cacheable(value = "entity.all")
public Map<Integer, Entity> getAllEntities(){ /* retreiving from DB */ }
@Cacheable(value = "entity.item", key="#p0")
public Entity getEntityByKey(Object key) { ... }
@CacheEvict(value = {"entity.all", "entity.item"}, allEntries=true)
public void insert(Entity entity){...}
@CacheEvict(value = {"entity.all", "entity.item"}, allEntries=true)
public void update(Entity entity){...}
@CacheEvict(value = {"entity.all", "entity.item"}, allEntries=true)
public void delete(Entity entity){...}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.