Skip to content
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
7 changes: 1 addition & 6 deletions django_elasticache/memcached.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,14 @@ def get_cluster_nodes(self):
self._servers[0], err
))

@property
@cached_property
def _cache(self):
# PylibMC uses cache options as the 'behaviors' attribute.
# It also needs to use threadlocals, because some versions of
# PylibMC don't play well with the GIL.
client = getattr(self._local, 'client', None)
if client:
return client

client = self._lib.Client(self.get_cluster_nodes)
if self._options:
client.behaviors = self._options

self._local.client = client

return client
17 changes: 17 additions & 0 deletions tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
from mock import patch, Mock
from nose.tools import eq_, raises

from django.conf import settings
settings.configure()
global_settings.configured = True

@patch('django.conf.settings', global_settings)
def test_patch_params():
Expand Down Expand Up @@ -40,3 +43,17 @@ def test_split_servers(get_cluster_info):
assert backend._cache
get_cluster_info.assert_called_once_with('h', '0')
backend._lib.Client.assert_called_once_with(servers)

@patch('django.conf.settings', global_settings)
@patch('django_elasticache.memcached.get_cluster_info')
def test_property_cache(get_cluster_info):
from django_elasticache.memcached import ElastiCache
backend = ElastiCache('h:0', {})
servers = ['h1:p', 'h2:p']
get_cluster_info.return_value = {
'nodes': servers
}
backend._lib.Client = Mock()
backend.set('key1', 'val')
backend.set('key2', 'val')
backend._lib.Client.assert_called_once_with(servers)