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

Discrepancy between pylibmc and python-memcached in storing bools #245

Open
hyperair opened this issue Nov 12, 2018 · 6 comments
Open

Discrepancy between pylibmc and python-memcached in storing bools #245

hyperair opened this issue Nov 12, 2018 · 6 comments

Comments

@hyperair
Copy link

While comparing python-memcached and pylibmc behaviours, I found that python-memcached was able to store and retrieve bool values as bools, while pylibmc would store them as integers being 1 or 0. It looks like python-memcached pickles bools but pylibmc stores them as integers.

@lericson
Copy link
Owner

lericson commented Nov 12, 2018 via email

@hyperair
Copy link
Author

hyperair commented Nov 14, 2018 via email

@lericson
Copy link
Owner

lericson commented Nov 14, 2018 via email

@hyperair
Copy link
Author

hyperair commented Nov 17, 2018 via email

@sergeief
Copy link

sergeief commented Apr 19, 2021

We've encountered this after switching from python-memcached. In our case, uncached value returns as True and cached as 1, which caused a bug in the code. Is there workaround available?

@hyperair
Copy link
Author

hyperair commented Apr 21, 2021

You can subclass pylibmc.Client and override the serialize method to match python-memcached's behaviour

import pickle
import pylibmc


class Client(pylibmc.Client):
    def serialize(self, value):
        """
        Overridden serialize method. See pylibmc.client.Client.serialize for
        more info
        """

        # Ensure that bools are pickled to match python-memcached behaviour.
        # Remove this when https://github.com/lericson/pylibmc/issues/245 is
        # fixed.
        if isinstance(value, bool):
            return pickle.dumps(value), 1
        else:
            return super().serialize(value)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants