Skip to content

Commit

Permalink
Support for SCRIPT FLUSH with SYNC/ASYNC (#1567)
Browse files Browse the repository at this point in the history
Part of #1546
  • Loading branch information
chayim authored Sep 1, 2021
1 parent 42a050c commit e9837c1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
14 changes: 11 additions & 3 deletions redis/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2786,9 +2786,17 @@ def script_exists(self, *args):
"""
return self.execute_command('SCRIPT EXISTS', *args)

def script_flush(self):
"Flush all scripts from the script cache"
return self.execute_command('SCRIPT FLUSH')
def script_flush(self, sync_type="SYNC"):
"""Flush all scripts from the script cache.
``sync_type`` is by default SYNC (synchronous) but it can also be
ASYNC.
See: https://redis.io/commands/script-flush
"""
if sync_type not in ["SYNC", "ASYNC"]:
raise DataError("SCRIPT FLUSH defaults to SYNC or"
"accepts SYNC/ASYNC")
pieces = [sync_type]
return self.execute_command('SCRIPT FLUSH', *pieces)

def script_kill(self):
"Kill the currently executing Lua script"
Expand Down
18 changes: 18 additions & 0 deletions tests/test_scripting.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ def test_eval(self, r):
# 2 * 3 == 6
assert r.eval(multiply_script, 1, 'a', 3) == 6

def test_script_flush(self, r):
r.set('a', 2)
r.script_load(multiply_script)
r.script_flush('ASYNC')

r.set('a', 2)
r.script_load(multiply_script)
r.script_flush('SYNC')

r.set('a', 2)
r.script_load(multiply_script)
r.script_flush()

with pytest.raises(exceptions.DataError):
r.set('a', 2)
r.script_load(multiply_script)
r.script_flush("NOTREAL")

def test_evalsha(self, r):
r.set('a', 2)
sha = r.script_load(multiply_script)
Expand Down

0 comments on commit e9837c1

Please sign in to comment.