diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-10-24 12:16:55 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-10-24 12:16:55 -0400 |
commit | 16b29ab85a26e0c7d72ae99ada00632bedf28a3c (patch) | |
tree | d3075cf510e4510c0d96096d71b55b1392899ee5 | |
parent | cac6e8f7890dcf53c6a243a0768ac749c4688524 (diff) | |
download | dogpile-core-16b29ab85a26e0c7d72ae99ada00632bedf28a3c.tar.gz |
integrate the registry with dogpile
-rw-r--r-- | README.rst | 7 | ||||
-rw-r--r-- | dogpile/__init__.py | 1 | ||||
-rw-r--r-- | dogpile/dogpile.py | 18 |
3 files changed, 22 insertions, 4 deletions
@@ -192,12 +192,12 @@ Here's the memcached example again using that technique:: import pylibmc mc_pool = pylibmc.ThreadMappedPool(pylibmc.Client("localhost")) - from dogpile import Dogpile, NeedRegenerationException, NameRegistry + from dogpile import Dogpile, NeedRegenerationException import pickle import time def cache(expiration_time) - dogpile_registry = NameRegistry(lambda identifier: Dogpile(expiration_time)) + dogpile_registry = Dogpile.registry(expiration_time)) def get_or_create(key): @@ -225,7 +225,8 @@ Here's the memcached example again using that technique:: return get_or_create -Above, we use a ``NameRegistry`` which will give us a ``Dogpile`` object that's +Above, we use ``Dogpile.registry()`` to create a name-based "registry" of ``Dogpile`` +objects. This object will provide to us a ``Dogpile`` object that's unique on a certain name. When all usages of that name are complete, the ``Dogpile`` object falls out of scope, so total number of keys used is not a memory issue. Then, tell Dogpile that we'll give it the "creation time" that we'll store in our diff --git a/dogpile/__init__.py b/dogpile/__init__.py index f6ef8df..9dfc847 100644 --- a/dogpile/__init__.py +++ b/dogpile/__init__.py @@ -1,5 +1,4 @@ from dogpile import Dogpile, SyncReaderDogpile, NeedRegenerationException -from nameregistry import NameRegistry __version__ = '0.2.1' diff --git a/dogpile/dogpile.py b/dogpile/dogpile.py index 6ec906e..94a50c8 100644 --- a/dogpile/dogpile.py +++ b/dogpile/dogpile.py @@ -66,6 +66,7 @@ from util import thread, threading import time import logging from readwrite_lock import ReadWriteMutex +from nameregistry import NameRegistry log = logging.getLogger(__name__) @@ -93,6 +94,23 @@ class Dogpile(object): else: self.createdtime = -1 + @clasmethod + def registry(cls, *arg, **kw): + """Return a name-based registry of :class:`.Dogpile` objects. + + The registry is an instance of :class:`.NameRegistry`, + and calling its ``get()`` method with an identifying + key (anything hashable) will construct a new :class:`.Dogpile` + object, keyed to that key. Subsequent usages will return + the same :class:`.Dogpile` object for as long as the + object remains in scope. + + The given arguments are passed along to the underlying + constructor of the :class:`.Dogpile` class. + + """ + return NameRegistry(lambda identifier: cls(*arg, **kw)) + def acquire(self, creator, value_fn=None, value_and_created_fn=None): |