Difference between singlecache, multicache and assigncache with respect to spring memcached annotations

2.4k views Asked by At

I am trying to understand the Simple Spring Memcached but have stuck with below mentioned.

What is the difference between:

  1. @ReadThroughSingleCache, @ReadThroughMultiCache and @ReadThroughAssignCache
  2. @UpdateSingleCache, @UpdateMultiCache and @UpdateAssignCache
  3. @InvalidateSingleCache, @InvalidateMultiCache and @InvalidateAssignCache

Also how does update work. If I make an update on a namespace with a certain key, does it go and execute all read*cache methods within the same namespace and using the same key. If yes, then would it work for multiple server applications.

For example in a certain scenario a user's points(for something) are cached Application 1

@ReadThroughSingleCache(namespace="userPoints")
public Integer getUserPoints(@ParamterKeyValueProvide String userId){
}

In another scenario from a different application(a background scheduler may be) following method is called: Application 2

UpdateThroughSingleCache(namespace="userPoints")
public void updateUserPoints(@ParameterKeyValueProvider String userId, Integer newPoints)
{
      //make database update
}

My question is that if the namespace("userPoints") had a cache entry for userId("1234") as 50 points initially and the update method is called with ("1234",100) how would the cache know the logic that the entry for "1234" has to updated with 100 points.

Either it should use the return value and update it with the old value(return type needs to be changed then) or if the update was "write through" the read method should be called with the direct logic of db look up and then updating the cache... but how does application 2 notify application 1 of the update.

1

There are 1 answers

0
ragnor On BEST ANSWER

There are 3 groups of caching annotations depend on amount of handled keys:

  • *AssignCache - the cache key is constant and doesn't depends on method's parameters, usually used on method without parameter i.e. getAllUsers()
  • *SingleCache - the cache key is created using annotated method's parameters, result is stored under single key, method's parameter of type List cannot be used to generate cache key
  • *MultiCache - the cache is created using annotated method's parameters, exactly one of the parameter has to be of type List, result also has to be a List, each element from the result List is stored under dedicated cache key

In SSM there is no direct communication between Application 1 and Application 2. To make it work correctly both applications have to use the same Simple Spring Memcached configuration including memcached servers and provider configuration so they will use the same memcached servers. The applications can be deployed on separated servers. When one application put a key to the memcached using ReadThroughSingleCache or UpdateThroughSingleCache the other one can access it while using ReadThroughSingleCache. The signature of update method should be to use method parameter as a new value:

@UpdateThroughSingleCache(namespace="userPoints")
public void updateUserPoints(@ParameterKeyValueProvider String userId, @ParameterDataUpdateContent Integer newPoints)
{
      //make database update
}

If you want to use return value to update annotate the method with @ReturnDataUpdateContent.