Skip to content

Commit

Permalink
Extract code to check redis version
Browse files Browse the repository at this point in the history
Also rename 'has_scripting' into 'support_scripting'
  • Loading branch information
twidi committed Jan 26, 2018
1 parent 2808b20 commit bd405af
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion limpyd/contrib/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
23 changes: 15 additions & 8 deletions limpyd/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/contrib/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit bd405af

Please sign in to comment.