0

我正在使用 Python 中的 Google App Engine 构建一个网站,但是这个问题应该适用于任何类似的东西,而不仅仅是这个特定的框架。我试图在两种相关数据模型之间做出决定。

它们涉及一个 Location 数据库类,每个 Location 实体都有多个与其挂钩的 Experience 数据库实体。

现在我在自己的内存缓存中有 Locations 类,并且我引用了存储在 expRef IntegerProperty 中的每个体验。然后我执行 Experience.get_by_id(expRef) 以获取所有体验并将它们显示在位置页面上。

class Location(ndb.Model):
    #a bunch of other properties
    expRefs = ndb.IntegerProperty(repeated=True)
class Experience(ndb.Model):
            #a bunch of other properties with no ref to Location

#here is the handler for the Location page
location = individual_location_cache(keyid)
exps= []
            if location.expRefs != []:
                for ref in location.expRefs:
                    records.append(Record.get_by_id(ref))

我想知道这是否是最好的选择,或者最好给每个体验一个对 Location 属性的引用,然后将所有引用存储在 Memcached 中并对 memcached 进行两次调用,一次用于 Location,然后一次用于所有那里的经历。

class Location(ndb.Model):
        #a bunch of other properties but no ref to experiences
class Experience(ndb.Model):
        #a bunch of other properties
        locationRef = ndb.IntegerProperty()

#the Location page handler starts here
location = individual_location_cache(keyid)
exps= exp_location_cache(keyid)

是否有很大的不同或我忘记了任何选择?

4

1 回答 1

0

您存储它的方式看起来不错 - 您想要优化的一件非常明显的事情是批量获取体验:

exps= []
        if location.expRefs != []:
            for future in [Record.get_by_id_async(ref) for ref in location.expRefs]:
                records.append(future.get_result())

这样,NDB 将尝试通过一个查询获取您的所有体验 - 并自动将它们缓存在 memcache 中。

于 2012-10-17T10:23:04.423 回答