To add an object to a "CplxObj" namespace I use the following :
@ReadThroughSingleCache(namespace = "CplxObj", expiration = EXPIRATION_TIME)
public List<MyObj> getComplexObjectFromDB(
@ParameterValueKeyProvider List<MyObj> listToAdd) {
return getSA(listToAdd);
}
/**
* simple-spring-memcached framework will add to cache if element does not exist
* @param listToAdd
* @return
*/
private List<MyObj> getSA(List<MyObj> listToAdd) {
System.out.println("Adding to cache");
return listToAdd;
}
But how can I get a List<MyObj> for an associated key (eg a userId)
? , something like
public List<MyObj> getComplexObjectFromDB("userId") {
//logic to get the List associated with the key
}
I don't think I should be creating a new namespace for each id ?
Simple Spring Memcached (SSM) annotation @ReadThroughSingleCache
work in following way:
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.