Skip to content

Commit 71398c9

Browse files
committed
upgrade CacheControl to use the more memory-efficient SeparateBodyFileCache
Closes: #598
1 parent 63f9062 commit 71398c9

File tree

8 files changed

+39
-34
lines changed

8 files changed

+39
-34
lines changed

mypy-stubs/cachecontrol/cache.pyi

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
from datetime import datetime
2+
from typing import IO
3+
14
class BaseCache:
25
def get(self, key: str) -> bytes | None: ...
3-
def set(self, key: str, value: bytes, expires: int | None = None) -> None: ...
6+
def set(self, key: str, value: bytes, expires: int | datetime | None = None) -> None: ...
47
def delete(self, key: str) -> None: ...
58
def close(self) -> None: ...
9+
10+
class SeparateBodyBaseCache(BaseCache):
11+
def set_body(self, key: str, body: bytes) -> None: ...
12+
def get_body(self, key: str) -> IO[bytes] | None: ...
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
# Stubs for cachecontrol.caches (Python 2)
2-
#
3-
# NOTE: This dynamically typed stub was automatically generated by stubgen.
1+
from cachecontrol.caches.file_cache import SeparateBodyFileCache as SeparateBodyFileCache
42

5-
from typing import Any
6-
7-
from .file_cache import FileCache as FileCache
8-
9-
# from .redis_cache import RedisCache as RedisCache
10-
11-
notice = ... # type: Any
3+
__all__ = ["SeparateBodyFileCache"]
Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
1-
from os import PathLike
2-
from typing import ContextManager
3-
4-
from ..cache import BaseCache as BaseCache
5-
from ..controller import CacheController as CacheController
1+
from cachecontrol.cache import SeparateBodyBaseCache
2+
from datetime import datetime
3+
from filelock import BaseFileLock
4+
from pathlib import Path
5+
from typing import IO, ContextManager
66

77
class _LockClass:
88
path: str
99

1010
_lock_class = ContextManager[_LockClass]
1111

12-
class FileCache(BaseCache):
12+
class _FileCacheMixin:
1313
directory: str
1414
forever: bool
1515
filemode: int
1616
dirmode: int
1717
lock_class: _lock_class | None = None
1818
def __init__(
1919
self,
20-
directory: str | PathLike[str],
21-
forever: bool = ...,
22-
filemode: int = ...,
23-
dirmode: int = ...,
24-
use_dir_lock: bool | None = ...,
25-
lock_class: _lock_class | None = ...,
20+
directory: str | Path,
21+
forever: bool = False,
22+
filemode: int = 384,
23+
dirmode: int = 448,
24+
lock_class: type[BaseFileLock] | None = None,
2625
) -> None: ...
2726
@staticmethod
2827
def encode(x: str) -> str: ...
29-
def get(self, key: str) -> None | bytes: ...
30-
def set(self, key: str, value: bytes, expires: int | None = None) -> None: ...
28+
def get(self, key: str) -> bytes | None: ...
29+
def set(self, key: str, value: bytes, expires: int | datetime | None = None) -> None: ...
30+
31+
class SeparateBodyFileCache(_FileCacheMixin, SeparateBodyBaseCache):
32+
def get_body(self, key: str) -> IO[bytes] | None: ...
33+
def set_body(self, key: str, body: bytes) -> None: ...
3134
def delete(self, key: str) -> None: ...

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ requires = [
88
"types-dataclasses",
99
"importlib_resources>=1.4;python_version<'3.9'",
1010
"ruamel.yaml>=0.17.6, < 0.19",
11-
"types-setuptools"
11+
"types-setuptools",
12+
"CacheControl[filecache] >= 0.13.1, < 0.15"
1213
]
1314
build-backend = "setuptools.build_meta"
1415

schema_salad/metaschema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ def __init__(
139139
self.fetcher = copyfrom.fetcher
140140
else:
141141
import requests
142-
from cachecontrol.caches import FileCache
142+
from cachecontrol.caches import SeparateBodyFileCache
143143
from cachecontrol.wrapper import CacheControl
144144

145145
root = pathlib.Path(os.environ.get("HOME", tempfile.gettempdir()))
146146
session = CacheControl(
147147
requests.Session(),
148-
cache=FileCache(root / ".cache" / "salad"),
148+
cache=SeparateBodyFileCache(root / ".cache" / "salad"),
149149
)
150150
self.fetcher: Fetcher = DefaultFetcher({}, session)
151151

schema_salad/python_codegen_support.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,13 @@ def __init__(
136136
self.fetcher = copyfrom.fetcher
137137
else:
138138
import requests
139-
from cachecontrol.caches import FileCache
139+
from cachecontrol.caches import SeparateBodyFileCache
140140
from cachecontrol.wrapper import CacheControl
141141

142142
root = pathlib.Path(os.environ.get("HOME", tempfile.gettempdir()))
143143
session = CacheControl(
144144
requests.Session(),
145-
cache=FileCache(root / ".cache" / "salad"),
145+
cache=SeparateBodyFileCache(root / ".cache" / "salad"),
146146
)
147147
self.fetcher: Fetcher = DefaultFetcher({}, session)
148148

schema_salad/ref_resolver.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
)
2323

2424
import requests
25-
from cachecontrol.caches import FileCache
25+
from cachecontrol.caches import SeparateBodyFileCache
2626
from cachecontrol.wrapper import CacheControl
2727
from rdflib.exceptions import ParserError
2828
from rdflib.graph import Graph
@@ -179,10 +179,12 @@ def __init__(
179179
root = pathlib.Path(os.environ.get("HOME", tempfile.gettempdir()))
180180
self.session = CacheControl(
181181
requests.Session(),
182-
cache=FileCache(root / ".cache" / "salad"),
182+
cache=SeparateBodyFileCache(root / ".cache" / "salad"),
183183
)
184184
elif isinstance(doc_cache, str):
185-
self.session = CacheControl(requests.Session(), cache=FileCache(doc_cache))
185+
self.session = CacheControl(
186+
requests.Session(), cache=SeparateBodyFileCache(doc_cache)
187+
)
186188
else:
187189
self.session = session
188190

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
"ruamel.yaml >= 0.17.6, < 0.19",
8989
"rdflib >= 4.2.2, < 8.0.0",
9090
"mistune>=3,<3.1",
91-
"CacheControl[filecache] >= 0.11.7, < 0.15",
91+
"CacheControl[filecache] >= 0.13.1, < 0.15",
9292
"mypy_extensions",
9393
"importlib_resources>=1.4;python_version<'3.9'",
9494
]

0 commit comments

Comments
 (0)