Python cache support allows information to be shared across different run times of the same python script or even different python scripts in a programmatic way. It essentially provides a central memory cache and a set of APIs which allows the user store and retrieve strings. For example, a DHCP python-script could store a DHCP option into cache and later a RADIUS python-script could retrieve stored string and add it into access-request.
Each cached entry in the cache is a tuple of (key, val). key is used as entry ID. val is the string to be cached. Both key and val are strings. The max length of the key is 512 bytes. Future more, the combine length of key+val is limited by the configured value of entry-size size command in the python-policy.
The Python cache can be enabled per python-policy. Each python policy has its own cache memory which script in other python-policy cannot access. This also implies that the key of a cached entry in different a python policy could overlap.
The user can also specify the max number cache entry per python policy the command max-entries command. System has a global limit for python cache memory of 256MB.
The cached entries could be made persistent by saving it to CF card. This can be enabled with the persistence command in the Python policy.
The system also supports syncing the python cache across chassis with MCS. This can be configured per python policy with the mcs-peer command in the python policy.
Each cached entry has a remaining lifetime. If it decreases over time, the system removes the cached entry if its remaining lifetime is 0. The remaining lifetime can be changed using a system- provided API. The initial lifetime of a newly created cache entry is 600 seconds.
The following are the python cache APIs in a module alc.cache:
alc.cache.save(val, key)
Saves the val identified by the key into the cache. If there is an existing cache entry with same key, then it is overwritten with the new val. An exception is raised if the save failed (for example, because the maximum number of entries is exceeded).
alc.cache.retrieve(key)
Returns the stored entry’s val identified by the key. A KeyError exception is raised if the specified entry does not exist.
alc.cache.clear(key)
Removes the cached entry identified by the key. Raise KeyError exception if the specified entry does not exist.
cache.get_lifetime(key)
The system returns a integer as seconds of remaining lifetime of the specified entry. It returns none if the specified entry does not exist. An exception is raised for any other error.
cache.set_lifetime(key,new_lifetime)
The new_lifetime value is an integer. The system sets the remaining lifetime of the specified entry to the number of seconds of the new_lifetime. An exception is raised for any error including specified entry does not exist. If the new_lifetime>=max_lifetime(configurable using the max-entry-life command in the python policy), then the system sets the actual lifetime to the max_lifetime.