mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-24 20:47:54 +00:00
Add enable param to init
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
### 0.1.7
|
||||
|
||||
- Fix default json coder for datetime.
|
||||
- Add `enable` param to `init`.
|
||||
|
||||
### 0.1.6
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ class FastAPICache:
|
||||
_init = False
|
||||
_coder = None
|
||||
_key_builder = None
|
||||
_enable = True
|
||||
|
||||
@classmethod
|
||||
def init(
|
||||
@@ -20,6 +21,7 @@ class FastAPICache:
|
||||
expire: int = None,
|
||||
coder: Coder = JsonCoder,
|
||||
key_builder: Callable = default_key_builder,
|
||||
enable: bool = True,
|
||||
):
|
||||
if cls._init:
|
||||
return
|
||||
@@ -29,6 +31,7 @@ class FastAPICache:
|
||||
cls._expire = expire
|
||||
cls._coder = coder
|
||||
cls._key_builder = key_builder
|
||||
cls._enable = enable
|
||||
|
||||
@classmethod
|
||||
def get_backend(cls):
|
||||
@@ -51,6 +54,10 @@ class FastAPICache:
|
||||
def get_key_builder(cls):
|
||||
return cls._key_builder
|
||||
|
||||
@classmethod
|
||||
def get_enable(cls):
|
||||
return cls._enable
|
||||
|
||||
@classmethod
|
||||
async def clear(cls, namespace: str = None, key: str = None):
|
||||
namespace = cls._prefix + ":" + namespace if namespace else None
|
||||
|
||||
@@ -29,7 +29,9 @@ def cache(
|
||||
copy_kwargs = kwargs.copy()
|
||||
request = copy_kwargs.pop("request", None)
|
||||
response = copy_kwargs.pop("response", None)
|
||||
if request and request.headers.get("Cache-Control") == "no-store":
|
||||
if (
|
||||
request and request.headers.get("Cache-Control") == "no-store"
|
||||
) or not FastAPICache.get_enable():
|
||||
return await func(*args, **kwargs)
|
||||
|
||||
coder = coder or FastAPICache.get_coder()
|
||||
|
||||
Reference in New Issue
Block a user