Skip to content

Commit 31cf271

Browse files
committed
add ability to disable HTTP cache
1 parent 47a3dfc commit 31cf271

File tree

6 files changed

+50
-14
lines changed

6 files changed

+50
-14
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,14 @@ locations by modifying the respective environment variables:
223223
* ``FINAGG_DATABASE_URL`` points to the **finagg** data storage. Defaults to
224224
``./findata/finagg.sqlite``.
225225

226+
## Other
227+
228+
You can change some **finagg** behavior with other environment variables:
229+
230+
* ``FINAGG_DISABLE_HTTP_CACHE``: Set this to ``"1"`` or ``"True"`` to disable the
231+
HTTP requests cache. Instead of a cachable session, a default, uncached user
232+
session will be used for all requests.
233+
226234
# Dependencies
227235

228236
* [pandas][11] for fast, flexible, and expressive representations of relational data.

docs/configuration.rst

+9
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,14 @@ locations by modifying the respective environment variables:
4040
* ``FINAGG_DATABASE_URL`` points to the **finagg** data storage. Defaults to
4141
``./findata/finagg.sqlite``.
4242

43+
Other
44+
-----
45+
46+
You can change some **finagg** behavior with other environment variables:
47+
48+
* ``FINAGG_DISABLE_HTTP_CACHE``: Set this to ``"1"`` or ``"True"`` to disable the
49+
HTTP requests cache. Instead of a cachable session, a default, uncached user
50+
session will be used for all requests.
51+
4352
.. _`BEA API site`: https://apps.bea.gov/API/#/
4453
.. _`FRED API site`: https://fredaccount.stlouisfed.org/#/secure/

src/finagg/backend.py

+10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@
2626
:meta hide-value:
2727
"""
2828

29+
disable_http_cache = os.environ.get("FINAGG_DISABLE_HTTP_CACHE", "false").lower() in {
30+
"1",
31+
"true",
32+
}
33+
"""Whether the disable the HTTP requests cache. Instead of a cachable session,
34+
a default, uncached user session will be used for all requests.
35+
36+
:meta hide-value:
37+
"""
38+
2939
http_cache_path = pathlib.Path(
3040
os.environ.get("FINAGG_HTTP_CACHE_PATH", root_path / "findata" / "http_cache")
3141
)

src/finagg/bea/api.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,14 @@
5151
)
5252
logger = logging.getLogger(__name__)
5353

54-
session = requests_cache.CachedSession(
55-
str(backend.http_cache_path),
56-
ignored_parameters=["ResultFormat"],
57-
expire_after=timedelta(days=1),
58-
)
54+
if backend.disable_http_cache:
55+
session = requests.Session()
56+
else:
57+
session = requests_cache.CachedSession(
58+
str(backend.http_cache_path),
59+
ignored_parameters=["ResultFormat"],
60+
expire_after=timedelta(days=1),
61+
)
5962

6063
_YEAR = int | str
6164

src/finagg/fred/api/_api.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212

1313
from ... import backend, ratelimit
1414

15-
session = requests_cache.CachedSession(
16-
str(backend.http_cache_path),
17-
ignored_parameters=["api_key", "file_type"],
18-
expire_after=timedelta(weeks=1),
19-
)
15+
if backend.disable_http_cache:
16+
session = requests.Session()
17+
else:
18+
session = requests_cache.CachedSession(
19+
str(backend.http_cache_path),
20+
ignored_parameters=["api_key", "file_type"],
21+
expire_after=timedelta(weeks=1),
22+
)
2023

2124

2225
class API(ABC):

src/finagg/sec/api.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,13 @@
4444
)
4545
logger = logging.getLogger(__name__)
4646

47-
session = requests_cache.CachedSession(
48-
str(backend.http_cache_path),
49-
expire_after=timedelta(weeks=1),
50-
)
47+
if backend.disable_http_cache:
48+
session = requests.Session()
49+
else:
50+
session = requests_cache.CachedSession(
51+
str(backend.http_cache_path),
52+
expire_after=timedelta(weeks=1),
53+
)
5154

5255

5356
class Concept(TypedDict):

0 commit comments

Comments
 (0)