|
19 | 19 | # 3rd party
|
20 | 20 | import markdown
|
21 | 21 | import urllib3
|
| 22 | +from cachecontrol import CacheControl |
| 23 | +from cachecontrol.caches.file_cache import SeparateBodyFileCache |
22 | 24 | from git import (
|
23 | 25 | GitCommandError,
|
24 | 26 | GitCommandNotFound,
|
|
34 | 36 | from requests.exceptions import ConnectionError, HTTPError
|
35 | 37 |
|
36 | 38 | # package
|
37 |
| -from mkdocs_rss_plugin.constants import MKDOCS_LOGGER_NAME, REMOTE_REQUEST_HEADERS |
| 39 | +from mkdocs_rss_plugin.constants import ( |
| 40 | + DEFAULT_CACHE_FOLDER, |
| 41 | + MKDOCS_LOGGER_NAME, |
| 42 | + REMOTE_REQUEST_HEADERS, |
| 43 | +) |
38 | 44 | from mkdocs_rss_plugin.git_manager.ci import CiHandler
|
39 | 45 | from mkdocs_rss_plugin.integrations.theme_material_social_plugin import (
|
40 | 46 | IntegrationMaterialSocialCards,
|
@@ -67,6 +73,7 @@ class Util:
|
67 | 73 | def __init__(
|
68 | 74 | self,
|
69 | 75 | path: str = ".",
|
| 76 | + cache_dir: Path = DEFAULT_CACHE_FOLDER, |
70 | 77 | use_git: bool = True,
|
71 | 78 | integration_material_social_cards: Optional[
|
72 | 79 | IntegrationMaterialSocialCards
|
@@ -122,8 +129,13 @@ def __init__(
|
122 | 129 | self.social_cards = integration_material_social_cards
|
123 | 130 |
|
124 | 131 | # http/s session
|
125 |
| - self.req_session = Session() |
126 |
| - self.req_session.headers.update(REMOTE_REQUEST_HEADERS) |
| 132 | + session = Session() |
| 133 | + session.headers.update(REMOTE_REQUEST_HEADERS) |
| 134 | + self.req_session = CacheControl( |
| 135 | + sess=session, |
| 136 | + cache=SeparateBodyFileCache(directory=cache_dir), |
| 137 | + cacheable_methods=("GET", "HEAD"), |
| 138 | + ) |
127 | 139 |
|
128 | 140 | def build_url(
|
129 | 141 | self, base_url: str, path: str, args_dict: Optional[dict] = None
|
|
0 commit comments