diff --git a/django_elasticache/memcached.py b/django_elasticache/memcached.py index d301580..35cb0e2 100644 --- a/django_elasticache/memcached.py +++ b/django_elasticache/memcached.py @@ -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 diff --git a/tests/test_backend.py b/tests/test_backend.py index e6d6d63..83a4e69 100644 --- a/tests/test_backend.py +++ b/tests/test_backend.py @@ -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(): @@ -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)