可以说我有大约100万用户。我想知道给定用户在哪个位置以及哪些用户在他身边。用户可以随时获得新的成就,如果他能看到他的最新消息,那将是美好的。老实说,我认为这样做的每一种方式在时间和/或记忆上都会非常昂贵。想法?到目前为止,我最接近的想法是命令用户离线并构建百分比桶,但无法向用户显示其确切位置。Django:如何创建排行榜
一些代码,如果这能帮助你的Django人:
class Alias(models.Model) :
awards = models.ManyToManyField('Award', through='Achiever')
@property
def points(self) :
p = cache.get('alias_points_' + str(self.id))
if p is not None : return p
points = 0
for a in self.achiever_set.all() :
points += a.award.points * a.count
cache.set('alias_points_' + str(self.id), points, 60 * 60) # 1 hour
return points
class Award(MyBaseModel):
owner_points = models.IntegerField(help_text="A non-normalized point value. Very subjective but try to be consistent. Should be proporional. 2x points = 2x effort (or skill)")
true_points = models.FloatField(help_text="The true value of this award. Recalculated with a cron job. Based on number of people who won it", editable=False, null=True)
@property
def points(self) :
if self.true_points :
# blend true_points into real points over 30 days
age = datetime.now() - self.created
blend_days = 30
if age > timedelta(days=blend_days) :
age = timedelta(days=blend_days)
num_days = 1.0 * age.days/blend_days
r = self.true_points * num_days + self.owner_points * (1 - num_days)
return int(r * 10)/10.0
else :
return self.owner_points
class Achiever(MyBaseModel):
award = models.ForeignKey(Award)
alias = models.ForeignKey(Alias)
count = models.IntegerField(default=1)
有人请编辑这个更清晰,即时睡觉。 – 2009-09-08 02:10:13
我试图给用户一个目标,向他们展示他们上面的人打败,但不能太远以至于无法访问。 – 2009-09-08 02:12:12
对分布底部的抖动会非常大,即使上升或下降1点也会使您下降或在1M以内获得数千个位置。你应该测量你的分数分布的样子。 – 2009-09-08 12:24:52