Repository URL to install this package:
|
Version:
3.0.176 ▾
|
tvault-contego-common
/
home
/
tvault
/
.virtenv
/
lib
/
python2.7
/
site-packages
/
dogpile
/
cache
/
region.pyc
|
|---|
ó
ëEYc @ sl d d l m Z d d l m Z m Z d d l m Z d d l m Z d d l m Z m Z m
Z
d d l m Z m Z d d l
m Z m Z d d
l m Z d d l m Z d d l Z d d l Z d d
l m Z d d l m Z d d l Z e d Z e j Z d d l m Z d Z d e f d YZ d e f d YZ! d e f d YZ" d Z# d S( iÿÿÿÿ( t with_statementi ( t Lockt NeedRegenerationException( t NameRegistryi ( t exception( t PluginLoadert memoized_propertyt coerce_string_conf( t function_key_generatort function_multi_key_generator( t NO_VALUEt CachedValue( t ProxyBackend( t compatN( t Number( t wrapss
dogpile.cache( t backendst RegionInvalidationStrategyc B sG e Z d Z e d Z d Z d Z d Z d Z d Z RS( s7 Region invalidation strategy interface
Implement this interface and pass implementation instance
to :meth:`.CacheRegion.configure` to override default region invalidation.
Example::
class CustomInvalidationStrategy(RegionInvalidationStrategy):
def __init__(self):
self._soft_invalidated = None
self._hard_invalidated = None
def invalidate(self, hard=None):
if hard:
self._soft_invalidated = None
self._hard_invalidated = time.time()
else:
self._soft_invalidated = time.time()
self._hard_invalidated = None
def is_invalidated(self, timestamp):
return ((self._soft_invalidated and
timestamp < self._soft_invalidated) or
(self._hard_invalidated and
timestamp < self._hard_invalidated))
def was_hard_invalidated(self):
return bool(self._hard_invalidated)
def is_hard_invalidated(self, timestamp):
return (self._hard_invalidated and
timestamp < self._hard_invalidated)
def was_soft_invalidated(self):
return bool(self._soft_invalidated)
def is_soft_invalidated(self, timestamp):
return (self._soft_invalidated and
timestamp < self._soft_invalidated)
The custom implementation is injected into a :class:`.CacheRegion`
at configure time using the
:paramref:`.CacheRegion.configure.region_invalidator` parameter::
region = CacheRegion()
region = region.configure(region_invalidator=CustomInvalidationStrategy())
Invalidation strategies that wish to have access to the
:class:`.CacheRegion` itself should construct the invalidator given the
region as an argument::
class MyInvalidator(RegionInvalidationStrategy):
def __init__(self, region):
self.region = region
# ...
# ...
region = CacheRegion()
region = region.configure(region_invalidator=MyInvalidator(region))
.. versionadded:: 0.6.2
.. seealso::
:paramref:`.CacheRegion.configure.region_invalidator`
c C s
t d S( sý Region invalidation.
:class:`.CacheRegion` propagated call.
The default invalidation system works by setting
a current timestamp (using ``time.time()``) to consider all older
timestamps effectively invalidated.
N( t NotImplementedError( t selft hard( ( sI /home/tvault/.virtenv/lib/python2.7/site-packages/dogpile/cache/region.pyt
invalidated s
c C s
t d S( sÝ Check timestamp to determine if it was hard invalidated.
:return: Boolean. True if ``timestamp`` is older than
the last region invalidation time and region is invalidated
in hard mode.
N( R ( R t timestamp( ( sI /home/tvault/.virtenv/lib/python2.7/site-packages/dogpile/cache/region.pyt is_hard_invalidatedp s c C s
t d S( sÝ Check timestamp to determine if it was soft invalidated.
:return: Boolean. True if ``timestamp`` is older than
the last region invalidation time and region is invalidated
in soft mode.
N( R ( R R ( ( sI /home/tvault/.virtenv/lib/python2.7/site-packages/dogpile/cache/region.pyt is_soft_invalidated{ s c C s
t d S( s¨ Check timestamp to determine if it was invalidated.
:return: Boolean. True if ``timestamp`` is older than
the last region invalidation time.
N( R ( R R ( ( sI /home/tvault/.virtenv/lib/python2.7/site-packages/dogpile/cache/region.pyt is_invalidated s c C s
t d S( s Indicate the region was invalidated in soft mode.
:return: Boolean. True if region was invalidated in soft mode.
N( R ( R ( ( sI /home/tvault/.virtenv/lib/python2.7/site-packages/dogpile/cache/region.pyt was_soft_invalidated s c C s
t d S( s Indicate the region was invalidated in hard mode.
:return: Boolean. True if region was invalidated in hard mode.
N( R ( R ( ( sI /home/tvault/.virtenv/lib/python2.7/site-packages/dogpile/cache/region.pyt was_hard_invalidated s (
t __name__t
__module__t __doc__t TrueR R R R R R ( ( ( sI /home/tvault/.virtenv/lib/python2.7/site-packages/dogpile/cache/region.pyR s F
t DefaultInvalidationStrategyc B sJ e Z d Z e d Z d Z d Z d Z d Z d Z RS( c C s d | _ d | _ d S( N( t Nonet _is_hard_invalidatedt _invalidated( R ( ( sI /home/tvault/.virtenv/lib/python2.7/site-packages/dogpile/cache/region.pyt __init__¥ s c C s" t | | _ t j | _ d S( N( t boolR" t timeR# ( R R ( ( sI /home/tvault/.virtenv/lib/python2.7/site-packages/dogpile/cache/region.pyR © s c C s | j d k o | | j k S( N( R# R! ( R R ( ( sI /home/tvault/.virtenv/lib/python2.7/site-packages/dogpile/cache/region.pyR s c C s
| j t k S( N( R" R ( R ( ( sI /home/tvault/.virtenv/lib/python2.7/site-packages/dogpile/cache/region.pyR ± s c C s | j o | j | S( N( R R ( R R ( ( sI /home/tvault/.virtenv/lib/python2.7/site-packages/dogpile/cache/region.pyR ´ s c C s
| j t k S( N( R" t False( R ( ( sI /home/tvault/.virtenv/lib/python2.7/site-packages/dogpile/cache/region.pyR · s c C s | j o | j | S( N( R R ( R R ( ( sI /home/tvault/.virtenv/lib/python2.7/site-packages/dogpile/cache/region.pyR º s (
R R R$ R R R R R R R ( ( ( sI /home/tvault/.virtenv/lib/python2.7/site-packages/dogpile/cache/region.pyR £ s t CacheRegionc B sS e Z d Z d e e d d d Z d d d d d e d d Z d Z d Z
d e f d YZ d Z
e d Z d Z e d
Z e d Z d e d Z d
Z d e d Z d d d Z d d d Z d Z d Z d Z d Z d Z d d d e j d d Z! d d d e e j d d Z" RS( s A front end to a particular cache backend.
:param name: Optional, a string name for the region.
This isn't used internally
but can be accessed via the ``.name`` parameter, helpful
for configuring a region from a config file.
:param function_key_generator: Optional. A
function that will produce a "cache key" given
a data creation function and arguments, when using
the :meth:`.CacheRegion.cache_on_arguments` method.
The structure of this function
should be two levels: given the data creation function,
return a new function that generates the key based on
the given arguments. Such as::
def my_key_generator(namespace, fn, **kw):
fname = fn.__name__
def generate_key(*arg):
return namespace + "_" + fname + "_".join(str(s) for s in arg)
return generate_key
region = make_region(
function_key_generator = my_key_generator
).configure(
"dogpile.cache.dbm",
expiration_time=300,
arguments={
"filename":"file.dbm"
}
)
The ``namespace`` is that passed to
:meth:`.CacheRegion.cache_on_arguments`. It's not consulted
outside this function, so in fact can be of any form.
For example, it can be passed as a tuple, used to specify
arguments to pluck from \**kw::
def my_key_generator(namespace, fn):
def generate_key(*arg, **kw):
return ":".join(
[kw[k] for k in namespace] +
[str(x) for x in arg]
)
return generate_key
Where the decorator might be used as::
@my_region.cache_on_arguments(namespace=('x', 'y'))
def my_function(a, b, **kw):
return my_data()
.. seealso::
:func:`.function_key_generator` - default key generator
:func:`.kwarg_function_key_generator` - optional gen that also
uses keyword arguments
:param function_multi_key_generator: Optional.
Similar to ``function_key_generator`` parameter, but it's used in
:meth:`.CacheRegion.cache_multi_on_arguments`. Generated function
should return list of keys. For example::
def my_multi_key_generator(namespace, fn, **kw):
namespace = fn.__name__ + (namespace or '')
def generate_keys(*args):
return [namespace + ':' + str(a) for a in args]
return generate_keys
:param key_mangler: Function which will be used on all incoming
keys before passing to the backend. Defaults to ``None``,
in which case the key mangling function recommended by
the cache backend will be used. A typical mangler
is the SHA1 mangler found at :func:`.sha1_mangle_key`
which coerces keys into a SHA1
hash, so that the string length is fixed. To
disable all key mangling, set to ``False``. Another typical
mangler is the built-in Python function ``str``, which can be used
to convert non-string or Unicode keys to bytestrings, which is
needed when using a backend such as bsddb or dbm under Python 2.x
in conjunction with Unicode keys.
:param async_creation_runner: A callable that, when specified,
will be passed to and called by dogpile.lock when
there is a stale value present in the cache. It will be passed the
mutex and is responsible releasing that mutex when finished.
This can be used to defer the computation of expensive creator
functions to later points in the future by way of, for example, a
background thread, a long-running queue, or a task manager system
like Celery.
For a specific example using async_creation_runner, new values can
be created in a background thread like so::
import threading
def async_creation_runner(cache, somekey, creator, mutex):
''' Used by dogpile.core:Lock when appropriate '''
def runner():
try:
value = creator()
cache.set(somekey, value)
finally:
mutex.release()
thread = threading.Thread(target=runner)
thread.start()
region = make_region(
async_creation_runner=async_creation_runner,
).configure(
'dogpile.cache.memcached',
expiration_time=5,
arguments={
'url': '127.0.0.1:11211',
'distributed_lock': True,
}
)
Remember that the first request for a key with no associated
value will always block; async_creator will not be invoked.
However, subsequent requests for cached-but-expired values will
still return promptly. They will be refreshed by whatever
asynchronous means the provided async_creation_runner callable
implements.
By default the async_creation_runner is disabled and is set
to ``None``.
.. versionadded:: 0.4.2 added the async_creation_runner
feature.
c C sD | | _ | | _ | | _ | | _ | _ | | _ t | _ d S( s&