dogpile.cache-dict-config / dogpile / cache /

zzzeek 4192624 

Mike Bayer b4b1b54 
zzzeek 4192624 

Mike Bayer 1a81a45 
Mike Bayer b4b1b54 

Mike Bayer 1a81a45 
Mike Bayer b4b1b54 
zzzeek 4192624 

Mike Bayer 8a6571a 

zzzeek 4192624 

zzzeek 0ee7467 

Mike Bayer 1a81a45 

zzzeek 0ee7467 

zzzeek 4192624 

zzzeek b1b5386 

zzzeek e87b051 

zzzeek 4192624 

Mike Bayer bf5be08 

zzzeek 4192624 

import operator

class NoValue(object):
    """Describe a missing cache value.
    The :attr:`.NO_VALUE` module global
    should be used.
    def payload(self):
        return self

    def __nonzero__(self):
        return False

NO_VALUE = NoValue()
"""Value returned from ``get()`` that describes 
a  key not present."""

class CachedValue(tuple):
    """Represent a value stored in the cache.
    :class:`.CachedValue` is a two-tuple of
    ``(payload, metadata)``, where ``metadata``
    is dogpile.cache's tracking information (
    currently the creation time).  The metadata
    and tuple structure is pickleable, if 
    the backend requires serialization.
    payload = property(operator.itemgetter(0))
    """Named accessor for the payload."""

    metadata = property(operator.itemgetter(1))
    """Named accessor for the dogpile.cache metadata dictionary."""

    def __new__(cls, payload, metadata):
        return tuple.__new__(cls, (payload, metadata))

    def __reduce__(self):
        return CachedValue, (self.payload, self.metadata)

class CacheBackend(object):
    """Base class for backend implementations."""

    key_mangler = None
    """Key mangling function.  
    May be None, or otherwise declared
    as an ordinary instance method.


    def __init__(self, arguments):
        """Construct a new :class:`.CacheBackend`.
        Subclasses should override this to
        handle the given arguments.
        :param arguments: The ``arguments`` parameter
         passed to :func:`.make_registry`.
        raise NotImplementedError()

    def from_config_dict(cls, config_dict, prefix):
        prefix_len = len(prefix)
        return cls(
                    (key[prefix_len:], config_dict[key]) 
                    for key in config_dict 
                    if key.startwith(prefix)

    def get_mutex(self, key):
        """Return an optional mutexing object for the given key.

        This object need only provide an ``acquire()``
        and ``release()`` method.
        May return ``None``, in which case the dogpile
        lock will use a regular ``threading.Lock``
        object to mutex concurrent threads for 
        value creation.   The default implementation
        returns ``None``.
        Different backends may want to provide various
        kinds of "mutex" objects, such as those which
        link to lock files, distributed mutexes,
        memcached semaphores, timers, etc.  Whatever
        kind of system is best suited for the scope
        and behavior of the caching backend.
        return None

    def get(self, key):
        """Retrieve a value from the cache.
        The returned value should be an instance of
        :class:`.CachedValue`, or ``NO_VALUE`` if
        not present.
        raise NotImplementedError()

    def set(self, key, value):
        """Set a value in the cache.
        The key will be whatever was passed
        to the registry, processed by the
        "key mangling" function, if any.
        The value will always be an instance
        of :class:`.CachedValue`.
        raise NotImplementedError()

    def delete(self, key):
        """Delete a value from the cache.
        The key will be whatever was passed
        to the registry, processed by the
        "key mangling" function, if any.
        The behavior here should be idempotent,
        that is, can be called any number of times
        regardless of whether or not the
        key exists.
        raise NotImplementedError()