authnzerver.dictcache module

This contains a simple dict-based in-memory key-value store.

Suitable for light-duty caching purposes.

class authnzerver.dictcache.DictCache(capacity: int = 20000)[source]

Bases: object

add(key, value, ttl=None, extras=None)[source]

Adds a key and sets it to the value.

If the key already exists, does nothing.

If value is None, does not add the key to the cache because this would be pointless.

if extras is provided, it must be a dict with key:val pairs. These will be added to the stored item in the container in a dict key called ‘extras’.

counter_add(key, initial_value, ttl=None)[source]

Adds a new counter key to the cache with the specified initial value.

counter_decrement(key, ttl=None)[source]

Decrements a counter key by 1 every time it’s called.

This will pop the key when its count reaches zero either after the current decrement or if the count has already reached zero before the decrement operation will be performed.

Returns the new count after decrement or None if the key doesn’t exist in the cache.

counter_get(key)[source]

This gets the current count for a counter key.

counter_increment(key, ttl=None)[source]

This increments a counter key by 1 every time it’s called and returns the new count.

If the key doesn’t exist, adds it to the cache with an initial count of 1.

counter_rate(key, period_seconds, return_allinfo=False, absolute_rate=True)[source]

This gets the rate of increment/decrement over period (in seconds) for a counter key that was incremented in the past.

If the counter key does not exist, returns None.

If return_allinfo = True, returns a tuple with the current rate, the current value, the initial value, the current time, and the insertion time. Otherwise, returns only the rate as a float.

If absolute_rate is True, returns the absolute value of the rate.

counter_set(key, value, ttl=None)[source]

Sets the counter key to the specified value.

delete(key)[source]

Deletes the key from the cache.

flush()[source]

This removes all items in the cache.

get(key, time_and_ttl=False, extras=False)[source]

Gets the value of key from the cache.

info()[source]

Returns the capacity of the cache, and number of normal and TTL items.

load(infile, hmac_key=None)[source]

This loads contents of the cache from a pickle file on disk.

If hmac_key is not None, this function will assume it has to load a signed pickle. If hmac_key is None but the saved pickle was signed, loading will throw an exception.

pop(key)[source]

Pops the key from the cache.

save(outfile, protocol=4, hmac_key=None)[source]

This saves the current contents of the cache to disk.

The items stored must be pickleable.

If hmac_key is not None, the pickle will be signed before saving it to disk.

set(key, value, ttl=None, extras=None, add_ifnotexists=True)[source]

This sets the value of key to value and returns the new value.

If the key doesn’t exist and add_ifnotexists is False, returns None. If add_ifnotexists is True, adds the key to the cache and returns the value.

ttl = None implies that the TTL no longer applies, in which it will be removed from the key, meaning the key becomes persistent.

extras is a dict with key:val pairs that will update the existing extras dict for item in the container using the dict.update() method.

size()[source]

Returns the number of items in the cache.

time()[source]

Returns the cache’s current time time counter.

class authnzerver.dictcache.KeyWithTime(keytime, key)

Bases: tuple

key

Alias for field number 1

keytime

Alias for field number 0