meteor/meteor — pull request #187 by joshrtay (2012-06-15 07:11:27+00:00)
Title: redis

Trying to to get some feedback from the community on how they would like a redis backed store implemented.

The following is my proposal:

# Meteor.Store

Meteor.Store is a way for Meteor apps to safely store data in memory.

## Example

Create a hash store:

store = new Meteor.Store('store');

Add some data:

store.hset('key','field1','value1');
store.hmset('key',{'field2': 'value2'});

Watch for changes on key:

```javascript
store.observe('key',{
changed: function(key,obj,old_obj) {
console.log('the value of ' + key + ' is now',obj);
}
});
```

## API

### Meteor.Store(name[,options])

Create a meteor store with id `name`. The following `options` are available:

- `manager` same as for collection
- `driver` same as for collection
- `type` one of the five redis types, defaults to hash

### Meteor.Store#observe(pattern,callbacks)

Watch a set of keys that match the `pattern`. Currently only trailing wildcards are implemented, but `pattern` should have the same functionality as Reds#keys patterns.

The following `callbacks` are available as properties:

* __changed(key,val,old_val)__
The value at `key` changed from `old_val` to `val`.

* __removed(key,old_val)__
The `key` was removed from the store. It's previous value was `old_val`.

### Meteor.Store#watch(pattern)

In the same way the publish functions on the server can return a Collection.Cursor for collections, they can use watch to publish the values at the keys that match `pattern` for stores.

```javascript
session = new Meteor.Store('session')
Meteor.publish('session',function(id) {
return session.watch(id);
});
```
### Commands

Each type of store will implement a unique set of commands that parallels the redis commands associated with that type. A small subset of the commands have been implemented:

Meteor.Store#keys
Meteor.Store#exists
Meteor.Store#del

Meteor.Store('string')#get
Meteor.Store('string')#set

Meteor.Store('hash')#hset
Meteor.Store('hash')#hmset
Meteor.Store('hash')#hget
Meteor.Store('hash')#hgetall
Meteor.Store('hash')#hdel

Note: Typing the stores makes the polling procedure more efficient, because a store knows how to read its data (string stores use `get`, hash stores use `hgetall`, ...).

Checkout the added redis-session example to see Meteor.Store in action.