slava:miniredis

v1.0.0Published 10 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

Miniredis

This is an all-javascript in-memory implementation of the Redis API, adapted for Meteor.

1var redis = new Miniredis.RedisStore;
2redis.set("key-1-1", "foo");
3redis.set("key-1-2", "bar");

You can install it by running:

meteor add slava:miniredis

Reactivity

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.

1var redis = new Miniredis.RedisStore;
2redis.set("key-1-1", "foo");
3
4Deps.autorun(function () {
5  console.log(_.pluck(redis.matching("key-1-*").fetch(), "value"));
6});
7// prints ["foo"]
8
9redis.set("key-2-1", "baz");
10// doesn't print anything
11
12redis.set("key-1-2", "bar");
13// prints ["foo", "bar"]
14
15redis.set("key-1-1", "foo1");
16// prints ["foo1", "bar"]

Observe API

Similar to Minimongo's Cursor's, Miniredis Cursors (as returned by redis.matching("*-pattern-*") calls) can be observed with added, changed and removed callbacks.

1redis.matching("key-2-*").observe({
2  added: function (item) { /* item: { _id, value } */ },
3  changed: function (item, oldItem) { /* item: { _id, value} */ },
4  removed: function (item) { /* item: { _id, value } */ }
5});

You can also have an ordered observe by passing addedAt, changedAt, removedAt callbacks. The current order is the lexicographical order of keys.

1redis.matching("key-2-*").observe({
2  addedAt: function (item, atIndex, before) { /* item: { _id, value } */ },
3  changedAt: function (item, oldItem, atIndex) { /* item: { _id, value} */ },
4  removedAt: function (item, atIndex) { /* item: { _id, value } */ }
5});

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:

1// Pause observers
2redis.pauseObservers();
3// Make a lot of changes
4_.each(removedValues, function (value, key) {
5  redis.del(key);
6});
7_.each(newValues, function (value, key) {
8  redis.set(key, value);
9});
10// Resume
11redis.resumeObservers();

Blaze compatibility

Miniredis's cursors can be observed by Blaze:

1<template name="orderedList">
2  {{#each listItems}}
3    <div>{{_id}} - {{value}}</div>
4  {{/each}}
5</template>
1Template.orderedList.listItems = function () {
2  return redis.matching("key-2-*");
3};

Known Issue

  • on Meteor 0.8.2 returning a miniredis cursor from a template helper Blaze will not observe it properly. It is fixed on the devel branch and will be released in next Meteor version. To workaround it, return a fetched array from cursor.

Redis API compatibility

To 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:

On Strings

  • set
  • get
  • del
  • exists
  • keys
  • randomkey
  • rename
  • renamenx
  • type
  • append
  • decr
  • decrby
  • getrange
  • getset
  • incr
  • incrby
  • incrbyfloat
  • mget
  • mset
  • msetnx
  • setx
  • setnx
  • setrange
  • strlen

On Hashes

  • hset
  • hsetnx
  • hget
  • hkeys
  • hvals
  • hgetall
  • hincrby
  • hincrbyfloat
  • hdel
  • hmset
  • hmget
  • hlen
  • hexists

On Lists

  • lpush
  • rpush
  • lpop
  • rpop
  • lindex
  • linsert
  • lrange
  • lset
  • ltrim
  • llen
  • lpushx
  • rpushx

License

MIT (c) Meteor Development Group