Skip to content

Commit 857d18b

Browse files
kumekayhfudev
authored andcommitted
feat: Add in-memory cache for API and storage requests
1 parent d9a4791 commit 857d18b

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
# IDF Component Manager
22

33
The IDF Component Manager is a tool that ensures the correct versions of all components required for a successful build are present in your [ESP-IDF](https://www.espressif.com/en/products/sdks/esp-idf) project.
4+
45
- The Component Manager downloads the dependencies for your project automatically during a `CMake` run.
56
- The components can be sourced either from [the ESP Component Registry](https://components.espressif.com/) or from a Git repository.
67
- A list of components can be found at https://components.espressif.com/
78

89
## Contributing to the project
10+
911
See [CONTRIBUTING.md](CONTRIBUTING.md)
1012

1113
## Resources
14+
1215
- [Offical Documentation at docs.espressif.com](https://docs.espressif.com/projects/idf-component-manager/en/latest/)
1316
- The Python Package Index project page https://pypi.org/project/idf-component-manager/
1417
- The Component Manager section in the [ESP-IDF Programming Guide](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/tools/idf-component-manager.html)
@@ -43,7 +46,6 @@ python -m pip uninstall -y idf-component-manager
4346
python -m pip install git+https://github.com/espressif/idf-component-manager.git@main
4447
```
4548

46-
4749
## Disabling the Component Manager
4850

4951
The Component Manager can be explicitly disabled by setting `IDF_COMPONENT_MANAGER` environment variable to `0`.
@@ -164,3 +166,4 @@ examples:
164166
| IDF_COMPONENT_SUPPRESS_UNKNOWN_FILE_WARNINGS | 0 | Ignore unknown files in managed_components directory |
165167
| IDF_COMPONENT_CHECK_NEW_VERSION | 1 | Check for new versions of components |
166168
| IDF_COMPONENT_VERIFY_SSL | 1 | Verify SSL certificates when making requests to the registry, set it 0 to disable or provide a CA bundle path |
169+
| IDF_COMPONENT_CACHE_HTTP_REQUESTS | 1 | Cache HTTP requests to the registry during runtime, set it 0 to disable |

idf_component_tools/environment.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class ComponentManagerSettings(BaseSettings):
109109
)
110110
API_TOKEN: t.Optional[str] = None
111111
VERIFY_SSL: t.Union[bool, str] = True
112+
CACHE_HTTP_REQUESTS: bool = True
112113
PROFILE: t.Optional[str] = Field(
113114
default=None,
114115
validation_alias=AliasChoices(

idf_component_tools/registry/base_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# SPDX-License-Identifier: Apache-2.0
33
import platform
44
import typing as t
5+
from functools import lru_cache
56

67
import requests
78
from requests.adapters import HTTPAdapter
@@ -24,6 +25,7 @@
2425
MAX_RETRIES = 3
2526

2627

28+
@lru_cache(maxsize=None)
2729
def create_session(
2830
token: t.Optional[str] = None,
2931
) -> requests.Session:

idf_component_tools/registry/request_processor.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
60.1, # Read timeout
2626
)
2727

28+
# Storage for caching requests
29+
_request_cache: t.Dict[t.Tuple[t.Any], Response] = {}
30+
2831

2932
def join_url(*args) -> str:
3033
"""
@@ -34,6 +37,23 @@ def join_url(*args) -> str:
3437
return '/'.join(parts)
3538

3639

40+
def cache_request(func):
41+
"""Decorator to conditionally cache function output based on CACHE_HTTP_REQUESTS"""
42+
43+
def wrapper(*args, **kwargs):
44+
if ComponentManagerSettings().CACHE_HTTP_REQUESTS:
45+
cache_key = (args, frozenset(kwargs.items()))
46+
if cache_key in _request_cache:
47+
return _request_cache[cache_key]
48+
result = func(*args, **kwargs)
49+
_request_cache[cache_key] = result
50+
return result
51+
return func(*args, **kwargs)
52+
53+
return wrapper
54+
55+
56+
@cache_request
3757
def make_request(
3858
method: str,
3959
session: requests.Session,

tests/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ def monkeypatch_idf_version_and_tools_path(monkeypatch, tmp_path):
3636
monkeypatch.setenv('IDF_TOOLS_PATH', str(tmp_path))
3737

3838

39+
@pytest.fixture(autouse=True)
40+
def monkeypatch_disable_request_cache(monkeypatch):
41+
monkeypatch.setenv('IDF_COMPONENT_CACHE_HTTP_REQUESTS', '0')
42+
43+
3944
@pytest.fixture()
4045
def valid_manifest():
4146
return {

0 commit comments

Comments
 (0)