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

Added support for SASL authentication in MemcachedCache, fixed FormParser bug, added support for python-binary-memcached #236

Closed
wants to merge 3 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions werkzeug/contrib/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,21 +296,24 @@ class MemcachedCache(BaseCache):
applications. Keep in mind that
:meth:`~BaseCache.clear` will also clear keys with a
different prefix.
:param username: a username for Memcache servers requiring SASL authentication
:param password: a password for Memcache servers requiring SASL authentication

"""

def __init__(self, servers=None, default_timeout=300, key_prefix=None):
def __init__(self, servers=None, default_timeout=300, key_prefix=None, username=None, password=None):

This comment was marked as off-topic.

BaseCache.__init__(self, default_timeout)
if servers is None or isinstance(servers, (list, tuple)):
if servers is None:
servers = ['127.0.0.1:11211']
self._client = self.import_preferred_memcache_lib(servers)
if self._client is None:
raise RuntimeError('no memcache module found')
if servers is None:
servers = '127.0.0.1:11211'
elif isinstance(servers, str):

This comment was marked as off-topic.

servers = [servers]
else:
# NOTE: servers is actually an already initialized memcache
# client.
self._client = servers

self._client = self.import_preferred_memcache_lib(servers, username, password)
if self._client is None:
raise RuntimeError('no memcache module found')

self.key_prefix = key_prefix

def get(self, key):
Expand Down Expand Up @@ -418,13 +421,16 @@ def dec(self, key, delta=1):
key = self.key_prefix + key
self._client.decr(key, delta)

def import_preferred_memcache_lib(self, servers):
def import_preferred_memcache_lib(self, servers, username, password):

This comment was marked as off-topic.

"""Returns an initialized memcache client. Used by the constructor."""

try:
import pylibmc
except ImportError:
pass
else:
if username and password:
return pylibmc.Client(servers, username, password, binary=True)
return pylibmc.Client(servers)

try:
Expand All @@ -441,6 +447,16 @@ def import_preferred_memcache_lib(self, servers):
else:
return memcache.Client(servers)

try:
import bmemcached
except ImportError:
pass
else:
if username and password:
client = bmemcached.Client(servers, username=username, password=password)
return client
return bmemcached.Client(servers)


# backwards compatibility
GAEMemcachedCache = MemcachedCache
Expand Down
11 changes: 11 additions & 0 deletions werkzeug/testsuite/contrib/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ def test_set_many(self):
c.set_many((i, i*i) for i in xrange(3))
assert c.get(2) == 4

class MemcachedCacheTestCase(WerkzeugTestCase):

def test_simple(self):
c = cache.MemcachedCache()
c.set('foo', 'bar')
self.assertEqual(c.get('foo'), 'bar')

def test_sasl_auth(self):
# hard to test this without a local SASL memcached installation
pass

class FileSystemCacheTestCase(WerkzeugTestCase):

Expand Down Expand Up @@ -166,6 +176,7 @@ def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(SimpleCacheTestCase))
suite.addTest(unittest.makeSuite(FileSystemCacheTestCase))
suite.addTest(unittest.makeSuite(MemcachedCacheTestCase))
if redis is not None:
suite.addTest(unittest.makeSuite(RedisCacheTestCase))
return suite
3 changes: 2 additions & 1 deletion werkzeug/testsuite/formparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from __future__ import with_statement

import unittest
import tempfile
from StringIO import StringIO
from os.path import join, dirname

Expand Down Expand Up @@ -134,7 +135,7 @@ def test_large_file(self):
method='POST')
# make sure we have a real file here, because we expect to be
# on the disk. > 1024 * 500

This comment was marked as off-topic.

self.assert_(isinstance(req.files['foo'].stream, file))
self.assertIsInstance(req.files['foo'].stream, tempfile._TemporaryFileWrapper)


class MultiPartTestCase(WerkzeugTestCase):
Expand Down