Package pyxmpp :: Module cache :: Class CacheFetcher
[hide private]

Class CacheFetcher

source code

Known Subclasses:

Base class for cache object fetchers -- classes responsible for retrieving objects from network.

An instance of a fetcher class is created for each object requested and not found in the cache, then fetch method is called to initialize the asynchronous retrieval process. Fetcher object's got_it method should be called on a successfull retrieval and error otherwise. timeout will be called when the request timeouts.

Instance Methods [hide private]
 
__init__(self, cache, address, item_freshness_period, item_expiration_period, item_purge_period, object_handler, error_handler, timeout_handler, timeout_period, backup_state=None)
Initialize an CacheFetcher object.
source code
 
_deactivate(self)
Remove the fetcher from cache and mark it not active.
source code
 
_deactivated(self)
Mark the fetcher inactive after it is removed from the cache.
source code
bool
_try_backup_item(self)
Check if a backup item is available in cache and call the item handler if it is.
source code
 
error(self, error_data)
Handle a retrieval error and call apriopriate handler.
source code
 
fetch(self)
Start the retrieval process.
source code
 
got_it(self, value, state='new')
Handle a successfull retrieval and call apriopriate handler.
source code
 
timeout(self)
Handle fetcher timeout and call apriopriate handler.
source code
Instance Variables [hide private]
bool active
True as long as the fetcher is active and requestor expects one of the handlers to be called.
any hashable address
requested item address.
Cache cache
cache object which created this fetcher.
datetime timeout_time
timeout time.
Method Details [hide private]

__init__(self, cache, address, item_freshness_period, item_expiration_period, item_purge_period, object_handler, error_handler, timeout_handler, timeout_period, backup_state=None)
(Constructor)

source code 
Initialize an CacheFetcher object.
Parameters:
  • cache (Cache) - cache object which created this fetcher.
  • address (any hashable) - requested item address.
  • item_freshness_period (timedelta) - freshness period for the requested item.
  • item_expiration_period (timedelta) - expiration period for the requested item.
  • item_purge_period (timedelta) - purge period for the requested item.
  • object_handler (callable(address, value, state)) - function to be called after the item is fetched.
  • error_handler (callable(address, error_data)) - function to be called on error.
  • timeout_handler (callable(address)) - function to be called on timeout
  • timeout_period (timedelta) - timeout interval.
  • backup_state (bool) - when not None and the fetch fails than an object from cache of at least this state will be passed to the object_handler. If such object is not available, then error_handler is called.

_try_backup_item(self)

source code 
Check if a backup item is available in cache and call the item handler if it is.
Returns: bool
True if backup item was found.

error(self, error_data)

source code 

Handle a retrieval error and call apriopriate handler.

Should be called when retrieval fails.

Do nothing when the fetcher is not active any more (after one of handlers was already called).

Parameters:
  • error_data (fetcher dependant) - additional information about the error (e.g. StanzaError instance).

fetch(self)

source code 

Start the retrieval process.

This method must be implemented in any fetcher class.

got_it(self, value, state='new')

source code 

Handle a successfull retrieval and call apriopriate handler.

Should be called when retrieval succeeds.

Do nothing when the fetcher is not active any more (after one of handlers was already called).

Parameters:
  • value (any) - fetched object.
  • state (str) - initial state of the object.

timeout(self)

source code 

Handle fetcher timeout and call apriopriate handler.

Is called by the cache object and should _not_ be called by fetcher or application.

Do nothing when the fetcher is not active any more (after one of handlers was already called).