summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy McCurdy <andy@andymccurdy.com>2018-11-12 22:47:35 -0800
committerGitHub <noreply@github.com>2018-11-12 22:47:35 -0800
commit3e3c96ffdbf44d6daba841b8b77f6dba4ec02e8a (patch)
tree8c01f593d728046125254afdf17f3dd6e5cdaaf7
parent27df0332608f5fa02406890ecfc0a99126ef4805 (diff)
parentb7d718054cbec4e4609312c96d25d08049685dfd (diff)
downloadredis-py-3e3c96ffdbf44d6daba841b8b77f6dba4ec02e8a.tar.gz
Merge pull request #1061 from itamarhaber/memory_command
Adds the memory_usage and memory_purge commands
-rwxr-xr-xredis/client.py20
-rw-r--r--tests/test_commands.py5
2 files changed, 25 insertions, 0 deletions
diff --git a/redis/client.py b/redis/client.py
index d82078e..c6a8e4b 100755
--- a/redis/client.py
+++ b/redis/client.py
@@ -495,6 +495,8 @@ class StrictRedis(object):
'HSCAN': parse_hscan,
'INFO': parse_info,
'LASTSAVE': timestamp_to_datetime,
+ 'MEMORY PURGE': bool_ok,
+ 'MEMORY USAGE': int_or_none,
'OBJECT': parse_object,
'PING': lambda r: nativestr(r) == 'PONG',
'PUBSUB NUMSUB': parse_pubsub_numsub,
@@ -895,6 +897,24 @@ class StrictRedis(object):
"Return the encoding, idletime, or refcount about the key"
return self.execute_command('OBJECT', infotype, key, infotype=infotype)
+ def memory_usage(self, key, samples=None):
+ """
+ Return the total memory usage for key, its value and associated
+ administrative overheads.
+
+ For nested data structures, ``samples`` is the number of elements to
+ sample. If left unspecified, the server's default is 5. Use 0 to sample
+ all elements.
+ """
+ args = []
+ if isinstance(samples, int):
+ args.extend([Token.get_token('SAMPLES'), samples])
+ return self.execute_command('MEMORY USAGE', key, *args)
+
+ def memory_purge(self):
+ "Attempts to purge dirty pages for reclamation by allocator"
+ return self.execute_command('MEMORY PURGE')
+
def ping(self):
"Ping the Redis server"
return self.execute_command('PING')
diff --git a/tests/test_commands.py b/tests/test_commands.py
index 15e7620..cd3b13d 100644
--- a/tests/test_commands.py
+++ b/tests/test_commands.py
@@ -2177,6 +2177,11 @@ class TestRedisCommands(object):
.execute())
assert resp == [0, None, 255]
+ @skip_if_server_version_lt('4.0.0')
+ def test_memory_usage(self, r):
+ r.set('foo', 'bar')
+ assert isinstance(r.memory_usage('foo'), int)
+
class TestStrictCommands(object):
def test_strict_zadd(self, sr):