Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing serializer as a class parameter. #136

Open
g-abello opened this issue Apr 28, 2022 · 3 comments
Open

Allow passing serializer as a class parameter. #136

g-abello opened this issue Apr 28, 2022 · 3 comments
Assignees
Labels
enhancement New feature or request feature request Request a new feature

Comments

@g-abello
Copy link

Following the discussion in #11 and the improvement done in #63, where custom serializers were added for each cache backend. I think that it would be very nice to have a few generic serializers (Pickle, JSON, etc) that could be passed as a parameter when initialising the cache backends.

Something that would look like:

from cachelib import FileSystemCache, RedisCache
from cachelib.serializers import JsonSerializer, PickleSerializer

redis_cache = RedisCache(serializer=JsonSerializer)
file_cache = FileSystemCache(serializer=PickleSerializer)

Each backend cache would obviously have a default serializer for backwards compatibility.

This would allow using more secure serialization alternatives than Pickle.
The ultimate goal that I would like to achieve would be to be able to use a custom serializer with the Flask-Caching library.

I could try to work out a solution for this and submit a PR if you think this approach would make sense.

@northernSage
Copy link
Member

Hey @g-abello, thanks for considering contributing to cachelib!

Our architecture currently allows users to use their own serializers by just inhering from cache classes and overriding the serializer class variable, so it's already not only possible to use alternatives safer than pickle (which is currently our default serializer for all cache types) but also further customize the behavior of cache classes.

From my understanding, not considering what we already have, what you propose would bring:

  1. New generic serializers
  2. Allow integration with flask-caching.

On 1: Interesting to have for the most common use cases like JSON, so I would be more than willing to review a PR for that.
On 2: How does our current architecture makes it harder to use custom serializers with flask-caching? Why would it be better to pass serializers as initialization parameters? It would be great if you could elaborate a little bit on that :bowtie:

Once again, thanks! 👍🏻

@northernSage northernSage added enhancement New feature or request feature request Request a new feature labels Apr 30, 2022
@g-abello
Copy link
Author

g-abello commented May 6, 2022

Hey @northernSage, thanks for your quick reply!

OK, so maybe I'm missing some information here (in that case, please tell me! 😄 ), and this might be a question more related to flask-caching than to cachelib really, but in flask-caching you can pick the backend that you want to use very easily by using the config, like:

config = {
    "CACHE_TYPE": "RedisCache",
    "CACHE_DEFAULT_TIMEOUT": 300,
    "CACHE_KEY_PREFIX": "my_cache:",
}

What I'm suggesting is to be able to do the same with the serialiser:

config = {
    ...
    "CACHE_SERIALIZER": "JSON",
    ...
}

That's why I think that having generic serializers and being able to pass them when initialising the cache classes would help in order to achieve that.

Does that make sense? Or is there any other way to achieve that right now?

Thanks!

@northernSage
Copy link
Member

@g-abello Fair enough. I think we could go for a initial implementation and further discuss it from there (if necessary). When you find the time, feel free to go for it. I will be happy to help as I can, just reach out 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature request Request a new feature
Development

No branches or pull requests

2 participants