Ejemplo n.º 1
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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
    def load_objects(cls, key, queryset):
        conn = RedisClient.get_connection()

        if conn.exists(key):
            serialized_list = conn.lrange(key, 0, -1)
            objects = []
            for data in serialized_list:
                obj = DjangoModelSerializer.deserialize(data)
                objects.append(obj)
            return objects

        cls._load_object_to_cache(key, queryset)
        return list(queryset)
Ejemplo n.º 4
0
    def load_objects(cls, key, queryset):
        conn = RedisClient.get_connection()

        # 如果在 cache 里存在,则直接拿出来,然后返回
        if conn.exists(key):
            serialized_list = conn.lrange(key, 0, -1)
            objects = []
            for serialized_data in serialized_list:
                deserialized_obj = DjangoModelSerializer.deserialize(serialized_data)
                objects.append(deserialized_obj)
            return objects

        # 转换为 list 的原因是保持返回类型的统一,因为存在 redis 里的数据是 list 的形式
        cls._load_objects_to_cache(key, queryset)
        return list(queryset)
Ejemplo n.º 5
0
    def load_objects(cls, key, queryset):
        conn = RedisClient.get_connection()

        # if data is in cache, get from cache and return it
        if conn.exists(key):
            serialized_list = conn.lrange(key, 0, -1)
            objects = []
            for serialized_data in serialized_list:
                deserialized_obj = DjangoModelSerializer.deserialize(
                    serialized_data)
                objects.append(deserialized_obj)
            return objects

        cls._load_objects_to_cache(key, queryset)

        # the value is the redis is list type. so convert the queryset to list
        return list(queryset)
    def load_objects(cls, key, queryset):
        queryset = queryset[:settings.REDIS_LIST_LENGTH_LIMIT]
        conn = RedisClient.get_connection()

        # if key is in cache, get and return it
        if conn.exists(key):
            serialized_list = conn.lrange(key, 0, -1)
            objects = []
            for serialized_data in serialized_list:
                deserialized_obj = DjangoModelSerializer.deserialize(
                    serialized_data)
                objects.append(deserialized_obj)
            return objects

        cls._load_objects_to_cache(key, queryset)

        # data is stored in list in redis, so keep data type same
        return list(queryset)
Ejemplo n.º 7
0
    def load_objects(cls, key, queryset):
        queryset = queryset[:settings.REDIS_LIST_LENGTH_LIMIT]
        conn = RedisClient.get_connection()

        # 如果在 cache 里面存在, 则直接拿出来, 然后返回
        if conn.exists(key):
            serialized_list = conn.lrange(key, 0, -1)
            objects = []
            for serialized_data in serialized_list:
                deserialized_obj = DjangoModelSerializer.deserialize(
                    serialized_data)
                objects.append(deserialized_obj)
            return objects

        # 不存在 load 进 redis cache
        cls._load_objects_to_cache(key, queryset)

        # 转换list 的原因是保持 返回类型的统一, 因为存在redis 里的数据是list 的形式
        return list(queryset)
Ejemplo n.º 8
0
    def load_objects(cls, key, queryset):
        queryset = queryset[:settings.REDIS_LIST_LENGTH_LIMIT]
        conn = RedisClient.get_connection()
        # if there is a cache
        if conn.exists(key):
            # get all tweets in this key
            serialized_list = conn.lrange(key, 0, -1)
            objects = []
            for serialized_data in serialized_list:
                deserialized_obj = DjangoModelSerializer.deserialize(
                    serialized_data)
                objects.append(deserialized_obj)
            return objects

        # if cache miss
        cls._load_objects_to_cache(key, queryset)

        # 转换为 list 的原因是保持返回类型的统一,因为存在 redis 里的数据是 list 的形式
        return list(queryset)
Ejemplo n.º 9
0
    def load_objects(cls, key, queryset):
        # 最多只 cache REDIS_LIST_LENGTH_LIMIT 个 objects
        # 超过这个限制的 objects 就去数据库里面读取。一般这个限制会比较大,比如200
        # 因此翻页翻到 200 的用户访问量会比较少,从数据库读取也不是大问题
        queryset = queryset[:settings.REDIS_LIST_LENGTH_LIMIT]
        conn = RedisClient.get_connection()

        # 如果在cache里存在,则直接拿出来,然后返回
        if conn.exists(key):
            # cache hit
            serialized_list = conn.lrange(key, 0, -1)
            objects = []
            for serialized_data in serialized_list:
                deserialized_object = DjangoModelSerializer.deserialize(
                    serialized_data)
                objects.append(deserialized_object)
            return objects

        # cache miss
        cls._load_objects_to_cache(key, queryset)

        # 转换为list的原因是保持返回类型的统一,因为存在redis里的数据是list的形式
        return list(queryset)