def push_tweet_to_cache(cls, tweet): # queryset is lazy loading 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_tweets(cls, user_id): # Queryset is lazy loading # 此时并没有进行数据库的访问,只有需要进行iteration的时候才会真正查询数据库 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_cached_tweets(cls, user_id): # queryset is lazy loading, it will loaded when it's needed. eg, for loop queryset, list(queryset) 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_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, lazy_load_tweets(user_id))
def push_tweet_to_cache(cls, tweet): key = USER_TWEETS_PATTERN.format(user_id=tweet.user_id) RedisHelper.push_object(key, tweet, lazy_load_tweets(tweet.user_id))
def get_cached_tweets(cls, user_id): key = USER_TWEETS_PATTERN.format(user_id=user_id) return RedisHelper.load_objects(key, lazy_load_tweets(user_id))
def get_cached_tweets(cls, user_id): # Queryset is lazy loading, this statement doesn't incur real database query 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)