-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16462 from mozilla/fxa-redis-v2
feat(redis): Add support to customs server to use redis or memcache
- Loading branch information
Showing
23 changed files
with
247 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const Memcached = require('memcached'); | ||
const { RedisShared } = require('fxa-shared/db/redis'); | ||
const P = require('bluebird'); | ||
P.promisifyAll(Memcached.prototype); | ||
|
||
class Cache { | ||
constructor(config) { | ||
const customsRedisConfig = config.redis.customs; | ||
this.useRedis = customsRedisConfig.enabled; | ||
|
||
if (this.useRedis) { | ||
this.client = new RedisShared(config.redis.customs); | ||
} else { | ||
this.client = new Memcached(config.memcache.address, { | ||
timeout: 500, | ||
retries: 1, | ||
retry: 1000, | ||
reconnect: 1000, | ||
idle: 30000, | ||
namespace: 'fxa~', | ||
}); | ||
} | ||
} | ||
|
||
async setAsync(key, value, lifetime) { | ||
if (this.useRedis) { | ||
if (lifetime === 0) { | ||
return this.client.redis.set(key, JSON.stringify(value)); | ||
} | ||
// Set the value in redis. We use 'EX' to set the expiration time in seconds. | ||
return this.client.redis.set(key, JSON.stringify(value), 'EX', lifetime); | ||
} | ||
return this.client.setAsync(key, value, lifetime); | ||
} | ||
|
||
async getAsync(key) { | ||
if (this.useRedis) { | ||
const value = await this.client.redis.get(key); | ||
try { | ||
return JSON.parse(value); | ||
} catch (err) { | ||
return {}; | ||
} | ||
} else { | ||
return this.client.getAsync(key); | ||
} | ||
} | ||
} | ||
|
||
module.exports = Cache; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"redis": { | ||
"customs": { | ||
"enabled": false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,14 @@ var test = require('tap').test; | |
var restifyClients = require('restify-clients'); | ||
var TestServer = require('../test_server'); | ||
var Promise = require('bluebird'); | ||
var mcHelper = require('../memcache-helper'); | ||
const { randomIp, randomEmail } = require('../utils'); | ||
|
||
var TEST_EMAIL = '[email protected]'; | ||
var TEST_EMAIL = randomEmail(); | ||
var ALLOWED_EMAIL = '[email protected]'; | ||
var TEST_IP = '192.0.2.1'; | ||
var TEST_IP = randomIp(); | ||
|
||
const config = require('../../lib/config').getProperties(); | ||
|
||
var testServer = new TestServer(config); | ||
|
||
var client = restifyClients.createJsonClient({ | ||
|
@@ -26,13 +27,6 @@ test('startup', async function (t) { | |
t.end(); | ||
}); | ||
|
||
test('clear everything', function (t) { | ||
mcHelper.clearEverything(function (err) { | ||
t.notOk(err, 'no errors were returned'); | ||
t.end(); | ||
}); | ||
}); | ||
|
||
test('missing email', function (t) { | ||
return client.postAsync('/blockEmail', {}).then( | ||
function (req, res, obj) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.