I use the EhCache SelfPopulatingCache decorator and have a problem when the cache tries to load a new record, but it does not exist (i.e. it does not exist in the database). That way, the cache will put the null value in the cache to unlock any other key, but then the next thread will make the same database call because it received "zero" from the cache. this means that he thinks the record should be loaded - although in fact it is empty because the data does not exist anywhere. I feel like I'm doing something wrong.
(pseudo code)
Value v = cache.get(key); // multiple threads will block here if (v == null) cache.put(key, getValueFromDB()); // this might put a null value
My current solution is not to put a zero, but to put a placeholder Object and test it.
Value v = cache.get(key); if (v == null) cache.put(key, getValueFromDB()); else if (v == NOENTRYOBJECT) return null; else return v;
Thoughts?
java ehcache
Gandalf
source share