Package entropy :: Module cache :: Class EntropyCacher

Class EntropyCacher

source code


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(wait = True)
...
>>> # 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()
Instance Methods
 
init_singleton(self)
Singleton overloaded method.
source code
 
__del__(self) 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
None
sync(self, wait=False)
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
None
push(self, key, data, async=True)
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)
This is the place where data is retrieved from cache.
source code

Inherited from core.Singleton: is_destroyed, is_singleton

Inherited from object: __delattr__, __getattribute__, __hash__, __init__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__

Static Methods

Inherited from core.Singleton: __new__

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, wait=False)

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.

Parameters:
  • wait (bool) - indicates if waiting until done (synchronous mode) or not
Returns: None
None

discard(self)

source code 

This method makes buffered cache to be discarded synchronously.

Returns:
None

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

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
Returns: None
None

pop(self, key)

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
Returns: Python object
object stored into the stack or None (if stack is empty)