feat: allow passing serializer on cache creation and implement JSONSerializer#462
feat: allow passing serializer on cache creation and implement JSONSerializer#462john0isaac wants to merge 1 commit into
Conversation
f5aa5ad to
7f65e69
Compare
| cache_item = self._get_item(self.key_prefix + key) | ||
| if cache_item: | ||
| response = cache_item[RESPONSE_FIELD] | ||
| value = self.serializer.loads(response) |
There was a problem hiding this comment.
the JSON serializer will be broken for dynamodb since this unpacking was happening inside the serializer .loads function I moved it here since it makes more sense and will allow other serializers to be used without special handling.
This change doesn't affect end user in anything while allowing for using other serializers.
| This cache doesn't have a serializer since the underlying memcached client | ||
| libraries handle serialization internally." |
There was a problem hiding this comment.
We could implement this but it's not simple so I left it as is.
| except ImportError as err: | ||
| raise RuntimeError("no pymongo module found") from err |
There was a problem hiding this comment.
this is the only cache backend that doesn't raise the error like this.
Also pymongo was unbound when used after these lines since on error we log and continue instead of raising.
|
|
||
| if client is None or isinstance(client, str): | ||
| client = pymongo.MongoClient(host=client) | ||
| client = pymongo.MongoClient(host=client, **kwargs) |
There was a problem hiding this comment.
**kwargs was there but not used for some reason.
| logging.warning( | ||
| f"An exception has been raised during a pickling operation: {e}" | ||
| ) | ||
| def _warn(self, e: Exception) -> None: |
There was a problem hiding this comment.
broaden exception to allow for pickle and json errors handling without getting type errors
| class MongoDbSerializer(BaseSerializer): | ||
| """Default serializer for MongoDbCache.""" | ||
|
|
||
| pass |
There was a problem hiding this comment.
this was not implemented for some reason
| ) -> _t.Optional[_t.Literal[True]]: | ||
| result = self._uwsgi.cache_update( | ||
| key, | ||
| self.serializer.dumps(value), | ||
| self._normalize_timeout(timeout), | ||
| self.cache, | ||
| ) # type: bool | ||
| ) # type: _t.Optional[_t.Literal[True]] |
There was a problem hiding this comment.
I looked into the code of uWSGI lib. the type is _TrueOrNone which is a type alias to Literal[True] | None
7f65e69 to
dd74f05
Compare
Allows people to pass the serializers when creating an instance of the class while maintaining a default to pickle serializer if no value is passed.
Adds an implementation of a JSON serializer that only handles the types handled by the
jsonlibrarycloses #136 and related to pallets-eco/flask-caching#209
It should simplify the implementation of pallets-eco/flask-caching#209 as backends will accept them as inputs now without rewriting the backend.