Skip to content

Commit f72147a

Browse files
author
Demannu
committed
Add support for redis namespaces
1 parent 19587f2 commit f72147a

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

lib/common/_connect.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const q = require('q')
22
const EventEmitter = require('events').EventEmitter
33
const { MongoClient, ObjectId } = require('mongodb')
4-
const Redis = require('redis')
4+
const Redis = require('ioredis')
55
const fs = require('fs')
66

77
const DATABASE_VERSION = 9
@@ -26,9 +26,14 @@ module.exports = function (config) {
2626
delete config.mongo.port
2727
delete config.mongo.database
2828

29-
const redis = Redis.createClient(config.redis)
30-
const pub = Redis.createClient(config.redis)
31-
const sub = Redis.createClient(config.redis)
29+
if (config.redis.namespace) {
30+
config.redis.keyPrefix = `${config.redis.namespace}:`
31+
delete config.redis.namespace
32+
}
33+
34+
const redis = new Redis(config.redis)
35+
const pub = new Redis(config.redis)
36+
const sub = new Redis(config.redis)
3237

3338
const mongo = q.ninvoke(MongoClient, 'connect', uri, Object.assign({ promiseLibrary: Promise, useUnifiedTopology: true }, config.mongo))
3439
.then(client => client.db())
@@ -78,10 +83,10 @@ module.exports = function (config) {
7883
if (v === '^[WE]d*5[NS]d*5$') {
7984
query.$regex = v.replace(/\]d\*/g, ']\\d*')
8085

81-
// https://github.com/screeps/backend-local/blob/7520c8c7e6a443ad955d25e064dbd151a909d8cb/lib/cronjobs.js#L393
82-
// https://github.com/screeps/backend-local/blob/7520c8c7e6a443ad955d25e064dbd151a909d8cb/lib/strongholds.js#L132
83-
//
84-
// ignore properly escaped sector regex queries
86+
// https://github.com/screeps/backend-local/blob/7520c8c7e6a443ad955d25e064dbd151a909d8cb/lib/cronjobs.js#L393
87+
// https://github.com/screeps/backend-local/blob/7520c8c7e6a443ad955d25e064dbd151a909d8cb/lib/strongholds.js#L132
88+
//
89+
// ignore properly escaped sector regex queries
8590
} else if (v.match(/\^[EW]\d*\\d[NS]\d*\\d\$/g) === null && v.match(/\^\[[EW]{2}\]\\d\*5\[[NS]{2}\]\\d\*5\$/g) === null) {
8691
// default regex escape fix for loki regex queries to work with mongo regex queries
8792
query.$regex = v.replace(/\\{1,2}/g, '\\\\')

lib/common/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ module.exports = function (config) {
1515
}, opts.mongo || {}),
1616
redis: Object.assign({
1717
host: process.env.REDIS_HOST || 'localhost',
18-
port: process.env.REDIS_PORT || 6379
18+
port: process.env.REDIS_PORT || 6379,
19+
namespace: process.env.REDIS_NAMESPACE || 'screeps'
1920
}, opts.redis || {})
2021
})
2122
config.common.storage.env.keys.ROOM_INTENTS = 'roomIntents:'

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
"body-parser": "^1.19.0",
2323
"express": "^4.17.1",
2424
"ini": "^1.3.4",
25+
"ioredis": "^4.28.5",
2526
"mongodb": "^3.5.5",
2627
"q-json-response": "^0.1.3",
2728
"redis": "^3.0.2"
2829
}
29-
}
30+
}

0 commit comments

Comments
 (0)