gpt4 book ai didi

python - 从分片计数器更新实体 - Python

转载 作者:太空狗 更新时间:2023-10-30 02:06:16 25 4
gpt4 key购买 nike

在我的 appengine python 应用程序中,我有分片计数器来计算照片的收藏次数和浏览次数。

我有照片和计数器模型。要按受欢迎程度(# of favs)排序照片,我应该将 # of favs 存储在我的照片实体中(Photo.all().order('num_favs'))。由于我一直在跟踪分片计数器中的收藏数量,我想知道用计数器模型中最新的收藏数量更新我的照片实体的最有效方法是什么?

这是我的模型的摘要:

class Photo(db.Model):
photo = db.BlobProperty()
favs = db.IntegerProperty() #num of favs

class Counter(db.Model):
#key_name = key to a model that's to be counted - Photo in this case
name = db.StringProperty() #counted entity key
count = db.IntegerProperty() #count
type = db.StringProperty() #type of model to count, "Photo" in this example

#Very unefficient way of updating my Photo entities with the latest count from Counter:

fav_counters = Counter.all().filter('type =', 'Photo')

for fav in fav_counters:
photo_fav = db.get(fav.name) #get the photo for which the fav count is for
photo_fav.favs = fav.count
photo_fav.put()

如果我能回答以下问题,我将不胜感激:1) 根据计数器对实体进行排序的最有效方式2) 如果我遵循的根据计数器对实体进行排序的逻辑是正确的 - 使用计数器更新照片实体的最有效方法是什么?

谢谢!

最佳答案

如果您需要根据值进行排序或过滤,您可能不应该使用分片计数器,而应该使用其中一种替代方法。简而言之,它们是:

  1. 只需使用常规计数器即可。如果您不希望更新率在较长时间内超过 1-5QPS(短暂的尖峰是可以的),那么这应该可以正常工作。
  2. 当用户收藏或取消收藏照片时,将任务放入任务队列。确保任务队列的执行率有限以限制争用。
  3. 使用一种有损但无争用的计数器模式,例如 this one .

关于python - 从分片计数器更新实体 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4893588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com