npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redis-scripts

v0.1.3

Published

Script manager module for Redis Lua script functionality.

Downloads

6

Readme

RedisScripts

Script manager module for Redis Lua script functionality.

Install

Simple installation with NPM

npm install redis-scripts

Usage

var redis = require("redis"),
	ScriptManager = require("redis-scripts"),
    client = redis.createClient();

client.on("error", function (err) {
    console.log("Error " + err);
});

client.on("ready", function (err) {
	var sm = new ScriptManager(client);

	// Load all Lua scripts in a directory into Redis
	sm.loadScriptsFromDirectory("./lua", function (err) {
         if (err) {
            console.log("Error " + err);
            process.exit(1);
         }

         // If your directory included a script called incrbyone.lua
         // you can now call it directly from the ScriptManager object
         sm.incrbyone('key');
    });
});

API

scriptManager.load(command, scriptFile, callback)

Load a file containing a Lua script into Redis and create a new command in the script manager object. With the support for Lua server side scripting, and the fact that Lua scripts run atomically, as any native command would, we can easily add new behavior to Redis. Once loaded the scripts are cached in the server and do not need to be reloaded again, You can expect this to be true until a SCRIPT FLUSH command is issued.

If a script is not found on the server you will receive a NOSCRIPT error message, and need to call scriptManager.load() again. Suggestion is to use your script loading code in the Redis client "ready" event handler. This way whenever you reconnect to Redis the scripts will be reloaded.

Parameters:

  • command name of the command that will be registered in the client
  • scriptFile name of the file containing the script. If no extension is given .lua is used.
  • callback optional callback

For more details on passing arguments to scripts and other important details about Lua scripting in general read the Redis EVAL command page at http://redis.io/commands/eval

After the script has been loaded you can call it as any native command, by using

scriptManager.command(key1, key2, ..., [callback]);

For example, imagine you need to implement a conditional increment command without using MULTI/EXEC/WATCH. Create a file called cincr.lua with the following content:

-- Conditional Increment
-- Increment Key if value is bigger than ARGV[1]
local value = tonumber(redis.call('get',KEYS[1]))
if value == nil then return {err="Value at key is not integer"} end
if value > tonumber(ARGV[1]) then
    value = value + 1
    redis.call('set',KEYS[1],value)
end
return value

Now from your application call:

scriptManager.Load('cincr', './cincr', 1, function(err, reply) {
    if( err ) {
        console.log( err );
        process.exit();
    }

    scriptManager.cincr('key1', 10, function (err, reply) {
        console.log(reply);
        process.exit();
    });
});

The load process will determine the number of Redis Keys your script needs by analysing the number of elements in the KEYS lua table.

scriptManager.getNumberOfKeys(command)

Return the number of Redis Keys the command script needs to run properly. If command does not exist, returns -1.

scriptManager.calcNumberOfKeys(code)

Calculate the number of Redis Keys the passed Lua source code needs to run properly. Returns the number of keys the script needs.

scriptManager.loadScriptsFromDirectory(path, next)

Load all Lua scripts found in path into Redis. Scripts that start with _ or . are ignored. Scripts must have a .lua extension.

Testing

Tests require Node mocha and instanbul modules to be installed globaly.

npm install mocha istanbul -g

To run all module tests simply run:

npm test

or

make test

To obtain a test coverage report run

npm cover

#License

MIT License