pyowm.caches package

Submodules

pyowm.caches.lrucache module

Module containing LRU cache related class

class pyowm.caches.lrucache.LRUCache(cache_max_size=20, item_lifetime_millis=600000)[source]

Bases: pyowm.abstractions.owmcache.OWMCache

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 Weather 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 Weather 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 None
  • 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.
Parameters:
  • cache_max_size (int) – the maximum size of the cache in terms of cached OWM Weather 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.
Returns:

a new LRUCache instance

clean()[source]

Empties the cache

get(request_url)[source]

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 None otherwise
set(request_url, response_json)[source]

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’

Parameters:
  • 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
size()[source]

Returns the number of elements that are currently stored into the cache

Returns:an int

pyowm.caches.nullcache module

Module containing a null-object cache for OWM Weather API responses

class pyowm.caches.nullcache.NullCache[source]

Bases: pyowm.abstractions.owmcache.OWMCache

A null-object implementation of the OWMCache abstract class

get(request_url)[source]

Always returns None (nothing will ever be cached or looked up!)

Parameters:request_url (str) – the request URL
Returns:None
set(request_url, response_json)[source]

Does nothing.

Parameters:
  • request_url (str) – the request URL
  • response_json (str) – the response JSON

Module contents