def decr_comments_count(sender, instance, **kwargs): from tweets.models import Tweet from django.db.models import F # handle comment deletion Tweet.objects.filter(id=instance.tweet_id) \ .update(comments_count=F('comments_count') - 1) RedisHelper.decr_count(instance.tweet, 'comments_count')
def decr_comments_count(sender, instance, **kwargs): from tweets.models import Tweet from django.db.models import F Tweet.objects.filter(id=instance.tweet_id)\ .update(comments_count=F('comments_count') - 1) invalidate_object_cache(sender=Tweet, instance=instance.tweet) RedisHelper.decr_count(instance.tweet, 'comments_count')
def decr_comment_count(sender, instance, **kwargs): from tweets.models import Tweet from django.db.models import F # 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.decr_count(instance.tweet, 'comment_count')
def decr_likes_count(sender, instance, **kwargs): from tweets.models import Tweet from django.db.models import F 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) tweet = instance.content_object RedisHelper.decr_count(tweet, 'likes_count')
def decr_likes_count(sender, instance, **kwargs): from tweets.models import Tweet from django.db.models import F model_class = instance.content_type.model_class() if model_class != Tweet: # TODO HOMEWORK 给 Comment 使用类似的方法进行 likes_count 的统计 return # handle tweet likes cancel Tweet.objects.filter(id=instance.object_id).update(likes_count=F('likes_count') - 1) RedisHelper.decr_count(instance.content_object, 'likes_count')
def decr_likes_count(sender, instance, **kwargs): from tweets.models import Tweet from django.db.models import F 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 Tweet.objects.filter(id=instance.object_id).update( likes_count=F('likes_count') - 1) tweet = instance.content_object RedisHelper.decr_count(tweet, 'likes_count')
def decr_like_count(sender, instance, **kwargs): from tweets.models import Tweet 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.decr_count(instance.content_object, 'like_count')
def decr_likes_count(sender, instance, **kwargs): from comments.models import Comment from tweets.models import Tweet from django.db.models import F model_class = instance.content_type.model_class() if model_class == Comment: # handle comment likes canceled Comment.objects.filter(id=instance.object_id).update( likes_count=F('likes_count') - 1) comment = instance.content_object RedisHelper.decr_count(comment, 'likes_count') else: # handle tweet likes canceled Tweet.objects.filter(id=instance.object_id).update( likes_count=F('likes_count') - 1) tweet = instance.content_object RedisHelper.decr_count(tweet, 'likes_count')