سؤال

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:

  1. Intercept method (getComplexObjectFromDB) call and check if value is in cache if yes then return it and do not execute the method.
  2. Result is not in cache so execute the method (getComplexObjectFromDB).
  3. Store result in cache.
  4. 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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top