Cache
The system provides a Python object that acts as a persistent datastore across multiple Python executions from within the same python-policy. One example use case is the following:
A DHCP script stores an option of a Discovery packet in cache, a RADIUS script retrieves it from the cache, and adds it in the RADIUS Accounting Request packet.
config>python>python-policy>cache needs to be configured before alc.cache can be used. See section Python cache support of Triple Play Service Delivery Architecture Guide for more information.
The alc.cache
context-manager
The alc.cache
object, which is an instance of class alc.Cache
, is a context-manager that provides the actual cache object and a context in which this object can be used.
Note
Only the pre-provided
alc.cache
object should be used in script, the classalc.Cache
should not be used directly.
To interact with the cache, a reference to the cache object needs to be retrieved using the “with” syntax:
>>> with alc.cache as cache:
... # cache can be interacted with using the variable "cache"
Outside the scope created by the with-statement, the cache object does not exist and cannot be used.
The alc.Cache
class
- class alc.Cache
- set(key, value, lifetime=600, /)
Associate the value with the key for a duration of the specified lifetime. If the entry already exists, the value and lifetime are updated.
- Parameters
key (bytes) – key
value (bytes) – value
lifetime (int) – time in seconds during which the entry remains valid in the cache. After this time, the entry is removed from the cache. The value must be in the range of the minimal to maximum value configured in python-policy.
- Raises
ValueError – when the lifetime parameter is negative or zero
- get(key, /)
Retrieves the value previously associated with the specified key. If the entry does not exist or has timed out, None is returned.
- Parameters
key (bytes) – key
- Return type
bytes or None
- get_lifetime(key, /)
Retrieves the lifetime of the entry for the specified key. If the entry does not exist or has timed out, None` is returned.
- Parameters
key (bytes) – key
- Return type
int or None
- set_lifetime(key, lifetime, /)
Updates the lifetime of the entry for the specified key. The value can both be increased or decreased, given it is in the range of the minimal to maximum value configured in python-policy. If the entry has timed out, no update is done.
- Parameters
key (bytes) – key
lifetime (int) – time in seconds during which the entry remains valid in the cache. The value must be in the range of the minimal to maximum value configured in python-policy.
- Raises
ValueError – when the lifetime parameter is negative or zero
- clear(key, /)
Removes the entry for the specified key. If the entry does not exist (anymore), nothing is done.
- Parameters
key (bytes) – key
Breaking changes
Context manager
Warning
The alc.cache object is now a context-manager that returns the actual cache object
Non-existing entries
Warning
The following actions on a non-existing entry don’t raise a KeyError exception:
get() (returns None)
clear()
get_lifetime()
set_lifetime()
Examples
Save
The following example saves a cache entry with as parameters:
key: the User-Name attribute in the RADIUS message
value: please remember this for me
lifetime: 86400 seconds
import alc
# Perform any operations before needing the cache
username = alc.radius.attributes[1]
if username is not None:
# using the "with" syntax, get a reference to the cache
with alc.cache as cache:
cache.set(username, b"please remember this for me", 86400)
# other operations not needing the cache are best done outside of this scope
print("entry saved in cache")
Get
The following example gets a cache entry with as key the User-Name attribute in the RADIUS message:
import alc
# Perform any operations before needing the cache
username = alc.radius.attributes[1]
if username is not None:
# using the "with" syntax, get a reference to the cache and fetch the value
with alc.cache as cache:
value = cache.get(username)
# other operations not needing the cache are best done outside of this scope
if value is None:
print("entry not found in cache")
else:
print("entry found in cache: {}".format(value))