comparison sat/memory/cache.py @ 3210:fedec192a83f

core (memory/cache): new removeFromCache method
author Goffi <goffi@goffi.org>
date Fri, 06 Mar 2020 18:19:03 +0100
parents f14eb24328d0
children be6d91572633
comparison
equal deleted inserted replaced
3209:f14eb24328d0 3210:fedec192a83f
181 None if file is not in cache (or cache is invalid) 181 None if file is not in cache (or cache is invalid)
182 """ 182 """
183 metadata = self.getMetadata(uid) 183 metadata = self.getMetadata(uid)
184 if metadata is not None: 184 if metadata is not None:
185 return metadata["path"] 185 return metadata["path"]
186
187 def removeFromCache(self, uid, metadata=None):
188 """Remove data from cache
189
190 @param uid(unicode): unique identifier cache file
191 """
192 cache_data = self.getMetadata(uid, update_eol=False)
193 if cache_data is None:
194 log.debug(f"cache with uid {uid!r} has already expired or been removed")
195 return
196
197 try:
198 filename = cache_data['filename']
199 except KeyError:
200 log.warning(_("missing filename for cache {uid!r}") .format(uid=uid))
201 else:
202 filepath = self.getPath(filename)
203 try:
204 filepath.unlink()
205 except FileNotFoundError:
206 log.warning(
207 _("missing file referenced in cache {uid!r}: {filename}")
208 .format(uid=uid, filename=filename)
209 )
210
211 cache_file = self.getPath(uid)
212 cache_file.unlink()
213 log.debug(f"cache with uid {uid!r} has been removed")
186 214
187 def cacheData(self, source, uid, mime_type=None, max_age=None, filename=None): 215 def cacheData(self, source, uid, mime_type=None, max_age=None, filename=None):
188 """create cache metadata and file object to use for actual data 216 """create cache metadata and file object to use for actual data
189 217
190 @param source(unicode): source of the cache (should be plugin's import_name) 218 @param source(unicode): source of the cache (should be plugin's import_name)