javascript in-memory implementation of Redis API with observe changes interface and reactivity
This is an all-javascript in-memory implementation of the Redis API, adapted for Meteor.
var redis = new Miniredis.RedisStore;
redis.set("key-1-1", "foo");
redis.set("key-1-2", "bar");
You can install it by running:
meteor add slava:miniredis
This implementation supports Deps.js reactivity with fine-grained reactivity: if a command was run in a Deps.autorun computation, then the minimal set of dependencies will be tracked.
var redis = new Miniredis.RedisStore;
redis.set("key-1-1", "foo");
Deps.autorun(function () {
console.log(_.pluck(redis.matching("key-1-*").fetch(), "value"));
});
// prints ["foo"]
redis.set("key-2-1", "baz");
// doesn't print anything
redis.set("key-1-2", "bar");
// prints ["foo", "bar"]
redis.set("key-1-1", "foo1");
// prints ["foo1", "bar"]
Similar to Minimongo's Cursor's, Miniredis Cursors (as returned by
redis.matching("*-pattern-*")
calls) can be observed with added
, changed
and removed
callbacks.
redis.matching("key-2-*").observe({
added: function (item) { /* item: { _id, value } */ },
changed: function (item, oldItem) { /* item: { _id, value} */ },
removed: function (item) { /* item: { _id, value } */ }
});
You can also have an ordered observe by passing addedAt
, changedAt
,
removedAt
callbacks. The current order is the lexicographical order of keys.
redis.matching("key-2-*").observe({
addedAt: function (item, atIndex, before) { /* item: { _id, value } */ },
changedAt: function (item, oldItem, atIndex) { /* item: { _id, value} */ },
removedAt: function (item, atIndex) { /* item: { _id, value } */ }
});
observeChanges
is also implemented but it is not very different from the
observe
version as of yet.
You can pause/resume observers with the same API as of Minimongo:
// Pause observers
redis.pauseObservers();
// Make a lot of changes
_.each(removedValues, function (value, key) {
redis.del(key);
});
_.each(newValues, function (value, key) {
redis.set(key, value);
});
// Resume
redis.resumeObservers();
Miniredis's cursors can be observed by Blaze:
<template name="orderedList">
{{#each listItems}}
<div>{{_id}} - {{value}}</div>
{{/each}}
</template>
Template.orderedList.listItems = function () {
return redis.matching("key-2-*");
};
devel
branch and will beTo support Meteor's latency compensation, this implementation tries to mimic the behavior of the Redis server.
Right now these Redis commands are implemented and available:
MIT (c) Meteor Development Group