ehcache / ehcache3

Ehcache 3.x line

Home Page:http://www.ehcache.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cache Invalidation Performance

mmonetha-sobis opened this issue · comments

Hi, at my company, we've encountered some issues connected to our cache invalidation logic.

For each cache entry that should be invalidated

  • we iterate over the cache entry set, which leads to the value to be deserialized as well
  • remove every cache entry, whose key matches the key that should be invalidated

We refactor our implementation that

  • the keys that should be invalidated are gathered beforehand
  • get invalidated using removeAll instead of remove

Unfortunately the performance appears to be unchanged.

We then started checking the ehcache code base and realized that both operations (remove and removeAll) also deserialize the cache entry's value everytime either operation is called.

  1. Are our observations correct? Doesn't it matter - performance wise, wether one iterates over the cache entries himself and removes them via cache.remove() vs gathering all keys beforehand and removing them via cache.removeAll()
  2. Is there a way to invalidate cache entries without deserializing the cache entry's value?

Currently it will be more efficient to do this via external iteration and calls to remove(K) as this will avoid unnecessary deserializations. I just created a draft PR that makes this true for removeAll(...) as well (#3138) but it needs some close review/checking before it can be merged.

Having an iteration like

for (Cache.Entry<ICustomCacheKey, Serializable> entry : cache) {
				if (entry != null) {
					ICustomCacheKey cachedKey = entry.getKey();
					LOGGER.trace("Iterating over {} on {}", cachedKey, cacheName);
					if (cachedKey.matches(givenKey)) {
						cache.remove(cachedKey);
						LOGGER.debug("'{}' was removed from cache '{}'.", givenKey, cacheName);
					}
				}

			}

also deserializes values. Can you provide more details about the external iteration you had in mind?

Hi Chris, is there some update on this topic?

Thanks in advance!

Hi Chris,
we are again stumbling across this issue, are there any news regarding our original question?