def identifier(self): data_string = None if self.data: data_string = '-'.join('{}={}'.format(k, make_hashable(v)) for k, v in sorted(self.data.viewitems())) identifier = '{}:{}'.format(self.email, data_string or None) return sha1(identifier).hexdigest()
def wrapper(self, *args, **kwargs): primary_key = getattr(self, primary_key_attr) if hasattr(self, 'cache_version'): key = u'{}[{}.{}].{}'.format( type(self).__name__, primary_key, self.cache_version, f.__name__) else: key = u'{}[{}].{}'.format( type(self).__name__, primary_key, f.__name__) args_key = u', '.join( map(repr, map(make_hashable, args)) + [ u'{}={}'.format(k, make_hashable(v)) for k, v in sorted(kwargs.viewitems()) ]) if args_key: key = '{}({})'.format(key, args_key) result = cache.get(key, _not_cached) if result is _not_cached: result = f(self, *args, **kwargs) # Cache the value with a somewhat random expiry so we don't end up with all keys # expiring at the same time if there hasn't been an update for some time cache.set(key, result, base_ttl + 300 * random.randint(0, 200)) return result
def js_vars_global(): """ Provides a JS file with global definitions (all users) Useful for server-wide config options, URLs, etc... """ config = Config.getInstance() config_hash = crc32(repr(make_hashable(sorted(config._configVars.items())))) cache_file = os.path.join(config.getXMLCacheDir(), 'assets_global_{}.js'.format(config_hash)) if not os.path.exists(cache_file): data = generate_global_file(config) with open(cache_file, 'wb') as f: f.write(data) return send_file('global.js', cache_file, mimetype='application/x-javascript', no_cache=False, conditional=True)
def wrapper(self, *args, **kwargs): primary_key = getattr(self, primary_key_attr) if hasattr(self, 'cache_version'): key = u'{}[{}.{}].{}'.format(type(self).__name__, primary_key, self.cache_version, f.__name__) else: key = u'{}[{}].{}'.format(type(self).__name__, primary_key, f.__name__) args_key = u', '.join(map(repr, map(make_hashable, args)) + [u'{}={}'.format(k, make_hashable(v)) for k, v in sorted(kwargs.viewitems())]) if args_key: key = '{}({})'.format(key, args_key) result = cache.get(key, _not_cached) if result is _not_cached: result = f(self, *args, **kwargs) # Cache the value with a somewhat random expiry so we don't end up with all keys # expiring at the same time if there hasn't been an update for some time cache.set(key, result, base_ttl + 300 * random.randint(0, 200)) return result
def hash(self): return crc32(repr(make_hashable(sorted(self.data.items()))))