将编码键映射到 appengine 中较短的标识符
我想向客户端发送唯一的引用,以便他们的客户端可以引用特定的对象。 appengine 提供的编码密钥有时有 50 个字节长,而我可能只需要两个或三个字节(我可能希望需要四个或五个字节,但这暂时不会!)。
发送较大的密钥实际上非常昂贵,因为我可能一次发送 400 个引用。
所以,我想将这些长键映射到更短的键。一个明显的解决方案是在数据存储中存储映射,但是当我发送 400 个对象时,我会执行 400 个额外查询,对吧?也许我也可以通过在内存缓存中保留映射的副本来减少开支。有更好的办法吗?
我可以从 appengine 创建的未编码密钥中取出数字并使用它吗?我只需要我使用的任何 id 对于每个实体类型都是唯一的,而不是在整个应用程序中。
谢谢,
莱利
I want to send unique references to the client so that they client can refer back to specific objects. The encoded keys appengine provides are sometimes 50 bytes long, and I probably only need two or three bytes (I could hope to need four or five, but that won't be for a while!).
Sending the larger keys is actually prohibitively expensive, since I might be sending 400 references at a time.
So, I want to map these long keys to much shorter keys. An obvious solution is to store a mapping in the datastore, but then when I'm sending 400 objects I'm doing 400 additional queries, right? Maybe I mitigate the expense by keeping copies of the mappings in memcache as well. Is there a better way?
Can I just yank the number out of the unencoded keys that appengine creates and use that? I only need whatever id I use to be unique per entity kind, not across the whole app.
Thanks,
Riley
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
数据存储区密钥包含您不需要的额外信息 - 例如应用程序 ID。所以您绝对不需要发送整个密钥。
如果这些引用是针对数据存储中的特定种类,那么您可以做得更好,只需发送 key_name 或数字 ID(无论您的密钥使用哪个)。如果是后者,那么您可以只用几个字节传输每个密钥(您可以选择可变长度或固定长度整数编码,具体取决于哪种编码对于您的具体情况更紧凑[可能是前者,直到您发送的大多数 ID 都变得相当大])。
当您从用户那里收到这些部分密钥时,应该很容易重建从数据存储中检索实体所需的完整密钥。如果您使用的是 Python 运行时,则可以使用
db .Key.from_path(kind_name, numeric_id_or_key_name)
。像这样的方案应该比尝试使用数据存储/内存缓存来存储自定义映射更简单并且(快得多)。
Datastore keys include extra information you don't need - like the app ID. So you definitely do not need to send the entire keys.
If these references are to a particular Kind in your datastore, then you can do even better and just send the key_name or numeric ID (whichever your keys use). If the latter is the case, then you could transmit each key with just a few bytes (you could opt for either a variable-length or fixed-length integer encoding depending on which would be more compact for your specific case [probably the former until most of the IDs you're sending get quite large]).
When you receive these partial keys back from the user, it should be easy to reconstruct the full key which you need to retrieve the entities from the datastore. If you are using the Python runtime, you could use
db.Key.from_path(kind_name, numeric_id_or_key_name)
.A scheme like this should be both simpler and (a lot) faster than trying to use the datastore/memcache to store a custom mapping.
您不需要自定义映射机制。只需使用实体键名称来存储您的短标识符:
然后您可以在一个查询中获取这些短标识符:
最后,使用
MyKind.get_by_key_name(short_id)
以便从用户发回的标识符中检索实体。You don't need a custom mapping mechanism. Just use entity key names to store your short identifier :
Then you can fetch these short identitiers in one query :
Finally, use
MyKind.get_by_key_name(short_id)
in order to retrieve entities from identifiers sent back by your users.