0
votes

Sometimes (when I perform stress test) the object stored in memcache is corrupted (it lacks a property and when I try to access it gives me "NullPointerException"). Could it be related to wrong synchronization between different memcache writes? In fact in different part of my code I just update the UserAccount stored in Memcache in this way:

cache.put(tempUser.getKey(), tempUser);

.. and I try to retrieve in this way:

if (cache.contains(key)) {
    readObject = (T) cache.get(key);
    return readObject;
}

Related to this retrieving I get NullPointerException, when I try (for exampple) to retrieve:

readObject.getMyProperty()

I tried to understand this feature (https://developers.google.com/appengine/docs/java/memcache/overview#Safely_Handling_Concurrent_Memcache_Updates), but it doens't seem very well documented. I also had a look to these answers on stackoverflow:

.. but it doesn't seem realated to my needs. At the end I just need to be sure a value is well stored in the cache, without the risk of race condition could affect its integrity.

================================================================================ UPDATE: I try to specify better the behaviour: I get "NullPointerException" in this row:

if(readObject!=null && readObject.getMyProperty.equals("test"))

so that it is not (only) totally null, but just a property. It seems really to be corrupted. Apart from that I know I have to improve the retrieve phase!

================================================================================

NEW UPDATE: I found the object in memcache with all the properties equals to null, apart from the key one

2

2 Answers

4
votes

Between the call to cache.contains() and cache.get() your entry may get evicted from memcache. A solution would be to just call cache.get() and check for a null result in which case the entry isn't there anymore. You even save some milliseconds for the unneeded call. Never rely on an unchanged state between calls. Instead memcache gives you helpful atomic compound operations to avoid race condition like increment(), putIfUntouched() and put()+ADD_ONLY_IF_NOT_PRESENT.

1
votes

Memcache uses standard Java serialization to serialize/deserialize objects.

To make this work your classes (and all fields in them) must implement java.io.Serializable.

What type is your MyProperty field?