使用Objectify将实体保存到memcache而不是磁盘 [英] Save an entity to memcache but not the disk with Objectify

查看:108
本文介绍了使用Objectify将实体保存到memcache而不是磁盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体用于游戏完成率的非关键记录,基本上只是简单的计数器。由于这个事实每天会更新数十万次(并且有希望增长),所以它会永久存储在memcache中。



为了把自己从不得不支付数十万次非关键数据的写入费用,我很高兴有风险不得不对每个15分钟左右的磁盘写入持久存储在memcache中。



我看不出有什么明显的方法去做,但我想知道是否有像 ofy()。save()。entity()这样的东西用于写入缓存但不是磁盘?如果没有,有没有其他简单的方法可以将实体写入memcache?



如果我自己需要处理这个问题,有没有办法使用memcache键Objectify用于读取由对象存储的对象,还是我最好只处理所有的缓存??解决方案

单独使用Objectify来做到这一点。但是,您可以非常有效地伪装它。



Objectify在命名空间 ObjectifyFactory.MEMCACHE_NAMESPACE 中缓存实体。所有缓存键都是字符串化的Key对象( Key <?>> .toWebSafeString())。所有缓存值都是gae低级别的api 实体对象。



您可以手动在Entity和Entity之间来回转换POJO与 ofy()。load()。fromEntity(entity) ofy()。save()。toEntity(pojo)



这可能足以实现您想要的效果。请注意,Objectify的内置缓存行为是为了在写入(保存/删除)时清除条目,并且仅在get时填充缓存。还要注意,有一个特殊的标记值 NEGATIVE (文字字符串),它指示实体不存在的缓存事实。


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?

解决方案

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.

这篇关于使用Objectify将实体保存到memcache而不是磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆