def test_with_cache_v2(): test_category = f"test_with_cache_category_{time.time()}_{random.random()}" cache_duration = 2 def f() -> float: return get_now().timestamp() test_key = f"test_with_cache_key_{random.random()}" # 先触发一次cache miss c1 = with_cache(test_category, test_key, f, cache_max_seconds=cache_duration) # 移除_update字段,模拟上个版本的存盘记录 db = CacheDB().with_context(test_category).load() db_info = db.cache[test_key] db_info.update_at = db_info._update_at delattr(db_info, "_update_at") db.save() # 再次调用,此时应当重新触发cache miss c2 = with_cache(test_category, test_key, f, cache_max_seconds=cache_duration) assert c1 != c2
def test_with_cache(): test_category = f"test_with_cache_category_{time.time()}_{random.random()}" cache_duration = 2 def f() -> float: return get_now().timestamp() test_key = f"test_with_cache_key_{random.random()}" c1 = with_cache(test_category, test_key, f, cache_max_seconds=cache_duration) time.sleep(1.2 * cache_duration) c2 = with_cache(test_category, test_key, f, cache_max_seconds=cache_duration) assert c1 != c2
def _download(fname: str) -> str: return with_cache( "download_cache", os.path.join(folder.name, fname), cache_max_seconds=cache_max_seconds, cache_miss_func=lambda: self.download_file(self.find_file( folder, fname), download_dir, overwrite=overwrite, show_log=show_log), cache_validate_func=lambda target_path: os.path.isfile( target_path), )
def _download(fname: str) -> str: return with_cache( cache_name_download, os.path.join(folder.name, fname), cache_max_seconds=cache_max_seconds, cache_miss_func=lambda: self. download_file(self.find_file(folder, fname), download_dir, overwrite=overwrite, show_log=show_log, download_only_if_server_version_is_newer= download_only_if_server_version_is_newer), cache_validate_func=lambda target_path: os.path.isfile( target_path), )
def get_ide_act_desc_json(actId) -> str: actId = str(actId) act_cache_file = with_cache( "ide_act_desc", actId, cache_max_seconds=DESC_JS_CACHE_SECONDS, cache_miss_func=lambda: download_ide_act_desc_json(actId), cache_validate_func=lambda filepath: os.path.isfile(filepath), ) if not os.path.exists(act_cache_file): return "" with open(act_cache_file, encoding="utf-8") as f: return f.read()
def load_file_with_cache(name, reader, f): return with_cache(os.path.join(cache_dir, name + '.pickle'), lambda: reader(os.path.join(data_file_dir, f)))
def generate_with_cache(name, g): return with_cache(os.path.join(cache_dir, name + '.pickle'), g)