Redis how to reduce lua copy-paste -


i'm writing logic redis inside lua , each of scripts have common, handy move out shared function but

  1. redis can't use lua's require statement
  2. officially can't call other redis function(see: https://stackoverflow.com/a/22599862/1812225)

for example have snippet literally everywhere

local prefix = "/" .. type if typeid     prefix = prefix .. "(" .. typeid .. ")" end 

i'm thinking post-processing before feeding scripts redis seems over-kill...

what best practice solve/reduce problem?

updated:

local registrykey = "/counters/set-" .. type local updatedkey = "/counters/updated/set-" .. type if typeid     redis.call("sadd", updatedkey, name .. ":" .. typeid)     redis.call("sadd", registrykey, name .. ":" .. typeid) else     redis.call("sadd", updatedkey, name)     redis.call("sadd", registrykey, name) end 

is code sample , can't trivially moved client-side invokes redis commands, , works part of transaction

thanks!

"hack" #1

after script load something, sha1 hash can use evalsha. same sha1 value can used call script inside script - call function f_<sha1>. said, there differences in how pass keys/argv structures when used way.

note undocumented behavior, means behavior change in future version of redis.

credit teaching me goes dr. josiah carlson who, in turn, credits else (iirc fritzy). more information check out lua-call python wrapper: https://github.com/josiahcarlson/lua-call

"hack" #2

redis sandboxes lua , puts several restrictions on in order maintain sanity. go around of these, e.g. access _g , define utility function there available scripts (like did https://github.com/redislabs/redis-lua-debugger).

however, pretty risky - besides potential replication issues, usage untested , therefore lead undefined behavior (i managed crash quite few instances little script ;)).

p.s.

both hacks require additional administrative work ensure these "global" scripts loaded before other script calls them.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -