diff piecrust/app.py @ 371:c2ca72fb7f0b 2.0.0a8

caching: Use separate caches for config variants and other contexts. * The `_cache` directory is now organized in multiple "sub-caches" for different contexts. * A new context is created when config variants or overrides are applied. * `serve` context uses a different context that the other commends, to prevent the `bake` command's output from messing up the preview server (e.g. with how asset URLs are generated differently between the two). * Fix a few places where the cache directory was referenced directly.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 03 May 2015 23:59:46 -0700
parents 4b1019bb2533
children e7b865f8f335
line wrap: on
line diff
--- a/piecrust/app.py	Sun May 03 23:45:32 2015 -0700
+++ b/piecrust/app.py	Sun May 03 23:59:46 2015 -0700
@@ -403,7 +403,8 @@
         self.plugin_loader = PluginLoader(self)
 
         if cache:
-            self.cache = ExtensibleCache(self.cache_dir)
+            cache_dir = os.path.join(self.cache_dir, 'default')
+            self.cache = ExtensibleCache(cache_dir)
         else:
             self.cache = NullExtensibleCache()
 
@@ -494,6 +495,12 @@
     def cache_dir(self):
         return os.path.join(self.root_dir, CACHE_DIR)
 
+    @property  # Not a cached property because its result can change.
+    def sub_cache_dir(self):
+        if self.cache.enabled:
+            return self.cache.base_dir
+        return None
+
     @cached_property
     def sources(self):
         defs = {}
@@ -557,6 +564,18 @@
                 return tax
         return None
 
+    def useSubCache(self, cache_name, cache_key):
+        cache_hash = hashlib.md5(cache_key.encode('utf8')).hexdigest()
+        cache_dir = os.path.join(self.cache_dir,
+                                 '%s_%s' % (cache_name, cache_hash))
+        self._useSubCacheDir(cache_dir)
+
+    def _useSubCacheDir(self, cache_dir):
+        assert cache_dir
+        logger.debug("Moving cache to: %s" % cache_dir)
+        self.cache = ExtensibleCache(cache_dir)
+        self.env._onSubCacheDirChanged(self)
+
     def _get_dir(self, default_rel_dir):
         abs_dir = os.path.join(self.root_dir, default_rel_dir)
         if os.path.isdir(abs_dir):