示例#1
0
    def store(self, key, value, expire=None):
        """Saves a given pair (key,value)
        Args:
            key(str): DB key
            value(str): DB JSON
        Returns:
            void
        Raises:
            RedisDBItemCannotBeSaved: When a item cannot be saved.
        """
        try:
            key = to_unicode(key)
            value = to_unicode(value)
            set_name = self.get_set_name()

            pipe = self.connection.pipeline()

            # Set the value
            if expire is None:
                pipe.set(self.get_namespace_key(key), value)
            else:
                pipe.setex(self.get_namespace_key(key), expire, value)

            if self.data_type == 'set':
                # add the key to the set
                pipe.sadd(set_name, key)
            elif self.data_type == 'zset':
                # add the key to the set
                pipe.zadd(set_name, 1, key)

            # run the commands
            pipe.execute()
        except Exception as err:
            raise RedisDBItemCannotBeSaved(str(err))
示例#2
0
    def store(self, key, value, expire=None):
        """Saves a given pair (key,value)
        Args:
            key(str): DB key
            value(str): DB JSON
        Returns:
            void
        Raises:
            RedisDBItemCannotBeSaved: When a item cannot be saved.
        """
        try:
            key = to_unicode(key)
            value = to_unicode(value)
            set_name = self.get_set_name()

            pipe = self.connection.pipeline()
            
            # Set the value
            if expire is None:
                pipe.set(self.get_namespace_key(key), value)
            else:
                pipe.setex(self.get_namespace_key(key), expire, value)
            
            if self.data_type == 'set':
                # add the key to the set
                pipe.sadd(set_name, key)
            elif self.data_type == 'zset':
                # add the key to the set
                pipe.zadd(set_name, 1, key)
                
            # run the commands
            pipe.execute()
        except Exception as err:
            raise RedisDBItemCannotBeSaved(str(err))
示例#3
0
    def store(self, key, value, expire=None):
        """
        Method stores a value after checking for space constraints and
        freeing up space if required.
        :param key: key by which to reference datum being stored in Redis
        :param value: actual value being stored under this key
        :param expire: time-to-live (ttl) for this datum
        """
        key = to_unicode(key)
        # value = to_unicode(value)
        set_name = self.get_set_name()

        while self.connection.scard(set_name) >= self.limit:
            del_key = self.connection.spop(set_name)
            self.connection.delete(self.make_key(del_key))

        pipe = self.connection.pipeline()
        if expire is None:
            expire = self.expire

        if isinstance(expire, int) and expire <= 0:
            pipe.set(self.make_key(key), value)
        else:
            pipe.setex(self.make_key(key), expire, value)

        pipe.sadd(set_name, key)
        pipe.execute()
示例#4
0
    def set_key_value(self, key, value):
        """Saves a given pair (key,value)
        Args:
            key(str): DB key
            value(str): DB JSON
        Returns:
            void
        Raises:
            RedisDBItemCannotBeSaved: When a item cannot be saved.
        """
        try:
            key = to_unicode(key)
            value = to_unicode(value)

            pipe = self.connection.pipeline()
            pipe.set(self.get_namespace_key(key), value)

            # run the commands
            pipe.execute()
        except Exception as err:
            raise RedisDBItemCannotBeSaved(str(err))
示例#5
0
    def set_key_value(self, key, value):
        """Saves a given pair (key,value)
        Args:
            key(str): DB key
            value(str): DB JSON
        Returns:
            void
        Raises:
            RedisDBItemCannotBeSaved: When a item cannot be saved.
        """
        try:
            key = to_unicode(key)
            value = to_unicode(value)

            pipe = self.connection.pipeline()
            pipe.set(self.get_namespace_key(key), value)

            # run the commands
            pipe.execute()
        except Exception as err:
            raise RedisDBItemCannotBeSaved(str(err))
示例#6
0
    def get(self, key):
        """Returns the NMAP Scan with the given key
        Args:
            key(id): The pulse id
        Returns:
            pulse(dic): A python dic containing the NMAP Scan information
        Raises:
            RedisDBInvalidKey: Wrong formatted key
            RedisDBKeyNotFound: Item not found
        """
        key = to_unicode(key)
        if not key:
            raise RedisDBInvalidKey(key)

        value = self.connection.get(self.get_namespace_key(key))
        if not value:
            raise RedisDBKeyNotFound(key)

        return value
示例#7
0
    def get(self, key):
        """Returns the NMAP Scan with the given key
        Args:
            key(id): The pulse id
        Returns:
            pulse(dic): A python dic containing the NMAP Scan information
        Raises:
            RedisDBInvalidKey: Wrong formatted key
            RedisDBKeyNotFound: Item not found
        """
        key = to_unicode(key)
        if not key:
            raise RedisDBInvalidKey(key)

        value = self.connection.get(self.get_namespace_key(key))
        if not value:
            raise RedisDBKeyNotFound(key)

        return value
示例#8
0
    def delete_key(self, key):
        """Removes the element with the given key
        Args:
            key(id): The pulse id
        Returns:
            void
        Raises:
            RedisDBInvalidKey: Wrong formatted key
            RedisDBKeyNotFound: Item not found
        """
        key = to_unicode(key)
        if not key:
            raise RedisDBInvalidKey(key)

        with self.connection.pipeline() as pipe:
            pipe.delete(self.get_namespace_key(key))
            if self.data_type == 'set':
                #remove the key to the set
                pipe.srem(self.get_set_name(), key)
            elif self.data_type == 'zset':
                #remove the key to the set
                pipe.zrem(self.get_set_name(), key)

            pipe.execute()
示例#9
0
    def delete_key(self, key):
        """Removes the element with the given key
        Args:
            key(id): The pulse id
        Returns:
            void
        Raises:
            RedisDBInvalidKey: Wrong formatted key
            RedisDBKeyNotFound: Item not found
        """
        key = to_unicode(key)
        if not key:
            raise RedisDBInvalidKey(key)

        with self.connection.pipeline() as pipe:
            pipe.delete(self.get_namespace_key(key))
            if self.data_type == 'set':
                #remove the key to the set
                pipe.srem(self.get_set_name(), key)
            elif self.data_type == 'zset':
                #remove the key to the set
                pipe.zrem(self.get_set_name(), key)

            pipe.execute()