Anonymous avatar Anonymous committed b1b5386

create deferred configuration

Comments (0)

Files changed (3)

 
     from dogpile.cache import make_region
 
-    region = make_region(
+    region = make_region().configure(
         'dogpile.cache.pylibmc',
         expiration_time = 3600,
         arguments = {
     def load_user_info(user_id):
         return some_database.lookup_user_by_id(user_id)
 
-Above, we create a ``CacheRegion`` using the ``make_region()`` function.  This 
-function accepts as arguments the name of the backend as the only required argument,
+Above, we create a ``CacheRegion`` using the ``make_region()`` function, then
+apply the backend configuration via the ``configure()`` method, which returns the 
+region.  The name of the backend is the only required argument,
 in this case ``dogpile.cache.pylibmc``.
 
 Subsequent arguments then include *expiration_time*, which is the expiration 
 
 ``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.
+
+``function_key_generator``
+
+  Optional.  Plug in a function that will produce a "cache key" given 
+  a data creation function and arguments.   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(fn):
+        namespace = fn.__name__
+        def generate_key(*arg):
+            return namespace + "_".join(str(s) for s in arg)
+        return generate_key
+
+
+    region = make_region(
+        "dogpile.cache.dbm",
+        expiration_time=300,
+        arguments={
+            "filename":"file.dbm"
+        },
+        function_key_generator = my_key_generator
+
+    )
+
+``key_mangler``
+
+  Optional.  Function which will "mangle" the incoming keys.  If left
+  at ``None``, the backend may provide a default "mangler" function.
+  Set to ``False`` to unconditionally disable key mangling.
+
+One you have a ``CacheRegion``, the ``cache_on_arguments()`` method can
+be used to decorate functions, but the cache itself can't be used until
+``configure()`` is called.  That method accepts these arguments:
+
+``backend``
   Required.  This is the name of the ``CacheBackend`` to use, and
   is resolved by loading the class from the ``dogpile.cache`` entrypoint.
 
   Optional.  The structure here is passed directly to the constructor
   of the ``CacheBackend`` in use, though is typically a dictionary.
 
-``function_key_generator``
+Configure Region from a Configuration Dictionary
+------------------------------------------------
 
-  Optional.  Plug in a function that will produce a "cache key" given 
-  a data creation function and arguments.   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::
+Call ``configure_from_config()`` instead::
 
-    def my_key_generator(fn):
-        namespace = fn.__name__
-        def generate_key(*arg):
-            return namespace + "_".join(str(s) for s in arg)
-        return generate_key
+    local_region = make_region()
+    memcached_region = make_region()
 
+    # regions are ready to use for function
+    # decorators, but not yet for actual caching
 
-    region = make_region(
-        "dogpile.cache.dbm",
-        expiration_time=300,
-        arguments={
-            "filename":"file.dbm"
-        },
-        function_key_generator = my_key_generator
-
-    )
+    # later, when config is available
+    myconfig = {
+        "cache.local.backend":"dogpile.cache.dbm",
+        "cache.local.arguments.filename":"/path/to/dbmfile.dbm",
+        "cache.memcached.backend":"dogpile.cache.pylibmc",
+        "cache.memcached.arguments.url":"127.0.0.1, 10.0.0.1",
+    }
+    local_region.configure_from_config(myconfig, "cache.local.")
+    memcached_region.configure_from_config(myconfig, "cache.memcached.")
 
 Using a Region
 --------------

dogpile/cache/api.py

         """
         raise NotImplementedError()
 
+    @classmethod
+    def from_config_dict(cls, config_dict, prefix):
+        prefix_len = len(prefix)
+        return cls(
+                dict(
+                    (key[prefix_len:], config_dict[key]) 
+                    for key in config_dict 
+                    if key.startwith(prefix)
+                )
+            )
+
     def get(self, key):
         """Retrieve a value from the cache.
         

dogpile/cache/region.py

 class CacheRegion(object):
     """A front end to a particular cache backend."""
 
-    def __init__(self, name, 
-            expiration_time=None,
-            arguments=None,
+    def __init__(self,
+            name=None, 
             function_key_generator=function_key_generator,
             key_mangler=None,
-        ):
+
+    ):
         """Construct a new :class:`.CacheRegion`.
         
-        :param name: Cache backend name.
-        :param expiration_time: Expiration time, in seconds
-        :param arguments: Argument structure passed to the 
-         backend.  Is typically a dict.
-        :function_key_generator: Key generator used by
+        :param name: Optional, name for the region.
+        :function_key_generator: Optional, key generator used by
          :meth:`.CacheRegion.cache_on_arguments`.
         :key_mangler: Function which will be used on all incoming
          keys before passing to the backend.  Defaults to ``None``,
          which coerces keys into a SHA1
          hash, so that the string length is fixed.  To
          disable all key mangling, set to ``False``.
-         
+        
         """
-        self.backend = _backend_loader.load(name)(arguments)
         self.function_key_generator = function_key_generator
         if key_mangler:
             self.key_mangler = key_mangler
-        elif key_mangler is False:
+        else:
             self.key_mangler = None
+
+    def configure(self, backend,
+            expiration_time=None,
+            arguments=None,
+            _config_argument_dict=None,
+            _config_prefix=None
+        ):
+        """Configure a :class:`.CacheRegion`
+        
+        The :class:`.CacheRegion` itself 
+        is returned..
+        
+        :param backend: Cache backend name
+        :param expiration_time: Expiration time, in seconds
+        :param arguments: Argument structure passed to the 
+         backend.  Is typically a dict.
+         
+        """
+        backend_cls = _backend_loader.load(backend)
+        if _config_argument_dict:
+            self.backend = backend_cls.from_config_dict(
+                _config_argument_dict,
+                _config_prefix
+            )
         else:
+            self.backend = backend_cls(arguments)
+        self.dogpile_registry = Dogpile.registry(expiration_time)
+        if self.key_mangler is None:
             self.key_mangler = backend.key_mangler
-        self.dogpile_registry = Dogpile.registry(expiration_time)
+        return self
+
+    def configure_from_config(self, config_dict, prefix):
+        """Configure from a configuration dictionary 
+        and a prefix."""
+        return self.configure(
+            config_dict["%s.backend" % prefix],
+            expiration_time = config_dict.get("%s.expiration_time" % prefix, None),
+            _config_argument_dict=config_dict,
+            _config_prefix="%s.arguments" % prefix
+        )
 
     def get(self, key):
         """Return a value from the cache, based on the given key.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.