Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring @CacheEvict using wildcards

Is there any way of using wildcards in @CacheEvict?

I have an application with multi-tenancy that sometimes needs to evict all the data from the cache of the tenant, but not of all tenants in the system.

Consider the following method:

@Cacheable(value="users", key="T(Security).getTenant() + #user.key")
public List<User> getUsers(User user) {
    ...
}

So, I would like to do something like:

@CacheEvict(value="users", key="T(Security).getTenant() + *")
public void deleteOrganization(Organization organization) {
    ...
}

Is there anyway to do it?

like image 222
Daniel Gomes Avatar asked Jul 19 '13 14:07

Daniel Gomes


1 Answers

Answer is: No.

And it is no easy way to achieve what you want.

  1. Spring Cache annotations must be simple to be easy to implement by cache provider.
  2. Efficient caching must be simple. There is a key and value. If key is found in cache use the value, otherwise compute value and put to cache. Efficient key must have fast and honest equals() and hashcode(). Assume you cached many pairs (key,value) from one tenant. For efficiency different keys should have different hashcode(). And you decide to evict whole tenant. It is no easy to find tenant elements in cache. You have to iterate all cached pairs and discard pairs belonging to the tenant. It is not efficient. It is rather not atomic, so it is complicated and needs some synchronization. Synchronization is not efficient.

Therefore no.

But, if you find a solution tell me, because feature you want is really useful.

like image 134
michaldo Avatar answered Sep 20 '22 18:09

michaldo