Package pyxmpp :: Module cache :: Class CacheFetcher
[show private | hide private]
[frames | no frames]

Class CacheFetcher

Known Subclasses:
DiscoCacheFetcherBase

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.


Method Summary
  __init__(self, cache, address, item_freshness_period, item_expiration_period, item_purge_period, object_handler, error_handler, timeout_handler, timeout_period, backup_state)
Initialize an CacheFetcher object.
  error(self, error_data)
Handle a retrieval error and call apriopriate handler.
  fetch(self)
Start the retrieval process.
  got_it(self, value, state)
Handle a successfull retrieval and call apriopriate handler.
  timeout(self)
Handle fetcher timeout and call apriopriate handler.
  _deactivate(self)
Remove the fetcher from cache and mark it not active.
  _deactivated(self)
Mark the fetcher inactive after it is removed from the cache.
bool _try_backup_item(self)
Check if a backup item is available in cache and call the item handler if it is.

Instance Variable Summary
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

__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)

Initialize an CacheFetcher object.
Parameters:
cache - cache object which created this fetcher.
           (type=Cache)
address - requested item address.
           (type=any hashable)
item_freshness_period - freshness period for the requested item.
           (type=timedelta)
item_expiration_period - expiration period for the requested item.
           (type=timedelta)
item_purge_period - purge period for the requested item.
           (type=timedelta)
object_handler - function to be called after the item is fetched.
           (type=callable(address, value, state))
error_handler - function to be called on error.
           (type=callable(address, error_data))
timeout_handler - function to be called on timeout
           (type=callable(address))
timeout_period - timeout interval.
           (type=timedelta)
backup_state - 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.
           (type=bool)

error(self, error_data)

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 - additional information about the error (e.g. StanzaError instance).
           (type=fetcher dependant)

fetch(self)

Start the retrieval process.

This method must be implemented in any fetcher class.

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

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 - fetched object.
           (type=any)
state - initial state of the object.
           (type=str)

timeout(self)

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).

_deactivate(self)

Remove the fetcher from cache and mark it not active.

_deactivated(self)

Mark the fetcher inactive after it is removed from the cache.

_try_backup_item(self)

Check if a backup item is available in cache and call the item handler if it is.
Returns:
True if backup item was found.
           (type=bool)

Instance Variable Details

active

True as long as the fetcher is active and requestor expects one of the handlers to be called.
Type:
bool

address

requested item address.
Type:
any hashable

cache

cache object which created this fetcher.
Type:
Cache

timeout_time

timeout time.
Type:
datetime