ModJS: A Javascript Module for Redis and KeyDB

栏目: IT技术 · 发布时间: 5年前

内容简介:A Javascript Module for KeyDB and Redis.ModJS allows you to extend Redis and KeyDB with new functionality implemented in JavaScript (ES6). ModJS uses the V8 JIT engine so complex scripts can execute significantly faster than their Lua equivalents. In add

ModJS

A Javascript Module for KeyDB and Redis.

ModJS allows you to extend Redis and KeyDB with new functionality implemented in JavaScript (ES6). ModJS uses the V8 JIT engine so complex scripts can execute significantly faster than their Lua equivalents. In addition ModJS supports many node.js modules offering extensive library support for common tasks.

Quick Start Guide

The fastest way to install ModJS is with docker .

Once installed there are two ways to use ModJS, the first is similar to Lua with the EVALJS Command:

> EVALJS "redis.call('get', 'testkey')"

While EVALJS is quick and easy, a much more powerful method exists in the form of startup scripts. In a startup script you can define your own custom commands and call them from any client as though they were built-in. In addition, these commands can skip the parsing step of EVALJS and so will execute much faster.

Adding a Command

All commands must be defined at the time the module is loaded in a startup script. A startup script is simply a javascript file who's path is passed to ModJS at load time.

Below we create an example script named startup.js which defines a new command named "concat". This command fetches two keys and returns the string concatenation of the two values. We then register this function with the server so that it will be directly callable from Redis or KeyDB clients.

function concat(key1, key2) {
    var str1 = redis.call('get', key1);
    var str2 = redis.call('get', key2);
    return str1 + str2;
}

keydb.register(concat);

Note: The redis and keydb objects may be used interchangebly.

To run this script on startup simply add the path as a module parameter, e.g. loadmodule /path/to/modjs.so /path/to/startup.js

We may now use this command from any client. E.g.:

127.0.0.1:6379> set keyA foo
OK
127.0.0.1:6379> set keyB bar
OK
127.0.0.1:6379> concat keyA keyB
"foobar"

Importing scripts from npm

The above examples were simple enough not to require external libraries, however for more complex tasks it may be desireable to import modules fetched via npm. ModJS implements the require() api with similar semantics to node.js.

In this example we will use the popular lodash library, installed with: npm install loadash . Below we've updated our example script to use the camelCase() function in lodash:

var _ = require("lodash")

function concat(key1, key2) {
    var str1 = redis.call('get', key1);
    var str2 = redis.call('get', key2);
    return _.camelCase(str1 + " " + str2)
}

keydb.register(concat);

The lodash module is imported with require() as it would be in a node.js script. Note that require() will search for modules starting from the working directory of Redis or KeyDB. Once loaded this new script will concatenate the two strings using camel case.

A quick note on compatibility: ModJS does not implement most I/O functionality available in Node. As a result libraries that open files, sockets, etc may not run in ModJS. This limitation is to ensure correct replication behavior of scripts. In the future we may enable an unsafe mode that provides more of this functionality.

Consistency Gurantees and Programming Model

ModJS offers the same consistency gurantees as provided with Lua scripts. Each JS command is executed atomically regardless of whether EVALJS or registered commands are used.

Global variables and functions created in startup sripts are available for subsequent use in registered commands and EVALJS functions. Modules imported via the require() method exist in their own javascript context and may only export via the exports object.

Compiling ModJS

ModJS requires you to first build V8, as a result we recommend using a pre-compiled docker image. However if you wish to compile ModJS first follow the instructions to download and build V8 here: https://v8.dev/docs/build

Once you have built the core V8 library we must then build the monolith with the following command:

/path/to/v8/$ ninja -C out.gn/x64.release.sample v8_monolith

If V8 compiled successfully you are now ready to build ModJS. ModJS can be built with one line:

make V8_PATH=/path/to/v8

Docker with ModJS

  • Visit the official Docker Repository here: eqalpha/modjs
  • Dockerfiles can be found here

Launch a container with ModJS

KeyDB:

$ sudo docker run eqalpha/modjs

Redis

$ sudo docker run eqalpha/modjs:redis-latest

Launch container bound to host:

$ sudo docker run -p 6379:6379 eqalpha/modjs

Launch with Startup Script

When launching with the docker container you will need to share your script with the docker container by mounting it as a volume to the "scripts" volume in the container:

With Redis:

$ sudo  docker  run  -p  6379:6379  -v  /path/to/startupjs/vol:/scripts  eqalpha/modjs:redis-latest  redis-server  --loadmodule  /usr/lib/keydb/modules/modjs.so  /scripts/startup.js

With KeyDB:

$ sudo  docker  run  -p  6379:6379  -v  /path/to/startupjs/vol:/scripts  eqalpha/modjs  keydb-server  /etc/keydb/keydb.conf  --loadmodule  /usr/lib/keydb/modules/modjs.so  /scripts/startup.js

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据结构与算法

数据结构与算法

BrunoRPreiss / 电子工业出版社 / 2003-1 / 55.00元

本书是作者根据他在滑铁卢大学计算机工程学院教授数据结构与算法课程的经验编写而成的。它采用C++面向对象的设计模式,不仅系统全面地介绍了各种传统的数据结构,还把它们按照类和类层次的现代理念予以展开,进而达到抽象结构与实际设计的完美统一。本书的后三章通过引入抽象问题求解的概念,集中讲述了算法技术和各算法之间的关系。另外,作者运用一定的数学工具以及必要的分析技术和分析理论,对每种数据结构及相关算法都进行一起来看看 《数据结构与算法》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具