问题
I've got an entity that I use for non-critical logging of game completion rates, basically just simple counters. Due to the fact this will be updated hundreds of thousands of times a day (and hopefully growing) it'll live (almost) permanently in memcache.
To save myself from having to pay for those hundreds of thousands of writes of non-critical data I'm happy to risk having to object persisted in memcache with a disk write only every 15 minutes or so.
I can't see any obvious way to do it, but I was wondering if there was something like ofy().save().entity()
that could be used to write to the cache but not the disk? If not, is there any other easy way to write an entity to memcache?
If I need to handle this myself, is there a way I can use the memcache keys that Objectify uses to read the object stored by objectify, or am I better off just handling all the caching myself?
回答1:
There is no way to do this with Objectify alone. However, you can fake it pretty effectively.
Objectify caches entities in the namespace ObjectifyFactory.MEMCACHE_NAMESPACE
. All cache keys are the stringified Key object (Key<?>.toWebSafeString()
). All cache values are gae low-level api Entity
objects.
You can manually convert back and forth between Entity and POJO with ofy().load().fromEntity(entity)
and ofy().save().toEntity(pojo)
.
That's probably enough to accomplish what you want. Note that Objectify's builtin cache behavior is to clear the entry on write (save/delete) and only to fill the cache on get. Also note that there is a special sentinel value NEGATIVE
(the literal string) which indicates the cached fact that the entity does not exist.
来源:https://stackoverflow.com/questions/32110149/save-an-entity-to-memcache-but-not-the-disk-with-objectify