From 6728c130e2cc59f2a2651b77323bbf1ecb52ec9d Mon Sep 17 00:00:00 2001 From: Anton-Shutik Date: Thu, 10 Jun 2021 13:09:56 +0300 Subject: [PATCH 1/4] Store client in thread safe way --- django_elasticache/memcached.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/django_elasticache/memcached.py b/django_elasticache/memcached.py index a78fbd7..89c65a8 100644 --- a/django_elasticache/memcached.py +++ b/django_elasticache/memcached.py @@ -5,6 +5,7 @@ from functools import wraps from django.core.cache import InvalidCacheBackendError from django.core.cache.backends.memcached import PyLibMCCache +from threading import local from .cluster_utils import get_cluster_info @@ -28,6 +29,7 @@ class ElastiCache(PyLibMCCache): it used pylibmc in binary mode """ def __init__(self, server, params): + self._local = local() self.update_params(params) super(ElastiCache, self).__init__(server, params) if len(self._servers) > 1: From d55062b1fbc289d14f25db05e802d5e277e7201c Mon Sep 17 00:00:00 2001 From: Anton-Shutik Date: Thu, 10 Jun 2021 15:12:42 +0300 Subject: [PATCH 2/4] Use BaseMemcachedCache as base class --- django_elasticache/memcached.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/django_elasticache/memcached.py b/django_elasticache/memcached.py index 89c65a8..4f7775f 100644 --- a/django_elasticache/memcached.py +++ b/django_elasticache/memcached.py @@ -4,7 +4,7 @@ import socket from functools import wraps from django.core.cache import InvalidCacheBackendError -from django.core.cache.backends.memcached import PyLibMCCache +from django.core.cache.backends.memcached import BaseMemcachedCache from threading import local from .cluster_utils import get_cluster_info @@ -23,7 +23,7 @@ def wrapper(self, *args, **kwds): return wrapper -class ElastiCache(PyLibMCCache): +class ElastiCache(BaseMemcachedCache): """ backend for Amazon ElastiCache (memcached) with auto discovery mode it used pylibmc in binary mode @@ -31,7 +31,8 @@ class ElastiCache(PyLibMCCache): def __init__(self, server, params): self._local = local() self.update_params(params) - super(ElastiCache, self).__init__(server, params) + import pylibmc + super(ElastiCache, self).__init__(server, params, library=pylibmc, value_not_found_exception=pylibmc.NotFound) if len(self._servers) > 1: raise InvalidCacheBackendError( 'ElastiCache should be configured with only one server ' From abdffead73b292fce03b76d57935c5c83a8a0c80 Mon Sep 17 00:00:00 2001 From: Anton-Shutik Date: Fri, 18 Jun 2021 16:11:00 +0300 Subject: [PATCH 3/4] Fixed test --- tests/test_protocol.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_protocol.py b/tests/test_protocol.py index 7232a1a..07ec8bc 100644 --- a/tests/test_protocol.py +++ b/tests/test_protocol.py @@ -108,7 +108,7 @@ def test_no_configuration_protocol_support_with_errors_ignored(Telnet): call(b'version\n'), call(b'config get cluster\n'), ]) - eq_(info['version'], '1.4.34') + eq_(info['version'], b'1.4.34') eq_(info['nodes'], ['test:0']) From 53f608a33e2b1503afb398e8953e156a68aeb2ba Mon Sep 17 00:00:00 2001 From: Anton Shutik Date: Tue, 6 Feb 2024 18:00:36 +0100 Subject: [PATCH 4/4] Replaced smart_text with smart_str --- django_elasticache/cluster_utils.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/django_elasticache/cluster_utils.py b/django_elasticache/cluster_utils.py index 60594ca..ff1fe09 100644 --- a/django_elasticache/cluster_utils.py +++ b/django_elasticache/cluster_utils.py @@ -2,7 +2,7 @@ utils for discovery cluster """ from distutils.version import StrictVersion -from django.utils.encoding import smart_text +from django.utils.encoding import smart_str import re from telnetlib import Telnet @@ -35,7 +35,7 @@ def get_cluster_info(host, port, ignore_cluster_errors=False): if len(version_list) not in [2, 3] or version_list[0] != b'VERSION': raise WrongProtocolData('version', res) version = version_list[1] - if StrictVersion(smart_text(version)) >= StrictVersion('1.4.14'): + if StrictVersion(smart_str(version)) >= StrictVersion('1.4.14'): cmd = b'config get cluster\n' else: cmd = b'get AmazonElastiCache:cluster\n' @@ -50,8 +50,8 @@ def get_cluster_info(host, port, ignore_cluster_errors=False): return { 'version': version, 'nodes': [ - '{0}:{1}'.format(smart_text(host), - smart_text(port)) + '{0}:{1}'.format(smart_str(host), + smart_str(port)) ] } @@ -67,8 +67,8 @@ def get_cluster_info(host, port, ignore_cluster_errors=False): try: for node in ls[2].split(b' '): host, ip, port = node.split(b'|') - nodes.append('{0}:{1}'.format(smart_text(ip or host), - smart_text(port))) + nodes.append('{0}:{1}'.format(smart_str(ip or host), + smart_str(port))) except ValueError: raise WrongProtocolData(cmd, res) return {