Skip to content
This repository was archived by the owner on Feb 21, 2023. It is now read-only.

Commit d178e66

Browse files
Support for SCRIPT FLUSH with SYNC/ASYNC (redis/redis-py#1567) Signed-off-by: Andrew-Chen-Wang <[email protected]>
1 parent 3024fb1 commit d178e66

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

aioredis/commands.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -3323,9 +3323,20 @@ def script_exists(self: _SELF_ANNOTATION, *args: str) -> Awaitable:
33233323
"""
33243324
return self.execute_command("SCRIPT EXISTS", *args)
33253325

3326-
def script_flush(self: _SELF_ANNOTATION) -> Awaitable:
3327-
"""Flush all scripts from the script cache"""
3328-
return self.execute_command("SCRIPT FLUSH")
3326+
def script_flush(
3327+
self: _SELF_ANNOTATION,
3328+
sync_type: Union[Literal["SYNC"], Literal["ASYNC"]] = "SYNC",
3329+
) -> Awaitable:
3330+
"""Flush all scripts from the script cache.
3331+
``sync_type`` is by default SYNC (synchronous) but it can also be
3332+
ASYNC.
3333+
See: https://redis.io/commands/script-flush
3334+
"""
3335+
if sync_type not in ["SYNC", "ASYNC"]:
3336+
raise DataError("SCRIPT FLUSH defaults to SYNC or "
3337+
"accepts SYNC/ASYNC")
3338+
pieces = [sync_type]
3339+
return self.execute_command('SCRIPT FLUSH', *pieces)
33293340

33303341
def script_kill(self: _SELF_ANNOTATION) -> Awaitable:
33313342
"""Kill the currently executing Lua script"""

tests/test_scripting.py

+19
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@ async def test_eval(self, r):
3333
# 2 * 3 == 6
3434
assert await r.eval(multiply_script, 1, "a", 3) == 6
3535

36+
@pytest.mark.asyncio(forbid_global_loop=True)
37+
async def test_script_flush(self, r):
38+
await r.set('a', 2)
39+
await r.script_load(multiply_script)
40+
await r.script_flush('ASYNC')
41+
42+
await r.set('a', 2)
43+
await r.script_load(multiply_script)
44+
await r.script_flush('SYNC')
45+
46+
await r.set('a', 2)
47+
await r.script_load(multiply_script)
48+
await r.script_flush()
49+
50+
with pytest.raises(exceptions.DataError):
51+
await r.set('a', 2)
52+
await r.script_load(multiply_script)
53+
await r.script_flush("NOTREAL")
54+
3655
@pytest.mark.asyncio(forbid_global_loop=True)
3756
async def test_evalsha(self, r):
3857
await r.set("a", 2)

0 commit comments

Comments
 (0)