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

Fixed #19810 -- MemcachedCache now uses pickle.HIGHEST_PROTOCOL #821

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions django/core/cache/backends/memcached.py
@@ -1,6 +1,7 @@
"Memcached cache backend"

import time
import pickle
from threading import local

from django.core.cache.backends.base import BaseCache, InvalidCacheBackendError
Expand Down Expand Up @@ -146,6 +147,12 @@ def __init__(self, server, params):
library=memcache,
value_not_found_exception=ValueError)

@property
def _cache(self):
if getattr(self, '_client', None) is None:
self._client = self._lib.Client(self._servers, pickleProtocol=pickle.HIGHEST_PROTOCOL)
return self._client

class PyLibMCCache(BaseMemcachedCache):
"An implementation of a cache binding using pylibmc"
def __init__(self, server, params):
Expand Down
13 changes: 13 additions & 0 deletions tests/regressiontests/cache/tests.py
Expand Up @@ -12,6 +12,7 @@
import tempfile
import time
import warnings
import pickle

from django.conf import settings
from django.core import management
Expand Down Expand Up @@ -977,6 +978,18 @@ def test_invalid_keys(self):
# memcached limits key length to 250
self.assertRaises(Exception, self.cache.set, 'a' * 251, 'value')

@unittest.skipUnless(
any(cache['BACKEND'] == 'django.core.cache.backends.memcached.MemcachedCache'
for cache in settings.CACHES.values()),
"cache with python-memcached library not available")
def test_memcached_uses_highest_pickle_version(self):
for cache_key, cache in settings.CACHES.items():
if cache['BACKEND'] == 'django.core.cache.backends.memcached.MemcachedCache':
break

cache = get_cache(cache_key)._cache # get client for library
self.assertEqual(cache.pickleProtocol, pickle.HIGHEST_PROTOCOL) # check library pickle version


class FileBasedCacheTests(unittest.TestCase, BaseCacheTests):
"""
Expand Down