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-embeded-lua

v0.5.15

Published

Redis Lua Script Embeded In NodeJS

Downloads

10

Readme

node-redis-embeded-lua

var redis = require("redis"),
    redisClient = redis.createClient();
var redisEmbededLua = require('redis-embeded-lua');
 
redisEmbededLua.attach(redisClient);
 
var pack = redisClient.sha1pack(`
    local r = redis.call('keys', '*')
    local count = 0
    for k,v in ipairs(r) do
        /*
         * k: index of Array
         * v: the redis key
         */
        count = count + 1
    end
    return 'the dbsize: '..count
`);
redisClient.evalScript(pack)
.then(function(ret) {
    console.log(ret);
    redisClient.unref();
})
.catch(function(e) {
    console.error(e.toString());
    process.exit(1);
});
$ npm install redis-embeded-lua
$ vi test.js
$ node ./test.js
the dbsize: 9        # maybe different from your system.

Usage

1: promotion

var pack = redisClient.sha1pack(`
    /*
     * the ARGV is a string array.
     * SEE ALSO: examples/insertDB.js
     */
    redis.call('set', ARGV[1], ARGV[2])
    return 'ok'
`);

redisClient.evalScript(pack, 'foo', 'bar')
.then(function(ret) {
    console.log('the result:', ret);
    redisClient.unref();
})
.catch(function(e) {
    console.error(e.toString());
    process.exit(1);
});

2: promotion

var pack = redisClient.sha1pack(`
    /*
     *  the method 'call':
     *    the same rules as the printf family of standard C functions
     *    SEE ALSO, string.format(fmt, ...)
     *  yes! there are another function 'pcall' as redis.pcall
     */
    call('set foo bar_%s', 'hello world')
    return call('get foo')
`);
$ vi test.js
$ node ./test.js
the result: bar_hello world

3: promotion

exports.PI = 3.14
exports.mul = function(a,b)
    return a*b
end
var pack = redisClient.sha1pack(`
    local mathDemo = require('./mathDemo.lua')
    call('set radius 9')
    local radius = call('get radius')
    //area = radius * radius * PI
    return mathDemo.mul(radius*radius, mathDemo.PI)
`);
$ vi mathDemo.lua
$ vi test.js
$ node ./test.js
the result: 254

4: promotion

var redis = require("redis"),
    redisClient = redis.createClient();
var redisEmbededLua = require('redis-embeded-lua');

redisEmbededLua.attach(redisClient);

/*
 * Actually, `evalScript' must be called in a closure;
 * because, performance of `sha1pack' is expensive.
 * Yes, `run bussiness in closure' is the `original intention'.
 */
var yourBussinessDBCount = (function() {
    var pack = redisClient.sha1pack(`
        local r = call('keys *')
        local count = 0
        for k,v in ipairs(r) do
            count = count + 1
        end
        return 'the dbsize: '..count
    `);
    return function() {
        return redisClient.evalScript(pack);
    }
})();

yourBussinessDBCount()
.then(function(ret) {
    console.log(ret);
    redisClient.unref();
})
.catch(function(e) {
    console.error(e.toString());
    process.exit(1);
});
$ vi test.js
$ node ./test.js
the dbsize: 9         

or define `class':

class YourBussiness() {
    constructor(redisClient) {
        this.client = redisClient;
        redisEmbededLua.attach(this.client);
    }
}
YourBussiness.prototype.set = (function() {
    var script = redisClient.sha1pack(`
        call("set %s %s", ARGS[1], ARGS[2])
        return
    `);
    return function(key, val) {
        return this.client.evalScript(script, key, val);
    };
})();

YourBussiness.prototype.get = (function() {
    var script = redisClient.sha1pack(`
        return call('get %s', ARGS[1])
    `);
    return function(key) {
        return this.client.evalScript(script, key);
    };
})();

5: promotion

TIP: If you use only one db in redis, ignore this section.

Although I don't like the multi-db design of redis, provide two method using reids-multi-dbs flexible.

If I were you, I would use only #0 db.

method 1:

redisClient.configDBName({
    DEFAULT : 0,
    USERINFO: 1,
    STATICS : 11,
    TEST    : 15
});
var pack = redisClient.sha1pack(`
    local t = {}
    //Luckly, the initial db is always #0
    table.insert(t, call('dbsize'))
    select('TEST')
    table.insert(t, call('dbsize'))
    select('STATICS')
    table.insert(t, call('dbsize'))
    //Certainly, you can use db number directly
    select(1)
    table.insert(t, call('dbsize'))
    return t
`);
$ vi test.js
$ node test.js
[ 6, 1, 1, 3 ]

method 2:

exports = {
    DEFAULT = 0,
    USERINFO = 1,
    STATICS = 11,
    TEST = 15
}
var pack = redisClient.sha1pack(`
    local db = require('./conf.lua')
    local t = {}
    select(db.TEST)
    table.insert(t, call('dbsize'))
    select(db.STATICS)
    table.insert(t, call('dbsize'))
    select(1)
    table.insert(t, call('dbsize'))
    return t
`);
$ vi conf.lua
$ vi test.js
$ node test.js
[ 1, 1, 3 ]

Installation

npm install redis-embeded-lua


LUA API

require('file')

require like nodejs. Only support one level(and in embeded lua).

call(fmt, ...)

printf(3) + redis.call + redis-cli

pcall(fmt, ...)

printf(3) + redis.pcall + redis-cli

select(db)

  • select index
    select(1)
  • select dbname redisClient.configDBName(conf), then select(dbname);
    select('USERINFO')

return value

  • success: nil
  • fail: message

exists(key)

for examples:

    exists('foo')

return true or false

Instead of the following

if redis.call('exists', 'foo') == 1 then
    return true
else
    return false
end

JavaScirpt API

redisClient.evalScript(pack, arg1, arg2 ...)

  • pack: object, return by redisClient.sha1pack(script). required
  • arg1 - argn: arguments. optional

return Promise

redisClient.sha1pack(script)

  • script: lua scirpt, string

return object

{
    text:"lua script...",
    sha1:"sha1num"
}

redisClient.configDBName(conf)

  • conf object
{
    DEFAULT: 0,
    USERINFO: 1
}