示例#1
0
def incr_comments_count(sender, instance, created, **kwargs):
    from tweets.models import Tweet
    from django.db.models import F

    if not created:
        return

    Tweet.objects.filter(id=instance.tweet_id).update(comments_count=F('comments_count') + 1)
    RedisHelper.incr_count(instance.tweet, 'comments_count')
示例#2
0
def incr_comments_count(sender, instance, created, **kwargs):
    from tweets.models import Tweet
    from django.db.models import F

    if not created:
        return

    # handle new comment
    Tweet.objects.filter(id=instance.tweet_id)\
        .update(comments_count=F('comments_count') + 1)
    #invalidate_object_cache(sender=Tweet, instance=instance.tweet)
    RedisHelper.incr_count(instance.tweet, 'comments_count')
示例#3
0
def incr_likes_count(sender, instance, created, **kwargs):
    from tweets.models import Tweet
    from django.db.models import F

    if not created:
        return

    model_class = instance.content_type.model_class()
    if model_class != Tweet:
        return

    Tweet.objects.filter(id=instance.object_id).update(
        likes_count=F('likes_count') + 1)
    RedisHelper.incr_count(instance.content_object, 'likes_count')
示例#4
0
def incr_comment_count(sender, instance, created, **kwargs):
    from tweets.models import Tweet
    from django.db.models import F

    if not created:
        return

    # atomic operations for concurrency safe
    # django F translated SQL ensures concurrency safe in db level
    Tweet.objects.filter(id=instance.tweet_id)\
        .update(comment_count=F('comment_count') + 1)
    # extra counter in cache
    # the cached popular object should not be invalid frequently
    RedisHelper.incr_count(instance.tweet, 'comment_count')
    print("COMMENT INCR + 1")
示例#5
0
def decr_likes_count(sender, instance, **kwargs):
    from tweets.models import Tweet
    from comments.models import Comment
    from django.db.models import F

    model_class = instance.content_type.model_class()
    if model_class == Comment:
        Comment.objects.filter(id=instance.object_id).update(
            likes_count=F('likes_count') + 1)
        comment = instance.content_object
        RedisHelper.incr_count(comment, 'likes_count')
    if model_class == Tweet:
        Tweet.objects.filter(id=instance.object_id).update(
            likes_count=F('likes_count') - 1)
        tweet = instance.content_object
        RedisHelper.decr_count(tweet, 'likes_count')
示例#6
0
def incr_like_count(sender, instance, created, **kwargs):
    from tweets.models import Tweet
    if not created:
         return

    model_class = instance.content_type.model_class()
    if model_class != Tweet:
        return

    # atomic operations for concurrency safe
    # django F translated SQL ensures concurrency safe in db level
    Tweet.objects.filter(id=instance.object_id)\
        .update(like_count=F('like_count') + 1)
    # extra counter in cache
    # the cached popular object should not be invalid frequently
    RedisHelper.incr_count(instance.content_object, 'like_count')
示例#7
0
def incr_likes_count(sender, instance, created, **kwargs):
    from tweets.models import Tweet
    from django.db.models import F

    if not created:
        return

    model_class = instance.content_type.model_class()
    if model_class != Tweet:
        return

    # can't use tweet.likes_count += 1; tweet.save() way.
    # as this is not atom action, must use update db operation.
    Tweet.objects.filter(id=instance.object_id).update(likes_count=F('likes_count') + 1)
    tweet = instance.content_object
    RedisHelper.incr_count(tweet, 'likes_count')
示例#8
0
def incr_likes_count(sender, instance, created, **kwargs):
    from tweets.models import Tweet
    from django.db.models import F

    if not created:
        return

    model_class = instance.content_type.model_class()
    if model_class != Tweet:
        # TODO HOMEWORK 给 Comment 使用类似的方法进行 likes_count 的统计
        return

    # 不可以使用 tweet.likes_count += 1; tweet.save() 的方式
    # 因此这个操作不是原子操作,必须使用 update 语句才是原子操作
    Tweet.objects.filter(id=instance.object_id).update(likes_count=F('likes_count') + 1)
    tweet = instance.content_object
    RedisHelper.incr_count(tweet, 'likes_count')
示例#9
0
def incr_likes_count(sender, instance, created, **kwargs):
    from tweets.models import Tweet
    from django.db.models import F

    if not created:
        return

    model_class = instance.content_type.model_class()
    if model_class != Tweet:
        # TODO: we can later do similar things to calculate number of likes for comments
        return

    # update db first and then call cache update
    Tweet.objects.filter(id=instance.object_id).update(
        likes_count=F('likes_count') + 1)
    tweet = instance.content_object
    RedisHelper.incr_count(tweet, 'likes_count')
示例#10
0
def incr_likes_count(sender, instance, created, **kwargs):
    from tweets.models import Tweet
    from django.db.models import F

    if not created:
        return

    model_class = instance.content_type.model_class()
    if model_class != Tweet:
        return

    # 不可以使用tweet = instance.content_object; tweet.likes_count += 1; tweet.save()的方式
    # 因为这个操作不是原子操作,必须使用update语句才是原子操作
    # SQL query: UPDATE likes_count = likes_count + 1 from tweets_table where id = <instance.object.id>
    tweet = instance.content_object
    Tweet.objects.filter(id=tweet.id).update(likes_count=F('likes_count') + 1)
    RedisHelper.incr_count(tweet, 'likes_count')
示例#11
0
def incr_likes_count(sender, instance, created, **kwargs):
    from tweets.models import Tweet
    from django.db.models import F

    if not created:
        return

    model_class = instance.content_type.model_class()
    if model_class != Tweet:
        return

    # Do not use tweet.likes_count += 1; tweet_save()
    # because this is not a atomic operation, we have to use update
    # SQL Query: UPDATE likes_count = likes_count + 1 FROM tweets_table WHERE id=<instance.object_id>
    # Method 1
    Tweet.objects.filter(id=instance.object_id).update(
        likes_count=F('likes_count') + 1)
    RedisHelper.incr_count(instance.content_object, 'likes_count')
示例#12
0
def incr_likes_count(sender, instance, created, **kwargs):
    from tweets.models import Tweet
    from django.db.models import F

    if not created:
        return

    model_class = instance.content_type.model_class()
    if model_class != Tweet:
        # TODO HOMEWORK 给 Comment 使用类似的方法进行 likes_count 的统计
        return

    # 不可以使用 tweet.likes_count += 1; tweet.save() 的方式
    # tweet = instance.content_object
    # tweet.likes_count += 1
    # tweet.save()   -- this is wrong way!
    # 因这个操作不是原子操作,必须使用 update 语句才是原子操作
    # SQL Query: UPDATE likes_count = likes_count + 1 FROM tweets_table WHERE id=<instance.object_id>

    # Method 1
    Tweet.objects.filter(id=instance.object_id).update(likes_count=F('likes_count') + 1)
    RedisHelper.incr_count(instance.content_object, 'likes_count')
示例#13
0
def incr_likes_count(sender, instance, created, **kwargs):
    from comments.models import Comment
    from tweets.models import Tweet
    from django.db.models import F

    if not created:
        return

    model_class = instance.content_type.model_class()
    if model_class == Comment:
        # handle comment likes created
        Comment.objects.filter(id=instance.object_id).update(
            likes_count=F('likes_count') + 1)
        comment = instance.content_object
        RedisHelper.incr_count(comment, 'likes_count')
    else:
        # handle tweet likes created
        # Don't use tweet.likes_count += 1; tweet.save()
        # not atomic, must use "update"
        Tweet.objects.filter(id=instance.object_id).update(
            likes_count=F('likes_count') + 1)
        tweet = instance.content_object
        RedisHelper.incr_count(tweet, 'likes_count')