servers module

class servers.Server(*arg, **kwargs)

Bases: redis.client.Redis

Subclass of Redis object, aiming to simplify the use of the server to few basic commands.

Parameters
  • args – arguments passed to parent constructor

  • kwargs – keyword arguments passed to parent constructor

Example

>>> s = Server(host='localhost', port=6379)
contains(*keys: str) int

Returns the number of keys that exist.

Parameters

keys (str) – key(s) to be checked

Returns

the number of keys that exist

Return type

int

get_multiple_values(*keys: str, prefix: str = '', **kwargs) Tuple[Dict[str, Any], List[str]]

Returns all the values corresponding the given keys. If key does not match any value, the key is returned explicitly tell that it is missing. An optional prefix can be added to every key.

Parameters
  • keys (str) – the keys

  • prefix (str) – the prefix to be added to each key

Returns

a tuple containing all values found and all keys which did not match

Return type

Tuple[Dict[str, Any], List[str]]

get_multiple_values_expired(*keys: str, prefix: str = '') Tuple[Dict[str, Optional[bool]]]

Returns, for each key, wether a expire notification was issued or not, and None is returned if the case the key does not exist. An optional prefix can be added to every key.

Parameters
  • keys (str) – the keys

  • prefix (str) – the prefix to be added to each key

Returns

a tuple containing all values found and all keys which did not match

Return type

Tuple[Dict[str, Optional[bool]]]

get_value(key: str, hmap: Optional[str] = None) Any

Returns the value with corresponding key stored in the server.

Parameters
  • key (str) – the key

  • hmap (str) – if present, will look for value stored in hash-map with this name

Returns

the object stored in the server, None if not object matching the key

Return type

Any

Example

>>> s.get_value('apple')
{'weight': 400, 'unit': 'g'}
is_running() bool

Checks whether the server is running.

Returns

True if the server is running

Return type

bool

run()

Runs the server. Since redis is an externally run server, it cannot be started from the python code properly.

set_value(key: str, value: Any, expire_in: Optional[Dict[str, int]] = None, notify_expire_in: Optional[Dict[str, int]] = None, hmap: bool = False)

Store a pair key / value in the server, with an optional expiration time.

Parameters
  • key (str) – the key

  • value (Any) – any object that can be dumped (see pickle.dumps)

  • expire_in (Optional[Dict[str, int]]) – dictionary of keyword arguments passed used to create a datetime.timedelta object

  • hmap (bool) – True if the value passed is a hash-map

Example

>>> s.set_value('apple', {'weight': 400, 'unit': 'g'}, expire_in={'hours': 10})
shutdown()

Shuts the server down.

servers.parse_redis_ttl_config(conf: Mapping[str, str]) Dict[str, Dict[str, int]]

Parses a config mapping (from a config file for example) into a usable TTL config. Each key, value pair holds the name of the resource stored in the server and the parameters to setup the default expiry duration.

Parameters

conf (Mapping[str, str]) – the config mapping

Returns

the TTL config mapping

Return type

Dict[str, Dict[str, int]]