user3203425
user3203425

Reputation: 3059

Is it ok to put Entity instances into memcache service?

I'm using the low-level java datastore api for app engine. I would like to store a fetched Entity instance into memcache and am not sure if there are any problems associated with that. Example:

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity entity = datastore.get(...);

MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
syncCache.put("abc", entity);

Now some time later I pull the Entity out of memcache, modify a property, and put it back to the datastore:

entityLaterOn = syncCache.get("abc");
entityLaterOn.setProperty("foo", "bar");
datastore.put(entityLaterOn);

is that legal usage? Is there any state associated with the Entity I originally put into memcache that would cause problems trying the above?

Most of the examples I see for memcache involve turning the Entity instance into an intermediate model class first, instead of just directly caching the Entity instance itself.

Thank you

Upvotes: 2

Views: 187

Answers (1)

Andrei Volgin
Andrei Volgin

Reputation: 41089

Entity implements Serializable, so there should be no problem storing it in Memcache. The only limitation is that the total size of the object should be less than app. 1MB, but it applies to all objects.

Remember, however, that an object can be evicted from Memcache at any time. So you have to be ready for entityLaterOn to be null when you call entityLaterOn = syncCache.get("abc");.

Upvotes: 2

Related Questions