|
9 | 9 | import numpy as np
|
10 | 10 |
|
11 | 11 | from auto_editor import version
|
12 |
| -from auto_editor.lang.json import Lexer, Parser, dump |
13 | 12 | from auto_editor.utils.subtitle_tools import convert_ass_to_text
|
14 | 13 | from auto_editor.wavfile import read
|
15 | 14 |
|
@@ -145,49 +144,29 @@ def all(self) -> NDArray[np.bool_]:
|
145 | 144 |
|
146 | 145 | def read_cache(self, tag: str, obj: dict[str, Any]) -> None | np.ndarray:
|
147 | 146 | workfile = os.path.join(
|
148 |
| - os.path.dirname(self.temp), f"ae-{version}", "cache.json" |
| 147 | + os.path.dirname(self.temp), f"ae-{version}", "cache.npz" |
149 | 148 | )
|
150 | 149 |
|
151 | 150 | try:
|
152 |
| - with open(workfile, encoding="utf-8") as file: |
153 |
| - cache = Parser(Lexer(workfile, file)).expr() |
154 |
| - except Exception: |
| 151 | + npzfile = np.load(workfile, allow_pickle=False) |
| 152 | + except Exception as e: |
| 153 | + self.log.debug(e) |
155 | 154 | return None
|
156 | 155 |
|
157 |
| - if f"{self.src.path.resolve()}" not in cache: |
| 156 | + key = f"{self.src.path}:{obj_tag(tag, self.tb, obj)}" |
| 157 | + if key not in npzfile.files: |
158 | 158 | return None
|
159 | 159 |
|
160 |
| - key = obj_tag(tag, self.tb, obj) |
161 |
| - |
162 |
| - if key not in (root := cache[f"{self.src.path.resolve()}"]): |
163 |
| - return None |
164 |
| - |
165 |
| - return np.asarray(root[key]["arr"], dtype=root[key]["type"]) |
| 160 | + self.log.debug("Using cache") |
| 161 | + return npzfile[key] |
166 | 162 |
|
167 | 163 | def cache(self, tag: str, obj: dict[str, Any], arr: np.ndarray) -> np.ndarray:
|
168 | 164 | workdur = os.path.join(os.path.dirname(self.temp), f"ae-{version}")
|
169 |
| - workfile = os.path.join(workdur, "cache.json") |
170 | 165 | if not os.path.exists(workdur):
|
171 | 166 | os.mkdir(workdur)
|
172 | 167 |
|
173 |
| - key = obj_tag(tag, self.tb, obj) |
174 |
| - |
175 |
| - try: |
176 |
| - with open(workfile, encoding="utf-8") as file: |
177 |
| - json_object = Parser(Lexer(workfile, file)).expr() |
178 |
| - except Exception: |
179 |
| - json_object = {} |
180 |
| - |
181 |
| - entry = {"type": str(arr.dtype), "arr": arr.tolist()} |
182 |
| - src_key = f"{self.src.path}" |
183 |
| - |
184 |
| - if src_key in json_object: |
185 |
| - json_object[src_key][key] = entry |
186 |
| - else: |
187 |
| - json_object[src_key] = {key: entry} |
188 |
| - |
189 |
| - with open(os.path.join(workdur, "cache.json"), "w", encoding="utf-8") as file: |
190 |
| - dump(json_object, file) |
| 168 | + tag = obj_tag(tag, self.tb, obj) |
| 169 | + np.savez(os.path.join(workdur, "cache.npz"), **{f"{self.src.path}:{tag}": arr}) |
191 | 170 |
|
192 | 171 | return arr
|
193 | 172 |
|
|
0 commit comments