Module containing LRU cache related class
This cache is made out of a ‘table’ dict and the ‘usage_recency’ linked list.’table’ maps uses requests’ URLs as keys and stores JSON raw responses as values. ‘usage_recency’ tracks down the “recency” of the OWM web API requests: the more recent a request, the more the element will be far from the “death” point of the recency list. Items in ‘usage_recency’ are the requests’ URLs themselves. The implemented LRU caching mechanism is the following:
- cached elements must expire after a certain time passed into the cache. So when an element is looked up and found in the cache, its insertion timestamp is compared to the current one: if the difference is higher than a prefixed value, then the lookup is considered a MISS: the element is removed either from ‘table’ and from ‘usage_recency’ and must be requested again to the OWM web API. If the time difference is ok, then the lookup is considered a HIT.
- when a GET results in a HIT, promote the element to the front of the recency list updating its cache insertion timestamp and return the data to the cache clients
- when a GET results in a MISS, return
- when a SET is issued, check if the maximum size of the cache has been reached: if so, discard the least recently used item from the recency list and the dict; then add the element to ‘table’ recording its timestamp and finally add it at the front of the recency list.
- cache_max_size (int) – the maximum size of the cache in terms of cached OWM web API responses. A reasonable default value is provided.
- item_lifetime_millis (int) – the maximum lifetime allowed for a cache item in milliseconds. A reasonable default value is provided.
a new LRUCache instance
Empties the cache
In case of a hit, returns the JSON string which represents the OWM web API response to the request being identified by a specific string URL and updates the recency of this request.
Parameters: request_url (str) – an URL that uniquely identifies the request whose response is to be looked up Returns: a JSON str in case of cache hit or
Checks if the maximum size of the cache has been reached and in case discards the least recently used item from ‘usage_recency’ and ‘table’; then adds the response_json to be cached to the ‘table’ dict using as a lookup key the request_url of the request that generated the value; finally adds it at the front of ‘usage_recency’
- request_url (str) – the request URL that uniquely identifies the request whose response is to be cached
- response_json (str) – the response JSON to be cached
Returns the number of elements that are currently stored into the cache
Returns: an int
Module containing a null-object cache for OWM web API responses
A null-object implementation of the OWMCache abstract class
None(nothing will ever be cached or looked up!)
Parameters: request_url (str) – the request URL Returns:
- request_url (str) – the request URL
- response_json (str) – the response JSON