Package entropy :: Module cache :: Class EntropyCacher

Class EntropyCacher

source code


Nested Classes
  SemaphoreTimeScheduled
Instance Methods
 
init_singleton(self)
Singleton overloaded method.
source code
 
__enter__(self)
When used with the with statement, pause cacher on-disk writes.
source code
 
__exit__(self, exc_type, exc_value, traceback)
When used with the with statement, pause cacher on-disk writes.
source code
 
start(self)
This is the method used to start the asynchronous cache writer but also the whole cacher.
source code
 
is_started(self)
Return whether start is called or not.
source code
 
stop(self)
This method stops the execution of the cacher, which won't accept cache writes anymore.
source code
 
sync(self)
This method can be called anytime and forces the instance to flush all the cache writes queued to disk.
source code
 
discard(self)
This method makes buffered cache to be discarded synchronously.
source code
 
save(self, key, data, cache_dir=None)
Save data object to cache asynchronously and in any case.
source code
 
push(self, key, data, async=True, cache_dir=None)
This is the place where data is either added to the write queue or written to disk (if async == False) only and only if start() method has been called.
source code
Python object
pop(self, key, cache_dir=None, aging_days=None)
This is the place where data is retrieved from cache.
source code

Inherited from core.Singleton: __init__

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Methods
 
current_directory(cls)
Return the path to current EntropyCacher cache storage directory.
source code
 
clear_cache_item(cls, cache_item, cache_dir=None)
Clear Entropy Cache item from on-disk cache.
source code
Static Methods

Inherited from core.Singleton: __new__

Class Variables
  WRITEBACK_TIMEOUT = 5
  STASHING_CACHE = True
Entropy asynchronous and synchronous cache writer and reader.
Properties

Inherited from object: __class__

Method Details

init_singleton(self)

source code 

Singleton overloaded method. Equals to __init__. This is the place where all the properties initialization takes place.

start(self)

source code 

This is the method used to start the asynchronous cache writer but also the whole cacher. If this method is not called, the instance will always trash and cache write request.

Returns:
None

is_started(self)

source code 

Return whether start is called or not. This equals to checking if the cacher is running, thus is writing cache to disk.

Returns:
None

stop(self)

source code 

This method stops the execution of the cacher, which won't accept cache writes anymore. The thread responsible of writing to disk is stopped here and the Cacher will be back to being inactive. A watchdog will avoid the thread to freeze the call if the write buffer is overloaded.

Returns:
None

sync(self)

source code 

This method can be called anytime and forces the instance to flush all the cache writes queued to disk. If wait == False a watchdog prevents this call to get stuck in case of write buffer overloads.

discard(self)

source code 

This method makes buffered cache to be discarded synchronously.

Returns:
None

save(self, key, data, cache_dir=None)

source code 

Save data object to cache asynchronously and in any case. This method guarantees that cached data is stored even if cacher is not started. If data cannot be stored, IOError will be raised.

Parameters:
  • key (string) - cache data identifier
  • data (any picklable object) - picklable object
  • cache_dir (string) - alternative cache directory

push(self, key, data, async=True, cache_dir=None)

source code 

This is the place where data is either added to the write queue or written to disk (if async == False) only and only if start() method has been called.

Parameters:
  • key (string) - cache data identifier
  • data (any picklable object) - picklable object
  • async (bool) - store cache asynchronously or not
  • cache_dir (string) - alternative cache directory

pop(self, key, cache_dir=None, aging_days=None)

source code 

This is the place where data is retrieved from cache. You must know the cache identifier used when push() was called.

Parameters:
  • key (string) - cache data identifier
  • cache_dir (string) - alternative cache directory
Returns: Python object
object stored into the stack or None (if stack is empty)

clear_cache_item(cls, cache_item, cache_dir=None)
Class Method

source code 

Clear Entropy Cache item from on-disk cache.

Parameters:
  • cache_item (string) - Entropy Cache item identifier
  • cache_dir (string) - alternative cache directory

Class Variable Details

STASHING_CACHE

Entropy asynchronous and synchronous cache writer and reader. This class is a Singleton and contains a thread doing the cache writes asynchronously, thus it must be stopped before your application is terminated calling the stop() method.

Sample code:

>>> # import module
>>> from entropy.cache import EntropyCacher
...
>>> # first EntropyCacher load, start it
>>> cacher = EntropyCacher()
>>> cacher.start()
...
>>> # now store something into its cache
>>> cacher.push('my_identifier1', [1, 2, 3])
>>> # now store something synchronously
>>> cacher.push('my_identifier2', [1, 2, 3], async = False)
...
>>> # now flush all the caches to disk, and make sure all
>>> # is written
>>> cacher.sync()
...
>>> # now fetch something from the cache
>>> data = cacher.pop('my_identifier1')
[1, 2, 3]
...
>>> # now discard all the cached (async) writes
>>> cacher.discard()
...
>>> # and stop EntropyCacher
>>> cacher.stop()
Value:
True