mirror of
https://github.com/spacedeck/spacedeck-open.git
synced 2025-12-16 09:57:30 +01:00
replace redis by in-memory object
This commit is contained in:
110
helpers/redis.js
110
helpers/redis.js
@@ -1,14 +1,103 @@
|
||||
'use strict';
|
||||
|
||||
const RedisConnection = require('ioredis');
|
||||
const websockets = require('./websockets');
|
||||
var notRedis = {
|
||||
state: {},
|
||||
topics: {},
|
||||
|
||||
publish: function(topic, msg, cb) {
|
||||
//console.log("[notredis] publish",topic,msg);
|
||||
if (!this.topics[topic]) {
|
||||
this.topics[topic] = {
|
||||
subscribers: []
|
||||
};
|
||||
}
|
||||
var t=this.topics[topic];
|
||||
for (var i=0; i<t.subscribers.length; i++) {
|
||||
var s=t.subscribers[i];
|
||||
if (s.handler) {
|
||||
s.handler(topic, msg);
|
||||
}
|
||||
}
|
||||
if (cb) cb(null);
|
||||
},
|
||||
|
||||
subscribe: function(topics, cb) {
|
||||
var handle = {
|
||||
handler: null,
|
||||
on: function(evt, cb) {
|
||||
if (evt == "message") {
|
||||
this.handler = cb;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
for (var i=0; i<topics.length; i++) {
|
||||
var topic = topics[i];
|
||||
if (!this.topics[topic]) {
|
||||
this.topics[topic] = {
|
||||
subscribers: []
|
||||
};
|
||||
}
|
||||
|
||||
var t=this.topics[topic];
|
||||
t.subscribers.push(handle);
|
||||
}
|
||||
|
||||
cb(null, handle, topics.length);
|
||||
return handle;
|
||||
},
|
||||
|
||||
get: function(key, cb) {
|
||||
cb(null, this.state[key]);
|
||||
return this.state[key];
|
||||
},
|
||||
|
||||
set: function(key, val, cb) {
|
||||
this.state[key] = val;
|
||||
cb();
|
||||
},
|
||||
|
||||
del: function(key, cb) {
|
||||
delete this.state[key];
|
||||
cb(null);
|
||||
},
|
||||
|
||||
sadd: function(key, skey, cb) {
|
||||
if (!this.state[key]) this.state[key] = {};
|
||||
this.state[key][skey] = true;
|
||||
cb(null);
|
||||
},
|
||||
|
||||
srem: function(key, skey, cb) {
|
||||
if (this.state[key]) {
|
||||
delete this.state[key][skey];
|
||||
}
|
||||
cb(null);
|
||||
},
|
||||
|
||||
smembers: function(key, cb) {
|
||||
cb(null, Object.keys(this.state[key]));
|
||||
},
|
||||
|
||||
incr: function(key, cb) {
|
||||
if (!this.state[key]) this.state[key] = 0;
|
||||
this.state[key]++;
|
||||
cb();
|
||||
},
|
||||
|
||||
expire: function() {
|
||||
},
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
connectRedis(){
|
||||
const redisHost = process.env.REDIS_PORT_6379_TCP_ADDR || 'localhost';
|
||||
this.connection = new RedisConnection(6379, redisHost);
|
||||
connectRedis: function() {
|
||||
this.connection = notRedis;
|
||||
},
|
||||
sendMessage(action, model, attributes, channelId) {
|
||||
getConnection: function() {
|
||||
this.connectRedis();
|
||||
return this.connection;
|
||||
},
|
||||
sendMessage: function(action, model, attributes, channelId) {
|
||||
const data = JSON.stringify({
|
||||
channel_id: channelId,
|
||||
action: action,
|
||||
@@ -17,12 +106,12 @@ module.exports = {
|
||||
});
|
||||
this.connection.publish('updates', data);
|
||||
},
|
||||
logIp(ip, cb) {
|
||||
logIp: function(ip, cb) {
|
||||
this.connection.incr("ip_"+ ip, (err, socketCounter) => {
|
||||
cb();
|
||||
});
|
||||
},
|
||||
rateLimit(namespace, ip, cb) {
|
||||
rateLimit: function(namespace, ip, cb) {
|
||||
const key = "limit_"+ namespace + "_"+ ip;
|
||||
const redis = this.connection;
|
||||
|
||||
@@ -47,7 +136,7 @@ module.exports = {
|
||||
}
|
||||
});
|
||||
},
|
||||
isOnlineInSpace(user, space, cb) {
|
||||
isOnlineInSpace: function(user, space, cb) {
|
||||
this.connection.smembers("space_" + space._id.toString(), function(err, list) {
|
||||
if (err) cb(err);
|
||||
else {
|
||||
@@ -59,3 +148,6 @@ module.exports = {
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return module.exports;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user