Skip to content

Commit

Permalink
add ability to disable HTTP cache
Browse files Browse the repository at this point in the history
  • Loading branch information
theOGognf committed Aug 9, 2024
1 parent 47a3dfc commit 31cf271
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 14 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@ locations by modifying the respective environment variables:
* ``FINAGG_DATABASE_URL`` points to the **finagg** data storage. Defaults to
``./findata/finagg.sqlite``.

## Other

You can change some **finagg** behavior with other environment variables:

* ``FINAGG_DISABLE_HTTP_CACHE``: Set this to ``"1"`` or ``"True"`` to disable the
HTTP requests cache. Instead of a cachable session, a default, uncached user
session will be used for all requests.

# Dependencies

* [pandas][11] for fast, flexible, and expressive representations of relational data.
Expand Down
9 changes: 9 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,14 @@ locations by modifying the respective environment variables:
* ``FINAGG_DATABASE_URL`` points to the **finagg** data storage. Defaults to
``./findata/finagg.sqlite``.

Other
-----

You can change some **finagg** behavior with other environment variables:

* ``FINAGG_DISABLE_HTTP_CACHE``: Set this to ``"1"`` or ``"True"`` to disable the
HTTP requests cache. Instead of a cachable session, a default, uncached user
session will be used for all requests.

.. _`BEA API site`: https://apps.bea.gov/API/signup/
.. _`FRED API site`: https://fredaccount.stlouisfed.org/login/secure/
10 changes: 10 additions & 0 deletions src/finagg/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@
:meta hide-value:
"""

disable_http_cache = os.environ.get("FINAGG_DISABLE_HTTP_CACHE", "false").lower() in {
"1",
"true",
}
"""Whether the disable the HTTP requests cache. Instead of a cachable session,
a default, uncached user session will be used for all requests.
:meta hide-value:
"""

http_cache_path = pathlib.Path(
os.environ.get("FINAGG_HTTP_CACHE_PATH", root_path / "findata" / "http_cache")
)
Expand Down
13 changes: 8 additions & 5 deletions src/finagg/bea/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@
)
logger = logging.getLogger(__name__)

session = requests_cache.CachedSession(
str(backend.http_cache_path),
ignored_parameters=["ResultFormat"],
expire_after=timedelta(days=1),
)
if backend.disable_http_cache:
session = requests.Session()
else:
session = requests_cache.CachedSession(
str(backend.http_cache_path),
ignored_parameters=["ResultFormat"],
expire_after=timedelta(days=1),
)

_YEAR = int | str

Expand Down
13 changes: 8 additions & 5 deletions src/finagg/fred/api/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@

from ... import backend, ratelimit

session = requests_cache.CachedSession(
str(backend.http_cache_path),
ignored_parameters=["api_key", "file_type"],
expire_after=timedelta(weeks=1),
)
if backend.disable_http_cache:
session = requests.Session()
else:
session = requests_cache.CachedSession(
str(backend.http_cache_path),
ignored_parameters=["api_key", "file_type"],
expire_after=timedelta(weeks=1),
)


class API(ABC):
Expand Down
11 changes: 7 additions & 4 deletions src/finagg/sec/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@
)
logger = logging.getLogger(__name__)

session = requests_cache.CachedSession(
str(backend.http_cache_path),
expire_after=timedelta(weeks=1),
)
if backend.disable_http_cache:
session = requests.Session()
else:
session = requests_cache.CachedSession(
str(backend.http_cache_path),
expire_after=timedelta(weeks=1),
)


class Concept(TypedDict):
Expand Down

0 comments on commit 31cf271

Please sign in to comment.