Skip to content

Commit 9928f4c

Browse files
committed
Add enable param to init
1 parent 4faa5b7 commit 9928f4c

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### 0.1.7
66

77
- Fix default json coder for datetime.
8+
- Add `enable` param to `init`.
89

910
### 0.1.6
1011

fastapi_cache/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class FastAPICache:
1111
_init = False
1212
_coder = None
1313
_key_builder = None
14+
_enable = True
1415

1516
@classmethod
1617
def init(
@@ -20,6 +21,7 @@ def init(
2021
expire: int = None,
2122
coder: Coder = JsonCoder,
2223
key_builder: Callable = default_key_builder,
24+
enable: bool = True,
2325
):
2426
if cls._init:
2527
return
@@ -29,6 +31,7 @@ def init(
2931
cls._expire = expire
3032
cls._coder = coder
3133
cls._key_builder = key_builder
34+
cls._enable = enable
3235

3336
@classmethod
3437
def get_backend(cls):
@@ -51,6 +54,10 @@ def get_coder(cls):
5154
def get_key_builder(cls):
5255
return cls._key_builder
5356

57+
@classmethod
58+
def get_enable(cls):
59+
return cls._enable
60+
5461
@classmethod
5562
async def clear(cls, namespace: str = None, key: str = None):
5663
namespace = cls._prefix + ":" + namespace if namespace else None

fastapi_cache/decorator.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ async def inner(*args, **kwargs):
2929
copy_kwargs = kwargs.copy()
3030
request = copy_kwargs.pop("request", None)
3131
response = copy_kwargs.pop("response", None)
32-
if request and request.headers.get("Cache-Control") == "no-store":
32+
if (
33+
request and request.headers.get("Cache-Control") == "no-store"
34+
) or not FastAPICache.get_enable():
3335
return await func(*args, **kwargs)
3436

3537
coder = coder or FastAPICache.get_coder()

0 commit comments

Comments
 (0)