python-memcached icon indicating copy to clipboard operation
python-memcached copied to clipboard

Add support for default value in get()

Open samupl opened this issue 6 years ago • 3 comments

I opened a PR that adds optional support for default keyword argument in get() method: https://github.com/linsomniac/python-memcached/pull/158

This is due to a bug I've found in one of the projects I work for, that's Django based. We've noticed that using django's MemcachedCache backend class makes it impossible to save None into the cache without the cache framework thinking that no key is available. E.g.:

With django-redis RedisCache:

>>> cache.set('key', None, 10000)
True
>>> cache.get('key', default=123)
None

With django's MemcachedCache (issue is seen here, get() should return None):

>>> from django.core.cache.backends.memcached import MemcachedCache
>>> mc1 = MemcachedCache('127.0.0.1', {})
>>> mc1.set('key', None, timeout=3600)
>>> mc1.get('key', default=123)
123

PylibMC (an alternative to python-memcached) supports the default param correctly (although it's Django backend is also broken). If this issue gets fixed, I'll push a PR to django to use the new capabilities correctly.

The PR I mentioned adds support for the default keywords and makes it possible for someone to retrieve None from the backend knowing if he set it there explicitly, or if the key is simply missing.

samupl avatar Feb 12 '19 11:02 samupl

I also took the opportunity and fixed the linter issues travis was reporting.

samupl avatar Feb 12 '19 12:02 samupl

Here's the issue in Django, which this PR originated from. As you can see fixing this here would mean that all natively supported memcached backends at Django will be consistent with all other backends, which would be a nice improvement.

https://code.djangoproject.com/ticket/30181

samupl avatar Feb 13 '19 06:02 samupl

@samupl @alex @bartTC One of my django PR(django/django#11812) is pending which is dependent on this PR. Are we planning to add this feature in memcache?

ahsanshafiq742 avatar Sep 25 '19 10:09 ahsanshafiq742