Exemplo n.º 1
0
 def push_objects(cls, key, obj, queryset):
     conn = RedisClient.get_connection()
     if not conn.exists(key):
         cls._load_object_to_cache(key, queryset)
         return
     serialized_data = DjangoModelSerializer.serialize(obj)
     conn.lpush(key, serialized_data)
Exemplo n.º 2
0
 def push_object(cls, key, obj, queryset):
     conn = RedisClient.get_connection()
     if not conn.exists(key):
         cls._load_objects_to_cache(key, queryset)
         return
     serialized_data = DjangoModelSerializer.serialize(obj)
     conn.lpush(key, serialized_data)
     conn.ltrim(key, 0, settings.REDIS_LIST_LENGTH_LIMIT - 1)
Exemplo n.º 3
0
 def push_object(cls, key, obj, queryset):
     conn = RedisClient.get_connection()
     if not conn.exists(key):
         # if key doesn't exist in cache, read from db
         # don't use push single object to cache
         cls._load_objects_to_cache(key, queryset)
         return
     serialized_data = DjangoModelSerializer.serialize(obj)
     conn.lpush(key, serialized_data)
     conn.ltrim(key, 0, settings.REDIS_LIST_LENGTH_LIMIT - 1)
Exemplo n.º 4
0
    def _load_objects_to_cache(cls, key, objects):
        conn = RedisClient.get_connection()
        serialized_list = []

        for obj in objects:
            serialized_data = DjangoModelSerializer.serialize(obj)
            serialized_list.append(serialized_data)
        if serialized_list:
            conn.rpush(key, *serialized_list)
            conn.expire(key, settings.REDIS_KEY_EXPIRE_TIME)
Exemplo n.º 5
0
    def test_cache_tweet_in_redis(self):
        tweet = self.create_tweet(self.linghu)
        conn = RedisClient.get_connection()
        serialized_data = DjangoModelSerializer.serialize(tweet)
        conn.set(f'tweet:{tweet.id}', serialized_data)
        data = conn.get(f'tweet:not_exists')
        self.assertEqual(data, None)

        data = conn.get(f'tweet:{tweet.id}')
        cached_tweet = DjangoModelSerializer.deserialize(data)
        self.assertEqual(tweet, cached_tweet)
Exemplo n.º 6
0
    def test_cache_tweet_in_redis(self):
        tweet = self.tweets[0]
        conn = RedisClient.get_connection()
        serialized_data = DjangoModelSerializer.serialize(tweet)
        conn.set(f'tweet:{tweet.id}', serialized_data)
        data=conn.get('tweet:bogus')
        self.assertEqual(data, None)

        data = conn.get(f'tweet:{tweet.id}')
        cached_tweet = DjangoModelSerializer.deserialize(data)
        self.assertEqual(cached_tweet, tweet)
Exemplo n.º 7
0
 def push_object(cls, key, obj, queryset):
     queryset = queryset[:settings.REDIS_LIST_LENGTH_LIMIT]
     conn = RedisClient.get_connection()
     if not conn.exists(key):
         # 如果key不存在,直接从数据库里面load
         # 就不走单个push的方式加到cache里面了
         cls._load_objects_to_cache(key, queryset)
         return
     serialized_data = DjangoModelSerializer.serialize(obj)
     conn.lpush(key, serialized_data)
     conn.ltrim(key, 0, settings.REDIS_LIST_LENGTH_LIMIT - 1)
Exemplo n.º 8
0
    def _load_objects_to_cache(cls, key, objects):
        conn = RedisClient.get_connection()

        serialized_list = []
        # 最多只 cache REDIS_LIST_LENGTH_LIMIT 那么多个 objects
        # 超过这个限制的 objects,就去数据库里读取。一般这个限制会比较大,比如 1000
        # 因此翻页翻到 1000 的用户访问量会比较少,从数据库读取也不是大问题
        for obj in objects[:settings.REDIS_LIST_LENGTH_LIMIT]:
            serialized_data = DjangoModelSerializer.serialize(obj)
            serialized_list.append(serialized_data)

        if serialized_list:
            conn.rpush(key, *serialized_list)
            conn.expire(key, settings.REDIS_KEY_EXPIRE_TIME)
Exemplo n.º 9
0
    def _load_objects_to_cache(cls, key, objects):
        conn = RedisClient.get_connection()

        serialized_list = []
        # allowing to cache at most REDIS_LIST_LENGTH_LIMIT number of objects
        # when exceeding the limit, fetch data in DB instead
        # since the limit if often large, it's a edge case for user to read that many number of items directly
        for obj in objects[:settings.REDIS_LIST_LENGTH_LIMIT]:
            serialized_data = DjangoModelSerializer.serialize(obj)
            serialized_list.append(serialized_data)

        if serialized_list:
            conn.rpush(key, *serialized_list)
            conn.expire(key, settings.REDIS_KEY_EXPIRE_TIME)
Exemplo n.º 10
0
    def _load_objects_to_cache(cls, key, objects):
        conn = RedisClient.get_connection()

        serialized_list = []
        for obj in objects[:settings.REDIS_LIST_LENGTH_LIMIT]:
            # it can only read REDIS_LIST_LENGTH_LIMIT number of objects
            # if the number of objects is over the limitation, read from db
            # usually, the limitation number is big, such as 1000
            # due to user normally not page down more than 1000 objects,
            # it is rarely need to read data from db
            serialized_data = DjangoModelSerializer.serialize(obj)
            serialized_list.append(serialized_data)

        if serialized_list:
            conn.rpush(key, *serialized_list)
            conn.expire(key, settings.REDIS_KEY_EXPIRE_TIME)