From bd405af2daf1f0fe293952e42a85dfc75fd1929f Mon Sep 17 00:00:00 2001 From: "Stephane Angel (Twidi)" Date: Tue, 7 Feb 2017 00:37:51 +0100 Subject: [PATCH] Extract code to check redis version Also rename 'has_scripting' into 'support_scripting' --- limpyd/contrib/collection.py | 2 +- limpyd/database.py | 23 +++++++++++++++-------- tests/contrib/collection.py | 2 +- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/limpyd/contrib/collection.py b/limpyd/contrib/collection.py index 7abad2a..1bdba9f 100644 --- a/limpyd/contrib/collection.py +++ b/limpyd/contrib/collection.py @@ -72,7 +72,7 @@ def _list_to_set(self, list_key, set_key): Use scripting if available to avoid retrieving all values locally from the list before sending them back to the set """ - if self.cls.database.has_scripting(): + if self.cls.database.support_scripting(): self._call_script('list_to_set', keys=[list_key, set_key]) else: conn = self.cls.get_connection() diff --git a/limpyd/database.py b/limpyd/database.py index 8ac21dd..4ed4b04 100644 --- a/limpyd/database.py +++ b/limpyd/database.py @@ -131,21 +131,28 @@ def connection(self): self._connection = self.connect() return self._connection - def has_scripting(self): + @property + def redis_version(self): + """Return the redis version as a tuple""" + if not hasattr(self, '_redis_version'): + self._redis_version = tuple( + map(int, self.connection.info().get('redis_version').split('.')[:3]) + ) + return self._redis_version + + def support_scripting(self): """ Returns True if scripting is available. Checks are done in the client - library (redis-py) AND the redis server. Resut is cached, so done only + library (redis-py) AND the redis server. Result is cached, so done only one time. """ - if not hasattr(self, '_has_scripting'): + if not hasattr(self, '_support_scripting'): try: - version = float('%s.%s' % - tuple(self.connection.info().get('redis_version').split('.')[:2])) - self._has_scripting = version >= 2.5 \ + self._support_scripting = self.redis_version >= (2, 5) \ and hasattr(self.connection, 'register_script') except: - self._has_scripting = False - return self._has_scripting + self._support_scripting = False + return self._support_scripting class Lock(redis.client.Lock): diff --git a/tests/contrib/collection.py b/tests/contrib/collection.py index e462f87..c96d82a 100644 --- a/tests/contrib/collection.py +++ b/tests/contrib/collection.py @@ -350,7 +350,7 @@ def test_intersect_should_accept_listfield_without_scripting(self): collection = set(Group.collection().intersect(container.groups_list)) self.assertEqual(collection, set(['1', '2'])) - @unittest.skipUnless(test_database.has_scripting(), "Redis scripting not available") + @unittest.skipUnless(test_database.support_scripting(), "Redis scripting not available") def test_intersect_should_accept_listfield_via_scripting(self): container = GroupsContainer()