Skip to content

Commit b61371a

Browse files
authored
fix: list objects are now uncachable (a-luna#36)
1 parent a5130ca commit b61371a

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/fastapi_redis_cache/client.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ def requested_resource_not_modified(self, request: Request, cached_data: str) ->
110110
return self.get_etag(cached_data) in check_etags
111111

112112
def add_to_cache(self, key: str, value: Dict, expire: int) -> bool:
113-
if not isinstance(value, dict): # pragma: no cover
114-
if self.hasmethod(value, 'dict'):
115-
value = value.dict()
116-
else:
117-
message = f"Object of type {type(value)} is not JSON-serializable"
118-
self.log(RedisEvent.FAILED_TO_CACHE_KEY, msg=message, key=key)
119-
return False
120-
cached = self.redis.set(name=key, value=serialize_json(value), ex=expire)
113+
response_data = None
114+
try:
115+
response_data = serialize_json(value)
116+
except TypeError:
117+
message = f"Object of type {type(value)} is not JSON-serializable"
118+
self.log(RedisEvent.FAILED_TO_CACHE_KEY, msg=message, key=key)
119+
return False
120+
cached = self.redis.set(name=key, value=response_data, ex=expire)
121121
if cached:
122122
self.log(RedisEvent.KEY_ADDED_TO_CACHE, key=key)
123123
else: # pragma: no cover
@@ -163,4 +163,4 @@ def get_log_time():
163163
def hasmethod(obj, method_name):
164164
"""Return True if obj.method_name exists and is callable. Otherwise, return False."""
165165
obj_method = getattr(obj, method_name, None)
166-
return callable(obj_method) if obj_method else False
166+
return callable(obj_method) if obj_method else False

0 commit comments

Comments
 (0)