Invalidate entire namespace using Simple Spring Memcached

1k views Asked by At

Has anyone tried invalidating an entire memcached namespace?

For example I have two reads methods having differents keys

@Override
@ReadThroughSingleCache(namespace = UrlClientExclusion.TABLE_NAME, expiration = 24 * HOUR)
public List<UrlClientExclusion> list(@ParameterValueKeyProvider String idClient) {

@Override
@ReadThroughSingleCache(namespace = UrlClientExclusion.TABLE_NAME, expiration = 24 * HOUR)
public UrlClientExclusion find(@ParameterValueKeyProvider int idUrlClientExclusion) {

and I want delete entire namespace UrlClientExclusion.TABLE_NAME on update/delete operation

I can't use a keylist method because there are many instances of the app

@InvalidateMultiCache(namespace = UrlClientExclusion.TABLE_NAME)
public int update(UrlClientExclusion urlClientExclusion, /*@ParameterValueKeyProvider List<Object> keylist*/ /* it is impossibile for me put all keys in this list*/) {

so the solution is to delete entire namespace.

What is the annototation to do this? It is possibible to build custom annotation to delete entire namespace? How?

Many thanks

2

There are 2 answers

2
ragnor On BEST ANSWER

Memcached doesn't support namespaces, SSM provides namespaces as a logic abstraction. It is not possible to flush all keys from given namespaces as memcached doesn't group keys into namespaces. Memcached support only flushing/removing single key or all keys.

You can flush all your data from memcached instance or need to provide exact keys that should be removed.

1
Saša On

I don't know how this can be handled with the simple-spring-memcached lib. But I would suggest you to use Spring's Cache Abstraction instead. In that way you can change a cache storage to one of your preference e.g. ConcurrentHashMap, Ehcache, Redis etc. It would be just a configuration change for your application. For the eviction of the namespace, you could do something like:

@CacheEvict(cacheNames="url_client_exclusion", allEntries=true) 
public int update(...)

Unfortunately there is not an official support for Memcached offered by Pivotal, but if you really need to use Memcached, you could check out Memcached Spring Boot library which is compliant with the Spring Cache Abstraction.

There is a sample Java app where you could see how this lib is used. Over there you could also find an example of @CacheEvict usage (link).