def test_create_new_tweet_before_get_cached_tweets(self): tweet1 = self.create_tweet(self.linghu, 'tweet1') RedisClient.clear() conn = RedisClient.get_connection() key = USER_TWEETS_PATTERN.format(user_id=self.linghu.id) self.assertEqual(conn.exists(key), False) tweet2 = self.create_tweet(self.linghu, 'tweet2') self.assertEqual(conn.exists(key), True) tweets = TweetService.get_cached_tweets(self.linghu.id) self.assertEqual([t.id for t in tweets], [tweet2.id, tweet1.id])
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 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 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 push_tweet_to_cache(cls, tweet): # Queryset is lazy loading # 此时并没有进行数据库的访问,只有需要进行iteration的时候才会真正查询数据库 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 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)