Ejemplo n.º 1
0
class MemcachedCache_Master(MemcachedCache, object):
    """ the master instance of the memcached cache interface.
    copies of this object will NOT share the connection, but instantiate their own.
    When this master is destroyed it can flush the database, effectively erasing all data
    
    Parameters
    ----------
    memc_params : dict
        dictionary with keys [host, port] to the memcached-server instance
    wait_on_insert : bool
        if True, wait on insert operations to successfully complete before continuing (default: False)
    val_ttl : int >0
        the time to life for each value in seconds
    flush_on_del : bool
        flush the databases when the object is destroyed (default: False)
    """

    def __init__(self,
                 memc_params=_DEFAULT_MEMCACHE_CRED,
                 wait_on_insert=False,
                 val_ttl=12,
                 flush_on_del=False):
        self.is_master = True
        self.wait_on_insert = wait_on_insert
        self.memc_params = memc_params
        self.val_ttl = val_ttl
        self.flush_on_del = flush_on_del
        self.client = None

    def __del__(self):
        if self.flush_on_del:
            self.client = Client((self.memc_params['host'], self.memc_params['port']),
                                 default_noreply=not self.wait_on_insert)
            self.client.flushdb()