This small package is intended to help out with managing Lua scripts for redis as those scripts need
to be sent via the client and can profit from a wrapper. More importantly, this wrapper allows for
scripts to include code that is in other script files via
#include 'file.lua' which promotes code
Note that this package is client agnostic and still requires you to create a wrapper for your client. You may also want to write the created code to new files for debugging purposes.
Install this module via npm..
npm install redis-lua-helper
.. and include is as such:
var RedisLuaHelper = ;var rlh = ;
In your lua script files, you may use a
#include 'filename' macro. This macro will include a
different script at the place where this macro was placed. File paths are relative to the current
file. Included files may contain macros as well, but circular dependencies will raise errors.
Example of one Lua script including the other via
print('This is foo!')#include 'bar'
print('This is bar!')
Processed contents when loading foo script:
print('This is foo!')-- #include bar:print('This is bar!')-- End of bar
Create a new instance of the RedisLuaHelper class, expects either the path of your script folder or a full configuration object. Below are all possible options and there default values:
var RedisLuaHelper = ;// using an options objectvar rlh =;// just provide the script pathvar rlh2 = ;
root field defines the path relative from which scripts will be loaded. The
that script files may only be loaded if they share that extension. File
encoding defaults to utf8
but can be overridden. The
macro option allows you to override the default
#include 'file' to
Load one or more Lua script files into the instance cache. The callback function should expect an error argument and an array of loaded script files. Examples:
// load a single scriptrlh;// you can load any amount of scripts in this fashionrlh;// filenames can be provided as an array as wellrlh;
Notice: Right now, checking for circular dependencies may cause problems if you try to call
multiple times in a row before awaiting the first call to finish. To avoid this, you should place
all filenames that you wish to load in an array and call
Loads all script files in a given directory, relative to the root directory. Does not include files in subdirectories. If the dirpath argument is omitted, the root directory will be used instead.
// load all files in the root directory (not including subdirectories)rlh;// load all files in /root/subdirrlh;
Returns the code of a previously loaded script. You must load a script first before you can access its code.
var code = rlhcode'myscript';
Returns the shasum of a previously loaded script. You must load a script first before you can access its shasum.
// returns the scripts shasum// i.e. 6b1bf486c81ceb7edf3c093f4c48582e38c0e791var shasum = rlh;
Returns the number of expected KEYS of a previously loaded script. You must load a script first before you can access its keys.
// returns the number of keys that a script expects// i.e. the script below will return 2var keys = rlh;
-- example script, rlh.keys() will return 2local foo = KEYSlocal bar = KEYS
Note that the keys are retrieved by stupidly looking for the highest number that is found in the script code, even if you comment out a line of code that contains a high number. Dynamic values are not supported; avoid confusion by keeping KEYS[*] at a single place, i.e. at the beginning of your file.
-- example script two, rlh.keys() will return 99local foo = KEYS-- commented line contains KEYS
Clears the entire cache, is the same as creating a fresh instance.
// clear the script cacherlh;
Added RLH#keys() functionality by scanning through script code and picking the highest number that is written using KEYS[*] so you can pass it to your redis client.
Copyright (C) 2013 Geerten van Meel
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.