Skip to content

Instantly share code, notes, and snippets.

@andymccurdy
Created October 24, 2011 22:07
Show Gist options
  • Save andymccurdy/1310500 to your computer and use it in GitHub Desktop.
Save andymccurdy/1310500 to your computer and use it in GitHub Desktop.
class Redis(...):
def __init__(self):
# dictionary of "installed" scripts to sha's
self.loaded_scripts = {}
def load_script(self, name, code):
"Install a script, giving it a friendly name"
sha = self.execute_command('SCRIPT', 'LOAD', code)
# save the friendly name => sha
self.loaded_scripts[name] = sha
def execute_script(self, name, *args):
try:
sha = self.loaded_scripts[name]
except KeyError:
raise Exception("Whoops, no script named: %s loaded" % name)
# lookup the script's sha by name in the local dictionary then eval
self.execute_command('EVALSHA', sha, *args)
# Some code that runs at application start time
redis.load_script("my_friendly_name", "MY LUA CODE.......")
redis.load_script("another script", "MORE LUA CODE.........")
...
# code that's run during the life of my application
redis.execute_script("my_friendly_name", arg1, arg2, ...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment