开发者

List query with, facebook friends in list?

开发者 https://www.devze.com 2023-01-14 11:32 出处:网络
In a python based facebook application on GAE, i want to check which friends of current user have \"marked\" a web page or not.

In a python based facebook application on GAE, i want to check which friends of current user have "marked" a web page or not.

For this i have to run as many DB queries as the number of开发者_Go百科 friends (say 100) I fear this may run into "timeout" because of large no of queries.

Google DOCs suggest that "list" queries run in parallel, will this save time ?? Also list has a limit of 30, so i have to make 2 or 3 queries of list type.

Please suggest a better way if possible, using task ques or something....


You can fetch up to 1000 entities in parallel if you already know their keys or their key names.

There are a few ways to solve your specific problem. Here are is one.

Let's assume that when a user "marks" a web page, you create an entity with a key_name that derives from a user's facebook id and the page key.

class PageMarker(db.Model):
    user = db.ReferenceProperty(AppUser)
    ....
    @classmethod
    def mark_page(cls, user, page_key):
        marker = cls.get_or_insert("%s_%s" % (user.facebook_id, 
                                             page_key, user=user)

This allows you to fetch all the users who marked a page in parallel:

key_names = ["%s_%s" % (friend.facebook_id, page_key) for friend in friends]
markers = db.get(key_names)
# Use get_value_for_datastore to get the entity key without making a trip to the 
# datastore
friends_who_bookmarked_keys = [marker.__class__.user.get_value_for_datastore(marker)\
                          for marker in markers]
friends = db.get(friends_who_bookmarked_keys)


I would suggest the following:

  • Make 'marked' entities child entities of the users who have marked them.
  • Use a key name for the 'marked' entity that is based on the URL of the page marked
  • To find friends who have marked a page, retrieve a list of friends, then generate the list of entity keys from the list of friends (easy, since you know the friend key and the URL), and do a single batch get to retrieve a list of 'mark' entities indicating which friends have marked that page.
0

精彩评论

暂无评论...
验证码 换一张
取 消