예제 #1
0
    def delete(self):
        """Deletes all the redis keys used by this model"""
        keys = [self._calculate_hash_key()]
        keys.extend([self._calculate_key_for_field(k) for k in self.__string_fields__.keys()])

        conn = configure.get_connection()
        results = conn.delete(*keys)
        return results
예제 #2
0
    def append_to_bytestream(self, field_name, value):
        redis_key = self._calculate_key_for_field(field_name)
        conn = configure.get_connection()
        conn.append(redis_key, value)

        old_value = getattr(self, field_name) or ""
        new_value = bytes(old_value) + bytes(value)
        setattr(self, field_name, new_value)
        return redis_key
예제 #3
0
    def append_to_bytestream(self, field_name, value):
        redis_key = self._calculate_key_for_field(field_name)
        conn = configure.get_connection()
        conn.append(redis_key, value)

        old_value = getattr(self, field_name) or ''
        new_value = bytes(old_value) + bytes(value)
        setattr(self, field_name, new_value)
        return redis_key
예제 #4
0
    def delete(self):
        """Deletes all the redis keys used by this model"""
        keys = [self._calculate_hash_key()]
        keys.extend([
            self._calculate_key_for_field(k)
            for k in self.__string_fields__.keys()
        ])

        conn = configure.get_connection()
        results = conn.delete(*keys)
        return results
예제 #5
0
    def get_item_from_redis_key(self, key, connection=None):
        conn = connection or configure.get_connection()
        raw = self.get_raw_dict_from_redis(key)
        if not raw:
            return

        data = self.deserialize_raw_item(raw)
        instance = self.model(**data)
        for field_name, field in self.model.__string_fields__.items():
            string_key = instance._calculate_key_for_field(field_name)
            value = conn.get(string_key)
            instance.set(field_name, value)

        return instance
예제 #6
0
    def get_item_from_redis_key(self, key, connection=None):
        conn = connection or configure.get_connection()
        raw = self.get_raw_dict_from_redis(key)
        if not raw:
            return

        data = self.deserialize_raw_item(raw)
        instance = self.model(**data)
        for field_name, field in self.model.__string_fields__.items():
            string_key = instance._calculate_key_for_field(field_name)
            value = conn.get(string_key)
            instance.set(field_name, value)

        return instance
예제 #7
0
    def all(self, connection=None):
        """Lists all items in redis, returns instances of the adopted model.
        ::

            class BlogPost(ActiveRecord):
                created_at = attributes.DateTime(auto_now=True)
                title = attributes.Unicode()
                text = attributes.Unicode()

           BlogPost.objects.all()

        """
        conn = connection or configure.get_connection()
        prefix = self.model._static_key_prefix()
        search_pattern = ':'.join([prefix, '*'])

        keys = conn.keys(search_pattern)
        items = [self.get_item_from_redis_key(k) for k in filter(lambda x: ':field:' not in x, keys)]
        return items
예제 #8
0
    def save(self):
        """Persists the model in redis.
        Automatically generates a primary key value if one was not provided
        """
        self._set_primary_key()
        redis_hash_key = self._calculate_hash_key()

        conn = configure.get_connection()
        data = self.to_dict()
        pipeline = conn.pipeline()
        pipeline = pipeline.hmset(redis_hash_key, data["hash"])
        redis_keys = {"hash": redis_hash_key, "strings": {}}
        for name, value in data["strings"].items():
            redis_string_key = self._calculate_key_for_field(name)
            redis_keys["strings"][name] = redis_string_key
            pipeline = pipeline.set(redis_string_key, value)

        pipeline.execute()
        return redis_keys
예제 #9
0
    def save(self):
        """Persists the model in redis.
        Automatically generates a primary key value if one was not provided
        """
        self._set_primary_key()
        redis_hash_key = self._calculate_hash_key()

        conn = configure.get_connection()
        data = self.to_dict()
        pipeline = conn.pipeline()
        pipeline = pipeline.hmset(redis_hash_key, data['hash'])
        redis_keys = {'hash': redis_hash_key, 'strings': {}}
        for name, value in data['strings'].items():
            redis_string_key = self._calculate_key_for_field(name)
            redis_keys['strings'][name] = redis_string_key
            pipeline = pipeline.set(redis_string_key, value)

        pipeline.execute()
        return redis_keys
예제 #10
0
    def all(self, connection=None):
        """Lists all items in redis, returns instances of the adopted model.
        ::

            class BlogPost(ActiveRecord):
                created_at = attributes.DateTime(auto_now=True)
                title = attributes.Unicode()
                text = attributes.Unicode()

           BlogPost.objects.all()

        """
        conn = connection or configure.get_connection()
        prefix = self.model._static_key_prefix()
        search_pattern = ':'.join([prefix, '*'])

        keys = conn.keys(search_pattern)
        items = [
            self.get_item_from_redis_key(k)
            for k in filter(lambda x: ':field:' not in x, keys)
        ]
        return items
예제 #11
0
 def get_raw_dict_from_redis(self, key, connection=None):
     conn = connection or configure.get_connection()
     try:
         return conn.hgetall(key)
     except Exception as e:
         logger.warning('Failed to retrieve key {0}: {1}'.format(key, e))
예제 #12
0
 def get_raw_dict_from_redis(self, key, connection=None):
     conn = connection or configure.get_connection()
     try:
         return conn.hgetall(key)
     except Exception as e:
         logger.warning('Failed to retrieve key {0}: {1}'.format(key, e))