RedisDatabase

class pyrubrum.RedisDatabase(server: redis.Redis, encoding: Optional[str] = 'utf-8', default_expire: Optional[Union[bool, int, datetime.timedelta]] = 86400)

Implementation of a database using a Redis server.

Parameters
  • server (redis.Redis) – The Redis instance which is being used.

  • encoding (Optional[str]) – The encoding format which shall be used to decode the content that is retrieved from the Redis server. Defaults to utf-8.

  • default_expire (Types.Expire) – The expire which is set by default. If it is False, no expire shall be set. Defaults to 86400 seconds (i.e. a day).

get(key: str)str

Get the value which is associated to a certain key inside the database. If no key is found, NotFoundError is raised.

This method will query the key using redis.Redis.get.

Parameters

key (str) – The key you are retrieving the value of from the Redis database.

Returns

The value which is associated to the key in the database.

Return type

Optional[str]

Raises

NotFoundError – If the provided key is not found.

set(key: str, value: str, expire: Optional[Union[bool, int, datetime.timedelta]] = None)

Assign a value to a certain key inside the database. If no expire is provided, it will automatically provide one from default_expire. If the expire is set to be False, no expire flag will be assigned.

This method will assign the provided value to the key using redis.Redis.set and mark the expire flag with redis.Redis.expire.

Parameters
  • key (str) – The key you are adding or updating the value of.

  • value (str) – The value which is being assigned to the key.

  • expire (Optional[Types.Expire]) – The expire in seconds or as a timedelta object. A key is set not to expire if False is provided for this argument. Defaults to None, which automatically provides a default expire from default_expire.

Raises
  • ExpireError – If an error occured while setting the expire for the key.

  • SetError – If an error occured while inserting the key into the database.

delete(key: str)

Delete a certain key from the database, together with its stored value.

This method will delete the provided key from the database using redis.Redis.delete.

Parameters

key (str) – The key which is being deleted from the database, together with its linked data.

Raises