Simple Spring Memcached (SSM) annotation @ReadThroughSingleCache
work in following way:
- Intercept method (getComplexObjectFromDB) call and check if value is in cache if yes then return it and do not execute the method.
- Result is not in cache so execute the method (getComplexObjectFromDB).
- Store result in cache.
- Return result.
As you see @ReadThroughSingleCache
will put value to cache when it absent and get it from cache when it present.
Because @ReadThroughSingleCache
stores whole result under single cache key usually it is not used with List @ParameterValueKeyProvider
. Common usage is:
@ReadThroughSingleCache(namespace = "CplxObj", expiration = EXPIRATION_TIME)
public List<User> getUserByNameFromDB(@ParameterValueKeyProvider String name) {
List<User> users = ......; // query DB to find users with given name
return users;
}
So next time when getUserByNameFromDB is called with the same name instead of querying database result is returned from cache.