阅读背景:

根据朋友列表从GAE中检索高分

来源:互联网 

I have a Facebook App that uses GAE as a backend, and implements a simple scoring system. The real model is a little more cluttered with level data and the possibility of multiple users, but a simplified version is as follows:

我有一个使用GAE作为后端的Facebook应用程序,并实现了一个简单的评分系统。真实的模型更混乱了水平数据和多个用户的可能性,但简化版本如下:

class score(db.Model):
    value = db.IntegerProperty()
    user = db.StringProperty()

I already display the global top ten scores, but I also want the option of displaying the top ten scores of a user's friends. In an SQL environment I would do something like SELECT * FROM scores WHERE user1 IN (...friendslist...) or user2 IN (...friendslist...) ORDER BY value LIMIT 10. In a non-relational database "IN" queries are costly, and in GAE specifically they are limited to blocks of 30 values at a time. Implementing a query this way is not ideal.

我已经显示了全球十大分数,但我也想要显示用户朋友的前十分。在SQL环境中,我会做类似SELECT * FROM得分的事情WHERE user1 IN(... friendslist ...)或user2 IN(... friendslist ...)ORDER BY value LIMIT 10.在非关系数据库中“ IN“查询成本很高,而在GAE中,它们一次限制为30个值的块。以这种方式实现查询并不理想。

My current solution is to store each score with key_name = facebook id (the app only stores their highest score, so there is no problem with duplicate keys), and then use score.get_by_key_name(...friendslist...). The problems with this method are numerous: I have to query for every friend, and this returns an unsorted list of ALL friends' scores that then needs to be parsed with code.

我目前的解决方案是使用key_name = facebook id存储每个分数(应用程序仅存储其最高分,因此重复密钥没有问题),然后使用score.get_by_key_name(... friendslist ...)。这个方法的问题很多:我必须查询每个朋友,然后返回一个未排序的所有朋友的分数列表,然后需要用代码解析。

By memcaching each score entity I lighten the load a little, but memcache won't scale well if the datastore becomes populated with many scores, and this is still an expensive and painful way to do what would be a simple operation in a relational database. Is there a better way to do this in Google App Engine?

通过memcaching每个得分实体我稍微减轻了负担,但是如果数据存储区中填充了许多分数,则memcache将无法很好地扩展,并且这仍然是在关系数据库中执行简单操作的昂贵且痛苦的方式。有没有更好的方法在Google App Engine中执行此操作?

1 个解决方案

#1


0  

Why don't you write the top ten scores to the each user's entity?

为什么不将十大分数写入每个用户的实体?

User 1 has a new score 100! Yeah!

用户1的新分数为100!是啊!

User 1 has a list of 20 friends.

用户1有20个朋友的列表。

Obtain the list of 20 friends and add 100 into a top scores field of each entity(if it's one of the top 20).

获取20个朋友的列表,并将100添加到每个实体的最高分数字段中(如果它是前20名中的一个)。

Downside: Every time a user has a new high score, you will be pulling the entities of all their friends and possibly updating them. WATCH YOUR READ WRITE COSTS Of course, if a user get's several high scores in a row, perhaps the friends values will be memcached.

缺点:每当用户获得新的高分时,您将拉动所有朋友的实体并可能更新它们。看完你的阅读费用当然,如果一个用户连续获得几个高分,那么朋友的值可能会被memcached。

Advantage: You can timestamp each top score entry, track the last time the user played, and HIGHLIGHT new high scores since the user last played! Just a thought...

优势:您可以为每个最高分数条目添加时间戳,跟踪用户上次播放的时间,以及自用户上次播放以来突出显示新的高分!只是一个想法...


分享到: