comparison src/memory/cache.py @ 2517:cd7a53c31eb6

core (memory/cache): new getMetadata method to retrieve metadata without opening the file
author Goffi <goffi@goffi.org>
date Wed, 14 Mar 2018 08:05:55 +0100
parents d485e9416493
children
comparison
equal deleted inserted replaced
2516:842bd1594077 2517:cd7a53c31eb6
57 if not filename or u'/' in filename: 57 if not filename or u'/' in filename:
58 log.error(u"invalid char found in file name, hack attempt? name:{}".format(filename)) 58 log.error(u"invalid char found in file name, hack attempt? name:{}".format(filename))
59 raise exceptions.DataError(u"Invalid char found") 59 raise exceptions.DataError(u"Invalid char found")
60 return os.path.join(self.cache_dir, filename) 60 return os.path.join(self.cache_dir, filename)
61 61
62 def getFilePath(self, uid): 62 def getMetadata(self, uid):
63 """retrieve absolute path to file 63 """retrieve metadata for cached data
64 64
65 @param uid(unicode): unique identifier of file 65 @param uid(unicode): unique identifier of file
66 @return (unicode, None): absolute path to cached file 66 @return (dict, None): metadata with following keys:
67 see [cacheData] for data details, an additional "path" key is the full path to cached file.
67 None if file is not in cache (or cache is invalid) 68 None if file is not in cache (or cache is invalid)
68 """ 69 """
70
69 uid = uid.strip() 71 uid = uid.strip()
70 if not uid: 72 if not uid:
71 raise exceptions.InternalError(u"uid must not be empty") 73 raise exceptions.InternalError(u"uid must not be empty")
72 cache_url = self.getPath(uid) 74 cache_url = self.getPath(uid)
73 if not os.path.exists(cache_url): 75 if not os.path.exists(cache_url):
91 if eol < time.time(): 93 if eol < time.time():
92 log.debug(u"removing expired cache (expired for {}s)".format( 94 log.debug(u"removing expired cache (expired for {}s)".format(
93 time.time() - eol)) 95 time.time() - eol))
94 return None 96 return None
95 97
96 return self.getPath(cache_data['filename']) 98 cache_data['path'] = self.getPath(cache_data['filename'])
99 return cache_data
100
101 def getFilePath(self, uid):
102 """retrieve absolute path to file
103
104 @param uid(unicode): unique identifier of file
105 @return (unicode, None): absolute path to cached file
106 None if file is not in cache (or cache is invalid)
107 """
108 metadata = self.getMetadata(uid)
109 if metadata is not None:
110 return metadata['path']
97 111
98 def cacheData(self, source, uid, mime_type=None, max_age=None, filename=None): 112 def cacheData(self, source, uid, mime_type=None, max_age=None, filename=None):
99 """create cache metadata and file object to use for actual data 113 """create cache metadata and file object to use for actual data
100 114
101 @param source(unicode): source of the cache (should be plugin's import_name) 115 @param source(unicode): source of the cache (should be plugin's import_name)