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

add compatibility with django 1.6 #13

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
16c3522
Obtain the memcache timeout after adding the herd timeout so values n…
niran Nov 3, 2010
26df38b
Remove incr and decr functions to fallback to BaseCache's naive imple…
niran Nov 17, 2010
e43330a
Added optional version param to the CacheClass's get() method to fix
May 2, 2011
b5254f8
Allow arbitrary kwargs in cache methods so they can be more future-proof
niran Sep 15, 2011
6a1f4ea
Changed version number for setup.py
joehillen Oct 11, 2011
143de7c
Merge pull request #1 from joehillen/master
dlrust Oct 11, 2011
6afd04d
Added support for pylibmc compression à la jbalogh/django-pylibmc.
joshourisman Feb 21, 2012
d23c001
Fixed mismatched variable name; changed to CACHE_MIN_COMPRESS for con…
joshourisman Feb 21, 2012
02e46b5
pylibmc is imported as memcache
joshourisman Feb 21, 2012
a840e4f
Only pass compression argument if using_pylibmc is True.
joshourisman Feb 21, 2012
c767f1d
Add client support for username and password
grillermo Jun 19, 2012
1eb0961
HOTFIX: Support lists of servers.
krisb78 Jul 15, 2013
7e110c7
Merge pull request #1 from greenmangaming/staging
krisb78 Jul 15, 2013
362cc06
Deprecated django.utils.hashcompat.sha_constructor removal
svartalf Oct 31, 2013
df9f45b
Upped version.
joshourisman Feb 9, 2014
95690f8
Merge branch 'release/0.2.5'
joshourisman Feb 9, 2014
476a692
Merge branch 'release/0.2.5' into develop
joshourisman Feb 9, 2014
25d0a53
Django doesn't include hashcompat anymore.
joshourisman Feb 9, 2014
b11e544
Upped version.
joshourisman Feb 9, 2014
d431510
Merge branch 'release/0.2.6'
joshourisman Feb 9, 2014
450b068
add compatibility with django 1.6
dicos Apr 6, 2016
87c744d
compability with django=1.6
dicos May 6, 2016
4808345
Merge branch 'master' of https://github.com/greenmangaming/django-new…
dicos May 6, 2016
247d2ef
Merge branch 'master' of https://github.com/grillermo/django-newcache
dicos May 6, 2016
5b33d75
Merge branch 'master' of https://github.com/joshourisman/django-newcache
dicos May 6, 2016
b8a2ac8
Merge branch 'master' of https://github.com/texastribune/django-newcache
dicos May 6, 2016
169f918
Merge branch 'master' of https://github.com/svartalf/django-newcache
dicos May 6, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 68 additions & 43 deletions newcache.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
"Modified memcached cache backend"

import time
import os
import hashlib

from threading import local

from django.core.cache.backends.base import BaseCache, InvalidCacheBackendError
from django.utils.hashcompat import sha_constructor
from django.utils import six

from django.utils.encoding import smart_str
from django.conf import settings

Expand All @@ -24,7 +27,7 @@
import memcache
NotFoundError = ValueError
except ImportError:
raise InvalidCacheBackendError('Memcached cache backend requires ' +
raise InvalidCacheBackendError('Memcached cache backend requires ' +
'either the "pylibmc" or "memcache" library')

# Flavor is used amongst multiple apps to differentiate the "flavor" of the
Expand All @@ -36,28 +39,38 @@
CACHE_KEY_MODULE = getattr(settings, 'CACHE_KEY_MODULE', 'newcache')
CACHE_HERD_TIMEOUT = getattr(settings, 'CACHE_HERD_TIMEOUT', 60)

CACHE_MIN_COMPRESS = getattr(settings, 'PYLIBMC_MIN_COMPRESS_LEN', 0) # Disabled
if CACHE_MIN_COMPRESS > 0 and not memcache.support_compression:
CACHE_MIN_COMPRESS = 0

class Marker(object):
pass

MARKER = Marker()

def get_key(key):
def get_key(key, version=None):
"""
Returns a hashed, versioned, flavored version of the string that was input.
"""
hashed = sha_constructor(smart_str(key)).hexdigest()
return ''.join((FLAVOR, '-', CACHE_VERSION, '-', hashed))
hashed = hashlib.sha1(smart_str(key)).hexdigest()
return ''.join((FLAVOR, '-', CACHE_VERSION, '-', hashed, version if version is not None else ''))

key_func = importlib.import_module(CACHE_KEY_MODULE).get_key

class CacheClass(BaseCache):

def __init__(self, server, params):
def __init__(self, server, params, username=None, password=None):
super(CacheClass, self).__init__(params)
self._servers = server.split(';')
if isinstance(server, six.string_types):
self._servers = server.split(';')
else:
self._servers = server
self._use_binary = bool(params.get('binary'))
self._use_binary = bool(params.get('BINARY'))
self._username = os.environ.get('MEMCACHE_USERNAME', username)
self._password = os.environ.get('MEMCACHE_PASSWORD', password)
self._local = local()

@property
def _cache(self):
"""
Expand All @@ -66,17 +79,19 @@ def _cache(self):
client = getattr(self._local, 'client', None)
if client:
return client

# Use binary mode if it's both supported and requested
if using_pylibmc and self._use_binary:
client = memcache.Client(self._servers, binary=True)
client = memcache.Client(self._servers, binary=True,
username=self._username, password=self._password)
else:
client = memcache.Client(self._servers)
client = memcache.Client(self._servers,
username=self._username, password=self._password)

# If we're using pylibmc, set the behaviors according to settings
if using_pylibmc:
client.behaviors = CACHE_BEHAVIORS

self._local.client = client
return client

Expand All @@ -87,7 +102,7 @@ def _pack_value(self, value, timeout):
"""
herd_timeout = (timeout or self.default_timeout) + int(time.time())
return (MARKER, value, herd_timeout)

def _unpack_value(self, value, default=None):
"""
Unpacks a value and returns a tuple whose first element is the value,
Expand Down Expand Up @@ -120,92 +135,102 @@ def _get_memcache_timeout(self, timeout):
timeout += int(time.time())
return timeout

def add(self, key, value, timeout=None, herd=True):
def add(self, key, value, timeout=None, herd=True, version=None, **kwargs):
# If the user chooses to use the herd mechanism, then encode some
# timestamp information into the object to be persisted into memcached
if herd and timeout != 0:
if timeout is None:
timeout = self.default_timeout
packed = self._pack_value(value, timeout)
real_timeout = (self._get_memcache_timeout(timeout) +
real_timeout = self._get_memcache_timeout(timeout +
CACHE_HERD_TIMEOUT)
else:
packed = value
real_timeout = self._get_memcache_timeout(timeout)
return self._cache.add(key_func(key), packed, real_timeout)
args = [key_func(key, version), packed, real_timeout]
if using_pylibmc is True:
args.append(CACHE_MIN_COMPRESS)
return self._cache.add(*args)

def get(self, key, default=None):
encoded_key = key_func(key)
def get(self, key, default=None, version=None, **kwargs):
encoded_key = key_func(key, version)
packed = self._cache.get(encoded_key)
if packed is None:
return default

val, refresh = self._unpack_value(packed)

# If the cache has expired according to the embedded timeout, then
# shove it back into the cache for a while, but act as if it was a
# cache miss.
if refresh:
self._cache.set(encoded_key, val,
self._get_memcache_timeout(CACHE_HERD_TIMEOUT))
return default

return val

def set(self, key, value, timeout=None, herd=True):
def set(self, key, value, timeout=None, herd=True, version=None, **kwargs):
# If the user chooses to use the herd mechanism, then encode some
# timestamp information into the object to be persisted into memcached
if herd and timeout != 0:
if timeout is None:
timeout = self.default_timeout
packed = self._pack_value(value, timeout)
real_timeout = (self._get_memcache_timeout(timeout) +
real_timeout = self._get_memcache_timeout(timeout +
CACHE_HERD_TIMEOUT)
else:
packed = value
real_timeout = self._get_memcache_timeout(timeout)
return self._cache.set(key_func(key), packed, real_timeout)
args = [key_func(key, version), packed, real_timeout]
if using_pylibmc is True:
args.append(CACHE_MIN_COMPRESS)
return self._cache.set(*args)

def delete(self, key):
self._cache.delete(key_func(key))
def delete(self, key, version=None, **kwargs):
self._cache.delete(key_func(key, version))

def get_many(self, keys):
def get_many(self, keys, version=None, **kwargs):
# First, map all of the keys through our key function
rvals = map(key_func, keys)
rvals = [key_func(k, version) for k in keys]

packed_resp = self._cache.get_multi(rvals)

resp = {}
reinsert = {}

for key, packed in packed_resp.iteritems():
# If it was a miss, treat it as a miss to our response & continue
if packed is None:
resp[key] = packed
continue

val, refresh = self._unpack_value(packed)
if refresh:
reinsert[key] = val
resp[key] = None
else:
resp[key] = val

# If there are values to re-insert for a short period of time, then do
# so now.
if reinsert:
self._cache.set_multi(reinsert,
self._get_memcache_timeout(CACHE_HERD_TIMEOUT))

# Build a reverse map of encoded keys to the original keys, so that
# the returned dict's keys are what users expect (in that they match
# what the user originally entered)
reverse = dict(zip(rvals, keys))

return dict(((reverse[k], v) for k, v in resp.iteritems()))

def close(self, **kwargs):
self._cache.disconnect_all()

def incr(self, key, delta=1):
def incr(self, key, delta=1, version=None):
try:
return self._cache.incr(key_func(key), delta)
return self._cache.incr(key_func(key, version), delta)
except NotFoundError:
raise ValueError("Key '%s' not found" % (key,))

Expand All @@ -215,17 +240,17 @@ def decr(self, key, delta=1):
except NotFoundError:
raise ValueError("Key '%s' not found" % (key,))

def set_many(self, data, timeout=None, herd=True):
def set_many(self, data, timeout=None, herd=True, version=None, **kwargs):
if herd and timeout != 0:
safe_data = dict(((key_func(k), self._pack_value(v, timeout))
safe_data = dict(((key_func(k, version), self._pack_value(v, timeout))
for k, v in data.iteritems()))
else:
safe_data = dict((
(key_func(k), v) for k, v in data.iteritems()))
(key_func(k, version), v) for k, v in data.iteritems()))
self._cache.set_multi(safe_data, self._get_memcache_timeout(timeout))

def delete_many(self, keys):
self._cache.delete_multi(map(key_func, keys))
def delete_many(self, keys, version=None, **kwargs):
self._cache.delete_multi([key_func(k, version) for k in keys])

def clear(self):
self._cache.flush_all()
def clear(self, **kwargs):
self._cache.flush_all()
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from setuptools import setup

VERSION = '0.2.4'
VERSION = '0.2.6-1'

setup(
name='django-newcache',
Expand All @@ -25,4 +25,4 @@
],
zip_safe=False,
py_modules=['newcache'],
)
)