0

I have setup a Memcache using the JCache API to interface with it. The cache is built as follows:

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

TreeMap<String, String> name2Id = Maps.newTreeMap();

...

if (cache == null) {
  @SuppressWarnings("rawtypes")
  Map properties = Collections.emptyMap();
  cache = CacheManager.getInstance().getCacheFactory().createCache(properties);
  CacheManager.getInstance().registerCache(dropDownCache, cache);
  log.fine("Registering cache: " + dropDownCache);
}
cache.put(innerCache, name2Id);

My cache is registered with the name "dropDownCache". The cache contains a single key, "innerCache" with a value containing a map. The map contains a set of key,values that represent the indexed contents for a dropdown menu.

I have a cron job/task that hits the cache every minute to make sure that the cache is not evicted. According to the documentation, I do not believe I should need to do this because I did not specify an eviction time, so keys should never expire. However, it does show me that over a long period of time, the cache is rarely evicted.

The problem is that when myself, or another user, actually visits the page on the webapp with the dropdown menu. When the page comes up, the dropdown menu ends up loading from the datastore because the registered cache is no longer there. The cache ends up missing very often and I do not understand why this is such a stark contrast to what I am seeing with my cron job/task.

Maybe storing a Map as a key is inappropriate? It seems reasonable though.

Any help to figure out why my cache is being evicted would be great help. Or is there a way to figure out exactly why it's being evicted?

Jake88
  • 955
  • 1
  • 19
  • 39
  • paid or free memcache service? – Dan Cornilescu Apr 04 '17 at 23:56
  • Should be paid service, but just in case, would there be a difference if it was free? – Jake88 Apr 05 '17 at 00:01
  • memcache is not meant to be a durable form of storage. See the note in https://cloud.google.com/appengine/docs/standard/java/memcache/#service_levels - there is no guarantee that a cached value persists and even a cronjob does not help. – zapl Apr 05 '17 at 00:07
  • @Jake88 Yes, see https://cloud.google.com/appengine/docs/standard/python/memcache/#service_levels. – Dan Cornilescu Apr 05 '17 at 00:07
  • I understand it's not durable and that it can be evicted, but is it reasonable that it's evicted every ~5s, or less, and has to be rebuilt? – Jake88 Apr 05 '17 at 00:12
  • are you saying that while the cron touching the cache every minute is running you're still seeing the page visit loading it mostly/only from the DB? – Dan Cornilescu Apr 05 '17 at 00:14
  • Yes that is exactly what is happening and I don't know why the cache is behaving so poorly when I visit the real page. – Jake88 Apr 05 '17 at 00:15
  • Our memcached is currently shared (free), so perhaps making it dedicated will improve the performance. I plan on exploring that avenue and updating on the behavior. – Jake88 Apr 05 '17 at 00:32
  • Are you sure the cron and the page really hit the same key? Does the cron find it? Also check the memcache info in the developer console - I see my (free) memcached keys typically persist way more than 1 minute - your cron should give you enough hits to keep the key in cache most of the time. – Dan Cornilescu Apr 05 '17 at 01:37
  • We changed memcache from a free to a paid service and we did notice a significant difference. The cache does not get evicted nearly as often. We are also trialing the use of a String instead of a TreeMap, just in case there some sort of issue there, but it may just be paranoia. In any case, the change to String should be live and we'll be observing the behavior over a period of time. – Jake88 May 03 '17 at 01:00

0 Answers0