Mercurial > libervia-backend
diff src/memory/cache.py @ 2509:d485e9416493
core (memory/cache): common cache:
host has now a common_cache attribute for data which can be shared between profiles. client.cache is for private data
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 02 Mar 2018 17:40:09 +0100 |
parents | 516bf5309517 |
children | cd7a53c31eb6 |
line wrap: on
line diff
--- a/src/memory/cache.py Fri Mar 02 17:37:41 2018 +0100 +++ b/src/memory/cache.py Fri Mar 02 17:40:09 2018 +0100 @@ -27,16 +27,25 @@ import os.path import time +DEFAULT_EXT = '.raw' + class Cache(object): """generic file caching""" - def __init__(self, host, profile=None): + def __init__(self, host, profile): + """ + @param profile(unicode, None): ame of the profile to set the cache for + if None, the cache will be common for all profiles + """ self.profile = profile - self.cache_dir = os.path.join( - host.memory.getConfig('', 'local_dir'), - C.CACHE_DIR, - regex.pathEscape(profile or '')) + path_elts = [host.memory.getConfig('', 'local_dir'), C.CACHE_DIR] + if profile: + path_elts.extend([u'profiles',regex.pathEscape(profile)]) + else: + path_elts.append(u'common') + self.cache_dir = os.path.join(*path_elts) + if not os.path.exists(self.cache_dir): os.makedirs(self.cache_dir) @@ -59,7 +68,7 @@ """ uid = uid.strip() if not uid: - return None + raise exceptions.InternalError(u"uid must not be empty") cache_url = self.getPath(uid) if not os.path.exists(cache_url): return None @@ -86,7 +95,7 @@ return self.getPath(cache_data['filename']) - def cacheData(self, source, uid, mime_type=u'', max_age=None, filename=None): + def cacheData(self, source, uid, mime_type=None, max_age=None, filename=None): """create cache metadata and file object to use for actual data @param source(unicode): source of the cache (should be plugin's import_name) @@ -102,21 +111,25 @@ @return(file): file object opened in write mode you have to close it yourself (hint: use with statement) """ - #Â FIXME: is it needed to use a separate thread? - # probably not with the little data expected with BoB cache_url = self.getPath(uid) - ext = mimetypes.guess_extension(mime_type, strict=False) - if ext is None: - log.warning(u"can't find extension for MIME type {}".format(mime_type)) - ext = '.dump' if filename is None: + if mime_type: + ext = mimetypes.guess_extension(mime_type, strict=False) + if ext is None: + log.warning(u"can't find extension for MIME type {}".format(mime_type)) + ext = DEFAULT_EXT + elif ext == u'.jpe': + ext = u'.jpg' + else: + ext = DEFAULT_EXT + mime_type = None filename = uid + ext if max_age is None: max_age = C.DEFAULT_MAX_AGE - cache_data = {'source': source, - 'filename': filename, - 'eol': int(time.time()) + max_age, - 'mime_type': mime_type, + cache_data = {u'source': source, + u'filename': filename, + u'eol': int(time.time()) + max_age, + u'mime_type': mime_type, } file_path = self.getPath(filename)