0
0
Redisquery~10 mins

Script loading and caching in Redis - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to load a Lua script into Redis and get its SHA1 hash.

Redis
sha1 = redis.call('SCRIPT', '[1]', script)
Drag options to blanks, or click blank then click option'
ALOAD
BEXECUTE
CRUN
DEVAL
Attempts:
3 left
💡 Hint
Common Mistakes
Using EVAL instead of LOAD to get the SHA1 hash.
Using EXECUTE which is not a Redis command.
Confusing RUN with the correct command.
2fill in blank
medium

Complete the code to execute a cached Lua script by its SHA1 hash.

Redis
result = redis.call('[1]', sha1, 0)
Drag options to blanks, or click blank then click option'
AEVALSHA
BLOAD
CEXECUTE
DRUN
Attempts:
3 left
💡 Hint
Common Mistakes
Using LOAD instead of EVALSHA to execute the script.
Using EXECUTE which is not a Redis command.
Using RUN which is not a Redis command.
3fill in blank
hard

Fix the error in the code to check if a script is cached before running it.

Redis
if redis.call('SCRIPT', '[1]', sha1) == 1 then
    redis.call('EVALSHA', sha1, 0)
else
    sha1 = redis.call('SCRIPT', 'LOAD', script)
    redis.call('EVALSHA', sha1, 0)
end
Drag options to blanks, or click blank then click option'
AEXECUTE
BLOAD
CRUN
DEXISTS
Attempts:
3 left
💡 Hint
Common Mistakes
Using LOAD to check existence instead of EXISTS.
Using RUN or EXECUTE which are not Redis commands.
Confusing SCRIPT LOAD with SCRIPT EXISTS.
4fill in blank
hard

Fill both blanks to load a script only if it is not cached, then execute it.

Redis
if redis.call('SCRIPT', '[1]', sha1) == 0 then
    sha1 = redis.call('SCRIPT', '[2]', script)
end
redis.call('EVALSHA', sha1, 0)
Drag options to blanks, or click blank then click option'
AEXISTS
BLOAD
CRUN
DEXECUTE
Attempts:
3 left
💡 Hint
Common Mistakes
Using LOAD to check existence.
Using RUN or EXECUTE commands which do not exist.
Not updating the sha1 variable after loading.
5fill in blank
hard

Fill all three blanks to cache a script, check its existence, and execute it safely.

Redis
sha1 = redis.call('SCRIPT', '[1]', script)
if redis.call('SCRIPT', '[2]', sha1) == 1 then
    redis.call('[3]', sha1, 0)
else
    redis.call('EVAL', script, 0)
end
Drag options to blanks, or click blank then click option'
ALOAD
BEXISTS
CEVALSHA
DRUN
Attempts:
3 left
💡 Hint
Common Mistakes
Using RUN which is not a Redis command.
Using EVAL instead of EVALSHA to run cached script.
Not checking if the script exists before running.