comparison sat/memory/cache.py @ 3198:08151c103636

core (memory/cache): added some metadata: - creation/last_access are now set - use filename to guess MIME type when suitable
author Goffi <goffi@goffi.org>
date Sun, 01 Mar 2020 18:31:45 +0100
parents a15773c6c273
children f14eb24328d0
comparison
equal deleted inserted replaced
3197:f4a28767ec35 3198:08151c103636
155 try: 155 try:
156 max_age = cache_data["max_age"] 156 max_age = cache_data["max_age"]
157 except KeyError: 157 except KeyError:
158 log.warning(f"no max_age found for cache at {cache_url}, using default") 158 log.warning(f"no max_age found for cache at {cache_url}, using default")
159 max_age = cache_data["max_age"] = C.DEFAULT_MAX_AGE 159 max_age = cache_data["max_age"] = C.DEFAULT_MAX_AGE
160 cache_data["eol"] = int(time.time()) + max_age 160 now = int(time.time())
161 cache_data["last_access"] = now
162 cache_data["eol"] = now + max_age
161 with cache_url.open("wb") as f: 163 with cache_url.open("wb") as f:
162 pickle.dump(cache_data, f, protocol=2) 164 pickle.dump(cache_data, f, protocol=2)
163 165
164 cache_data["path"] = self.getPath(cache_data["filename"]) 166 cache_data["path"] = self.getPath(cache_data["filename"])
165 return cache_data 167 return cache_data
180 182
181 @param source(unicode): source of the cache (should be plugin's import_name) 183 @param source(unicode): source of the cache (should be plugin's import_name)
182 @param uid(unicode): an identifier of the file which must be unique 184 @param uid(unicode): an identifier of the file which must be unique
183 @param mime_type(unicode): MIME type of the file to cache 185 @param mime_type(unicode): MIME type of the file to cache
184 it will be used notably to guess file extension 186 it will be used notably to guess file extension
187 It may be autogenerated if filename is specified
185 @param max_age(int, None): maximum age in seconds 188 @param max_age(int, None): maximum age in seconds
186 the cache metadata will have an "eol" (end of life) 189 the cache metadata will have an "eol" (end of life)
187 None to use default value 190 None to use default value
188 0 to ignore cache (file will be re-downloaded on each access) 191 0 to ignore cache (file will be re-downloaded on each access)
189 @param filename: if not None, will be used as filename 192 @param filename: if not None, will be used as filename
204 ext = ".jpg" 207 ext = ".jpg"
205 else: 208 else:
206 ext = DEFAULT_EXT 209 ext = DEFAULT_EXT
207 mime_type = None 210 mime_type = None
208 filename = uid + ext 211 filename = uid + ext
212 elif mime_type is None:
213 # we have filename but not MIME type, we try to guess the later
214 mime_type = mimetypes.guess_type(filename, strict=False)[0]
209 if max_age is None: 215 if max_age is None:
210 max_age = C.DEFAULT_MAX_AGE 216 max_age = C.DEFAULT_MAX_AGE
217 now = int(time.time())
211 cache_data = { 218 cache_data = {
212 "source": source, 219 "source": source,
213 "filename": filename, 220 "filename": filename,
214 "eol": int(time.time()) + max_age, 221 "creation": now,
222 "eol": now + max_age,
215 # we also store max_age for updating eol 223 # we also store max_age for updating eol
216 "max_age": max_age, 224 "max_age": max_age,
217 "mime_type": mime_type, 225 "mime_type": mime_type,
218 } 226 }
219 file_path = self.getPath(filename) 227 file_path = self.getPath(filename)