Skip to content

Commit 89cd662

Browse files
committed
feature(http): use caching control to improve HTTP requests performance
1 parent 905dee5 commit 89cd662

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

mkdocs_rss_plugin/util.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
# 3rd party
2020
import markdown
2121
import urllib3
22+
from cachecontrol import CacheControl
23+
from cachecontrol.caches.file_cache import SeparateBodyFileCache
2224
from git import (
2325
GitCommandError,
2426
GitCommandNotFound,
@@ -34,7 +36,11 @@
3436
from requests.exceptions import ConnectionError, HTTPError
3537

3638
# 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+
)
3844
from mkdocs_rss_plugin.git_manager.ci import CiHandler
3945
from mkdocs_rss_plugin.integrations.theme_material_social_plugin import (
4046
IntegrationMaterialSocialCards,
@@ -67,6 +73,7 @@ class Util:
6773
def __init__(
6874
self,
6975
path: str = ".",
76+
cache_dir: Path = DEFAULT_CACHE_FOLDER,
7077
use_git: bool = True,
7178
integration_material_social_cards: Optional[
7279
IntegrationMaterialSocialCards
@@ -122,8 +129,13 @@ def __init__(
122129
self.social_cards = integration_material_social_cards
123130

124131
# 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+
)
127139

128140
def build_url(
129141
self, base_url: str, path: str, args_dict: Optional[dict] = None

requirements/base.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Common requirements
22
# -----------------------
33

4-
4+
cachecontrol[filecache] >=0.14,<1
55
GitPython>=3.1,<3.2
66
mkdocs>=1.5,<2
77
requests>=2.31,<3

0 commit comments

Comments
 (0)