rebridge基础入门_Redis的透明Javascript接口

rebridge基础入门

GitHub:https://github.com/CapacitorSet/rebridge

rebridge基础入门_Redis的透明Javascript接口

简介描述:Redis的透明Javascript接口

Rebridge是一个透明的Javascript-Redis桥。您可以使用它来创建自动同步到Redis数据库的JavaScript对象。

安装

npm install rebridge

同步,非阻塞使用

const Rebridge = require("rebridge");
const redis = require("redis");

const client = redis.createClient();
const db = new Rebridge(client, {
    mode: "deasync"
});

db.users = [];
db.users.push({
    username: "johndoe",
    email: "johndoe@domain.com"
});
db.users.push({
    username: "foobar",
    email: "foobar@domain.com"
});
db.users.push({
    username: "CapacitorSet",
    email: "CapacitorSet@users.noreply.github.com"
});
console.log("Users:", db.users._value); // Prints the list of users
const [me] = db.users.filter(user => user.username === "CapacitorSet");
console.log("Me:", me); // Prints [{username: "CapacitorSet", email: "..."}]
client.quit();

异步使用

const Rebridge = require("rebridge");
const redis = require("redis");

const client = redis.createClient();
const db = new Rebridge(client);

db.users.set([])
    .then(() => Promise.all([
        db.users.push({
            username: "johndoe",
            email: "johndoe@domain.com"
        }),
        db.users.push({
            username: "foobar",
            email: "foobar@domain.com"
        }),
        db.users.push({
            username: "CapacitorSet",
            email: "CapacitorSet@users.noreply.github.com"
        })
    ]))
    .then(() => db.users._promise)
    .then(arr => console.log("Users:", arr)) // Prints the list of users
    .then(() => db.users.filter(user => user.username === "CapacitorSet"))
    .then(([me]) => console.log("Me:", me)) // Prints [{username: "CapacitorSet", email: "..."}]
    .then(() => client.quit())
    .catch(err => console.log("An error occurred:", err));

Rebridge使用ES6代理对象,因此至少需要Node 6。  

海计划公众号
(0)
上一篇 2020/03/11 04:51
下一篇 2020/03/11 05:54

您可能感兴趣的内容