Redis avec Meteor 1.4
Premiere chose bien sur il vous faut avoir installé redis sur votre environement
###Pour linux
apt-get install redis
###Pour OSx avec Brew
brew install redis
###Installation NPM pour redis meteor npm install --save redis meteor npm install --save hiredis
Il vous faudrat aussi installer ValidatedMethod meteor add mdg:validated-method
###Import de redis dans votre projet meteor 1.4 Dans le repertoire imports de votre projet Meteor 1.4 cree un nouveau repertoir redis puis un autre repertoire server. Dans le repertoire redis crée un fichier methods.js Dans le repertoire serveur crée un fichier redis_conf.js
redis/server/redis_conf.js
import { Meteor } from 'meteor/meteor'; import redis from 'redis'; export let client = redis.createClient(); client.setSync = Meteor.wrapAsync(client.set); client.getSync = Meteor.wrapAsync(client.get); client.setExpire = Meteor.wrapAsync(client.expire); client.on("error", function(err) { console.log("REDIS ERROR", err); }); client.on("connect", function() { console.log("connected to REDIS"); });
redis/methods.js
import { client } from './server/redis_conf.js'; export const setRedis = new ValidatedMethod({ name: 'setRedis', validate: new SimpleSchema({ redis_key: { type: String }, redis_val: { type: String } }).validator(), run({redis_key, redis_val}){ return client.setSync(redis_key, redis_val); } }); export const getRedis = new ValidatedMethod({ name: 'getRedis', validate: new SimpleSchema({ redis_key: { type: String } }).validator(), run({redis_key}){ return client.getSync(redis_key); } }); export const setRedisExpire = new ValidatedMethod({ name: 'setRedisExpire', validate: new SimpleSchema({ redis_key: { type: String }, key_expire: { type: String } }).validator(), run({redis_key, key_expire}){ return client.setExpire(redis_key, key_expire); } });
Pour l'utiliser Redis dans votre projet (serveur uniquement bien sur) c'est tres simple.
let isInRedis = Meteor.call('getRedis', {redis_key: Mykey}); let setInRedis = Meteor.call('setRedis', {redis_key: "MyKey", redis_val: "MyVal"}); let setExpire = Meteor.call('setRedisExpire', {redis_key: "MyKey", key_expire: "600"} );
Source: http://www.curtismlarson.com/blog/2016/05/31/meteor-connect-to-redis/










