mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-25 04:57:54 +00:00
first commit
This commit is contained in:
33
fastapi_cache/coder.py
Normal file
33
fastapi_cache/coder.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import json
|
||||
import pickle # nosec:B403
|
||||
from typing import Any
|
||||
|
||||
|
||||
class Coder:
|
||||
@classmethod
|
||||
def encode(cls, value: Any):
|
||||
raise NotImplementedError
|
||||
|
||||
@classmethod
|
||||
def decode(cls, value: Any):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class JsonCoder(Coder):
|
||||
@classmethod
|
||||
def encode(cls, value: Any):
|
||||
return json.dumps(value)
|
||||
|
||||
@classmethod
|
||||
def decode(cls, value: Any):
|
||||
return json.loads(value)
|
||||
|
||||
|
||||
class PickleCoder(Coder):
|
||||
@classmethod
|
||||
def encode(cls, value: Any):
|
||||
return pickle.dumps(value)
|
||||
|
||||
@classmethod
|
||||
def decode(cls, value: Any):
|
||||
return pickle.loads(value) # nosec:B403
|
||||
Reference in New Issue
Block a user