Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using redis cache to support multiple server (#5)
`redis or ur or useRedis: (default false)`- If you want to run multiple servers behind a load balancer (or multiple pods in Kubernetes) use this option to keep token information in one shared Redis server. Redis information can be provided with environment variables `REDIS_HOST` and `REDIS_PORT`.
- Loading branch information
1 parent
112be01
commit 392d4e1
Showing
7 changed files
with
134 additions
and
34 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
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
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,59 @@ | ||
const redis = require('redis'); | ||
|
||
let client = null; | ||
|
||
|
||
|
||
async function ensureConnection() { | ||
if (!client) { | ||
if (process.env.REDIS_HOST && process.env.REDIS_PORT) { | ||
client = redis.createClient({ | ||
socket: { | ||
host: process.env.REDIS_HOST, | ||
port: process.env.REDIS_PORT | ||
}, | ||
}); | ||
await openConnection(); | ||
} | ||
} else if (!client.isReady) { | ||
await openConnection(); | ||
} | ||
} | ||
|
||
async function openConnection() { | ||
if (!client.isOpen) await client.connect().catch(console.error); | ||
await client.ping().then(console.log).catch(console.error); | ||
} | ||
|
||
async function setData(store, key, value) { | ||
await ensureConnection(); | ||
const data = typeof value === 'object' ? JSON.stringify(value) : value; | ||
|
||
await client.set(`${store}_${key}`, data); | ||
return true; | ||
} | ||
|
||
async function getData(store, key) { | ||
await ensureConnection(); | ||
return client.get(`${store}_${key}`) | ||
.then((reply) => { | ||
console.log(`Data retrieved successfully: ${reply}`); | ||
try { | ||
return JSON.parse(reply); | ||
} catch(e) { | ||
console.error('Error parsing data: ', e.message); | ||
throw e; | ||
} | ||
}); | ||
} | ||
|
||
async function deleteKey(store, key) { | ||
await ensureConnection(); | ||
return client.del(`${store}_${key}`); | ||
} | ||
|
||
module.exports = { | ||
setData, | ||
getData, | ||
deleteKey, | ||
}; |