Skip to content

Add support for redis namespaces #41

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions lib/common/_connect.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const q = require('q')
const EventEmitter = require('events').EventEmitter
const { MongoClient, ObjectId } = require('mongodb')
const Redis = require('redis')
const Redis = require('ioredis')
const fs = require('fs')

const DATABASE_VERSION = 9
Expand All @@ -26,9 +26,14 @@ module.exports = function (config) {
delete config.mongo.port
delete config.mongo.database

const redis = Redis.createClient(config.redis)
const pub = Redis.createClient(config.redis)
const sub = Redis.createClient(config.redis)
if (config.redis.namespace) {
config.redis.keyPrefix = `${config.redis.namespace}:`
delete config.redis.namespace
}

const redis = new Redis(config.redis)
const pub = new Redis(config.redis)
const sub = new Redis(config.redis)

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

// https://github.com/screeps/backend-local/blob/7520c8c7e6a443ad955d25e064dbd151a909d8cb/lib/cronjobs.js#L393
// https://github.com/screeps/backend-local/blob/7520c8c7e6a443ad955d25e064dbd151a909d8cb/lib/strongholds.js#L132
//
// ignore properly escaped sector regex queries
// https://github.com/screeps/backend-local/blob/7520c8c7e6a443ad955d25e064dbd151a909d8cb/lib/cronjobs.js#L393
// https://github.com/screeps/backend-local/blob/7520c8c7e6a443ad955d25e064dbd151a909d8cb/lib/strongholds.js#L132
//
// ignore properly escaped sector regex queries
} else if (v.match(/\^[EW]\d*\\d[NS]\d*\\d\$/g) === null && v.match(/\^\[[EW]{2}\]\\d\*5\[[NS]{2}\]\\d\*5\$/g) === null) {
// default regex escape fix for loki regex queries to work with mongo regex queries
query.$regex = v.replace(/\\{1,2}/g, '\\\\')
Expand Down
3 changes: 2 additions & 1 deletion lib/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ module.exports = function (config) {
}, opts.mongo || {}),
redis: Object.assign({
host: process.env.REDIS_HOST || 'localhost',
port: process.env.REDIS_PORT || 6379
port: process.env.REDIS_PORT || 6379,
namespace: process.env.REDIS_NAMESPACE || ''
}, opts.redis || {})
})
config.common.storage.env.keys.ROOM_INTENTS = 'roomIntents:'
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
"body-parser": "^1.19.0",
"express": "^4.17.1",
"ini": "^1.3.4",
"ioredis": "^4.28.5",
"mongodb": "^3.5.5",
"q-json-response": "^0.1.3",
"redis": "^3.0.2"
}
}
}