Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

chore: upgrade redis client #374

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
181 changes: 143 additions & 38 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"passport-oauth2": "^1.5.0",
"pm2": "^4.5.6",
"raw-body": "^2.3.2",
"redis": "^3.1.2",
"redis": "^4.6.11",
"rollbar": "^2.2.9",
"string-similarity": "^3.0.0",
"supertest": "^4.0.2",
Expand Down
99 changes: 28 additions & 71 deletions src/lib/redisClient.js
Original file line number Diff line number Diff line change
@@ -1,94 +1,51 @@
import redis from 'redis';
import { createClient } from 'redis';
import rollbar from './rollbar';

const client = redis.createClient(
// Reuse this connected client for future method calls
const clientPromise = createClient(
process.env.REDIS_URL || 'redis://127.0.0.1:6379'
);
)
.on('error', (err) => console.log('[redisClient]', err))
.connect();

function set(key, value) {
async function set(key, value) {
if (typeof key !== 'string') {
throw new Error('key of `set(key, value)` must be a string.');
}
return new Promise((resolve, reject) => {
client.set(key, JSON.stringify(value), (err, reply) => {
if (err) {
reject(err);
} else {
resolve(reply);
}
});
});

return (await clientPromise).set(key, JSON.stringify(value));
}

function get(key) {
async function get(key) {
if (typeof key !== 'string') {
throw new Error('key of `get(key)` must be a string.');
}
return new Promise((resolve, reject) => {
client.get(key, (err, reply) => {
if (err) {
reject(err);
} else {
try {
resolve(JSON.parse(reply));
} catch (e) {
// Gracefully fallback, in case the stuff in redis is a mess
//
console.error(e);
rollbar.error(e);
resolve({});
}
}
});
});
const reply = (await clientPromise).get(key);
try {
return JSON.parse(reply);
} catch (e) {
// Gracefully fallback, in case the stuff in redis is a mess
//
console.error(e);
rollbar.error(e);
return {};
}
}

function del(key) {
return new Promise((resolve, reject) => {
client.del(key, (err, reply) => {
if (err) {
reject(err);
} else {
resolve(reply);
}
});
});
async function del(key) {
return (await clientPromise).del(key);
}

function incr(key) {
return new Promise((resolve, reject) => {
client.incr(key, (err, reply) => {
if (err) {
reject(err);
} else {
resolve(reply);
}
});
});
async function incr(key) {
return (await clientPromise).incr(key);
}

function decr(key) {
return new Promise((resolve, reject) => {
client.decr(key, (err, reply) => {
if (err) {
reject(err);
} else {
resolve(reply);
}
});
});
async function decr(key) {
return (await clientPromise).decr(key);
}

function quit() {
return new Promise((resolve, reject) => {
client.quit((err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
async function quit() {
return (await clientPromise).quit();
}

export default {
Expand Down
Loading