def get_following_user_id_set(cls, from_user_id): # cache set in Redis name = FOLLOWINGS_PATTERN.format(user_id=from_user_id) user_id_set = RedisHelper.get_all_members_from_set(name) # cache hit, need to change b'123' -> 123 if user_id_set is not None: user_id_set_new = set([]) for user_id in user_id_set: if isinstance(user_id, bytes): user_id = user_id.decode('utf-8') user_id_set_new.add(int(user_id)) return user_id_set_new # cache miss if not GateKeeper.is_switch_on('switch_friendship_to_hbase'): friendships = Friendship.objects.filter(from_user_id=from_user_id) else: friendships = HBaseFollowing.filter(prefix=(from_user_id, None)) user_id_set = set([ fs.to_user_id for fs in friendships ]) # push in Redis RedisHelper.add_id_to_set(name, user_id_set) return user_id_set
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_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 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')
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 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')
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 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')
def load_tweets_through_cache(cls, user_id): # Django query is lazy loading # it is triggered by iterations inside the load_object() queryset = Tweet.objects.filter( user_id=user_id).order_by('-created_at') name = USER_TWEET_PATTERN.format(user_id=user_id) return RedisHelper.load_objects(name, queryset)
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')
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')
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')
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')
def get_cached_newsfeeds(cls, user_id): key = USER_NEWSFEEDS_PATTERN.format(user_id=user_id) if GateKeeper.is_switch_on('switch_newsfeed_to_hbase'): serializer = HBaseModelSerializer else: serializer = DjangoModelSerializer return RedisHelper.load_objects(key, lazy_load_newsfeeds(user_id), serializer=serializer)
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')
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')
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')
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')
def index(): form = InfoForm() if form.validate_on_submit(): session['nick'] = form.nick.data session['age'] = form.age.data session['level'] = form.level.data session['goal'] = form.goal.data session['country'] = form.country.data session['skype'] = form.skype.data if not "iid" in session.keys(): session['iid'] = CommonHelper.randomString(12) rediis.hset("sessions", session['iid'], str(dict(session))) return redirect(url_for('index')) sessions = RedisHelper.convert_redis_to_json(rediis, "sessions") resp = make_response( render_template("index.html", form=form, sessions=sessions)) resp.set_cookie("key", "value", "key2", "value2") return resp
def push_tweet_to_cache(cls, tweet): queryset = Tweet.objects.filter( user_id=tweet.user_id).order_by('-created_at') key = USER_TWEETS_PATTERN.format(user_id=tweet.user_id) RedisHelper.push_object(key, tweet, queryset)
def get_cached_tweet(cls, user_id): # queryset is lazy loading, here is not get data from db yet queryset = Tweet.objects.filter( user_id=user_id).order_by('-created_at') key = USER_TWEETS_PATTERN.format(user_id=user_id) return RedisHelper.load_objects(key, queryset)
def push_newsfeed_to_cache(cls, newsfeed): queryset = NewsFeed.objects.filter(user_id=newsfeed.user_id).order_by('-created_at') key = USER_NEWSFEEDS_PATTERN.format(user_id=newsfeed.user_id) RedisHelper.push_object(key, newsfeed, queryset)
def get_cached_newsfeeds(cls, user_id): queryset = NewsFeed.objects.filter(user_id=user_id).order_by('-created_at') key = USER_NEWSFEEDS_PATTERN.format(user_id=user_id) return RedisHelper.load_objects(key, queryset)
def get_cached_tweets(cls, user_id): queryset = Tweet.objects.filter( user_id=user_id).order_by('-created_at') key = USER_TWEETS_PATTERN.format(user_id=user_id) return RedisHelper.load_objects(key, queryset)
def get_comments_count(self, obj): # return obj.comments_count return RedisHelper.get_count(obj, 'comments_count')
def get_likes_count(self, obj): # return obj.likes_count return RedisHelper.get_count(obj, 'likes_count')
def get_comments_count(self, obj): # Django定义的反查机制 return RedisHelper.get_count(obj, 'comments_count')