|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 |
| -from typing import TYPE_CHECKING |
| 3 | +from typing import TYPE_CHECKING, Callable |
4 | 4 |
|
5 |
| -from virtualenv.app_data.na import AppDataDisabled |
6 | 5 | from virtualenv.cache import Cache
|
7 | 6 |
|
8 | 7 | if TYPE_CHECKING:
|
9 | 8 | from pathlib import Path
|
10 | 9 |
|
11 |
| - from virtualenv.app_data.base import AppData |
| 10 | + from virtualenv.app_data.base import ContentStore |
12 | 11 |
|
13 | 12 |
|
14 | 13 | class FileCache(Cache):
|
15 |
| - def __init__(self, app_data: AppData) -> None: |
16 |
| - self.app_data = app_data if app_data is not None else AppDataDisabled() |
| 14 | + def __init__(self, store_factory: Callable[[Path], ContentStore], clearer: Callable[[], None] | None) -> None: |
| 15 | + self.store_factory = store_factory |
| 16 | + self.clearer = clearer |
17 | 17 |
|
18 | 18 | def get(self, key: Path) -> dict | None:
|
19 | 19 | """Get a value from the file cache."""
|
20 |
| - py_info, py_info_store = None, self.app_data.py_info(key) |
21 |
| - with py_info_store.locked(): |
22 |
| - if py_info_store.exists(): |
23 |
| - py_info = py_info_store.read() |
24 |
| - return py_info |
| 20 | + result, store = None, self.store_factory(key) |
| 21 | + with store.locked(): |
| 22 | + if store.exists(): |
| 23 | + result = store.read() |
| 24 | + return result |
25 | 25 |
|
26 | 26 | def set(self, key: Path, value: dict) -> None:
|
27 | 27 | """Set a value in the file cache."""
|
28 |
| - py_info_store = self.app_data.py_info(key) |
29 |
| - with py_info_store.locked(): |
30 |
| - py_info_store.write(value) |
| 28 | + store = self.store_factory(key) |
| 29 | + with store.locked(): |
| 30 | + store.write(value) |
31 | 31 |
|
32 | 32 | def remove(self, key: Path) -> None:
|
33 | 33 | """Remove a value from the file cache."""
|
34 |
| - py_info_store = self.app_data.py_info(key) |
35 |
| - with py_info_store.locked(): |
36 |
| - if py_info_store.exists(): |
37 |
| - py_info_store.remove() |
| 34 | + store = self.store_factory(key) |
| 35 | + with store.locked(): |
| 36 | + if store.exists(): |
| 37 | + store.remove() |
38 | 38 |
|
39 | 39 | def clear(self) -> None:
|
40 | 40 | """Clear the entire file cache."""
|
41 |
| - self.app_data.py_info_clear() |
| 41 | + if self.clearer is not None: |
| 42 | + self.clearer() |
42 | 43 |
|
43 | 44 |
|
44 | 45 | __all__ = [
|
|
0 commit comments