def render(self, context): content = self.nodelist.render(context) if 'MEDIA_URL' in context: media_url = context['MEDIA_URL'] else: media_url = settings.MEDIA_URL if self.kind == 'css': compressor = CssCompressor(content, xhtml=self.xhtml, media_url=media_url) if self.kind == 'js': compressor = JsCompressor(content, xhtml=self.xhtml, media_url=media_url) in_cache = cache.get(compressor.cachekey) if in_cache: return in_cache else: # do this to prevent dog piling in_progress_key = '%s.django_css.in_progress.%s' % (DOMAIN, compressor.cachekey) added_to_cache = cache.add(in_progress_key, True, 300) if added_to_cache: output = compressor.output() cache.set(compressor.cachekey, output, 2591000) # rebuilds the cache every 30 days if nothing has changed. cache.set(in_progress_key, False, 300) else: while cache.get(in_progress_key): sleep(0.1) output = cache.get(compressor.cachekey) return output
def render_offline(self, forced): """ If enabled and in offline mode, and not forced or in debug mode check the offline cache and return the result if given """ if (settings.COMPRESS_ENABLED and settings.COMPRESS_OFFLINE) and not forced: return cache.get(get_offline_cachekey(self.nodelist))
def render(self, context, forced=False): if (settings.COMPRESS_ENABLED and settings.COMPRESS_OFFLINE) and not forced: key = get_offline_cachekey(self.nodelist) content = cache.get(key) if content: return content content = self.nodelist.render(context) if (not settings.COMPRESS_ENABLED or not len(content.strip())) and not forced: return content compressor = self.compressor_cls(content) cachekey = self.cache_key(compressor) output = self.cache_get(cachekey) if output is None or forced: try: if self.mode == OUTPUT_INLINE: return compressor.output_inline() output = compressor.output(forced=forced) self.cache_set(cachekey, output) except: if settings.DEBUG: from traceback import format_exc raise Exception(format_exc()) else: return content return output
def render(self, context, compress=settings.COMPRESS, offline=settings.OFFLINE): if compress and offline: key = get_offline_cachekey(self.nodelist) content = cache.get(key) if content: return content content = self.nodelist.render(context) if offline or not compress or not len(content.strip()): return content if self.kind == 'css': compressor = CssCompressor(content) if self.kind == 'js': compressor = JsCompressor(content) cachekey = "%s-%s" % (compressor.cachekey, self.mode) output = self.cache_get(cachekey) if output is None: try: if self.mode == OUTPUT_FILE: output = compressor.output() else: output = compressor.output_inline() self.cache_set(cachekey, output) except: from traceback import format_exc raise Exception(format_exc()) return output
def input(self, **kwargs): key = self.get_cache_key() data = cache.get(key) if data is not None: return data filtered = super(TypeScriptFilter, self).input(**kwargs) cache.set(key, filtered, settings.COMPRESS_REBUILD_TIMEOUT) return filtered
def get_mtime(filename): if settings.MTIME_DELAY: key = get_mtime_cachekey(filename) mtime = cache.get(key) if mtime is None: mtime = os.path.getmtime(filename) cache.set(key, mtime, settings.MTIME_DELAY) return mtime return os.path.getmtime(filename)
def input(self, **kwargs): if self.mimetype in settings.COMPRESS_CACHEABLE_PRECOMPILERS: key = self.get_cache_key() data = cache.get(key) if data is not None: return data filtered = super(CachedCompilerFilter, self).input(**kwargs) cache.set(key, filtered, settings.COMPRESS_REBUILD_TIMEOUT) return filtered else: return super(CachedCompilerFilter, self).input(**kwargs)
def cache_get(self, key): packed_val = cache.get(key) if packed_val is None: return None val, refresh_time, refreshed = packed_val if (time.time() > refresh_time) and not refreshed: # Store the stale value while the cache # revalidates for another MINT_DELAY seconds. self.cache_set(key, val, timeout=settings.MINT_DELAY, refreshed=True) return None return val