spring-mvcmemcachedsimple-spring-memcachedssm

Invalidating namespace using simple spring memcached


Has anyone tried invalidating a memcached namespace based on timestamp? For e.g. I need to have a memcached namespace which is the timestamp of the tomcat server that hosts my API services. I need to annotate my method calls so that the namespace is a variable (rather than a constant) and it holds a timestamp of the server startup.

@ReadThroughAssignCache(namespace = "api_divisions_", assignedKey="allDivisions")
public List<Division> getAllDivisions()
{
    List<Division> all = (List<Division>)getHibernateTemplate().find("from Division");
    return all;
}

Then everytime the server starts up, the namespace on this method needs to be dynamically set to the server startup time.

Update - i know that there is a roundabout way of doing this using javaassist and dynamic annotations, but I wanted a better approach.

-Gotz


Solution

  • The namespace is always a constant, you cannot use variable. As I understand you want to create cache key using server startup time. There are at least two way how it can be done. First: use @ReadThroughSingleCache

    @ReadThroughSingleCache(namespace = "api_divisions_allDivisions")
    public List<Division> getAllDivisions(@ParameterValueKeyProvider long startup)
    {
      List<Division> all = (List<Division>)getHibernateTemplate().find("from Division");
      return all;
    }
    

    Second: use custom CacheKeyBuilder which can add timestamp to each cache key.

    Do you need old values stored before server start? If not then simple solution is to always use the same cache key and clean memcached at server starts up.