mirror of
https://github.com/wassname/talk.git
synced 2026-08-14 12:50:17 +08:00
replaced eslint:recommended with prettier
This commit is contained in:
+34
-39
@@ -1,7 +1,7 @@
|
||||
const redis = require('./redis');
|
||||
const debug = require('debug')('talk:services:cache');
|
||||
|
||||
const cache = module.exports = {};
|
||||
const cache = (module.exports = {});
|
||||
|
||||
/**
|
||||
* This collects a key that may either be an array or a string and creates a
|
||||
@@ -9,7 +9,7 @@ const cache = module.exports = {};
|
||||
* @param {Mixed} key Either an array of items composing a key or a string
|
||||
* @return {String} A string that represents a key
|
||||
*/
|
||||
const keyfunc = (key) => {
|
||||
const keyfunc = key => {
|
||||
if (Array.isArray(key)) {
|
||||
return `cache[${key.join(':')}]`;
|
||||
}
|
||||
@@ -55,7 +55,6 @@ cache.wrap = async (key, expiry, work, kf = keyfunc) => {
|
||||
* Init sets up the scripts used in Redis with the incr/decr commands.
|
||||
*/
|
||||
cache.init = async () => {
|
||||
|
||||
// Create the redis instance.
|
||||
cache.client = redis.createClient();
|
||||
|
||||
@@ -98,13 +97,15 @@ cache.init = async () => {
|
||||
* This will increment a key in redis and update the expiry iff it already
|
||||
* exists, otherwise it will do nothing.
|
||||
*/
|
||||
cache.incr = async (key, expiry, kf = keyfunc) => cache.client.increx(kf(key), expiry);
|
||||
cache.incr = async (key, expiry, kf = keyfunc) =>
|
||||
cache.client.increx(kf(key), expiry);
|
||||
|
||||
/**
|
||||
* This will decrement a key in redis and update the expiry iff it already
|
||||
* exists, otherwise it will do nothing.
|
||||
*/
|
||||
cache.decr = async (key, expiry, kf = keyfunc) => cache.client.decrex(kf(key, expiry));
|
||||
cache.decr = async (key, expiry, kf = keyfunc) =>
|
||||
cache.client.decrex(kf(key, expiry));
|
||||
|
||||
/**
|
||||
* This will increment many keys in redis and update the expiry iff it already
|
||||
@@ -114,7 +115,6 @@ cache.incrMany = async (keys, expiry, kf = keyfunc) => {
|
||||
let multi = cache.client.multi();
|
||||
|
||||
for (const key of keys) {
|
||||
|
||||
// Queue up the evalsha command.
|
||||
multi.increx(kf(key), expiry);
|
||||
}
|
||||
@@ -130,7 +130,6 @@ cache.decrMany = async (keys, expiry, kf = keyfunc) => {
|
||||
let multi = cache.client.multi();
|
||||
|
||||
for (const key of keys) {
|
||||
|
||||
// Queue up the evalsha command.
|
||||
multi.decrex(kf(key), expiry);
|
||||
}
|
||||
@@ -154,10 +153,10 @@ cache.wrapMany = async (keys, expiry, work, kf = keyfunc) => {
|
||||
|
||||
// find any of the null valued items by collecting the work
|
||||
let workRefs = values
|
||||
.map((value, index) => ({value, index, key: keys[index]}))
|
||||
.filter(({value}) => value === null);
|
||||
.map((value, index) => ({ value, index, key: keys[index] }))
|
||||
.filter(({ value }) => value === null);
|
||||
|
||||
let workKeys = workRefs.map(({key}) => key);
|
||||
let workKeys = workRefs.map(({ key }) => key);
|
||||
|
||||
debug(`wrapMany: hit ratio: ${keys.length - workKeys.length}/${keys.length}`);
|
||||
|
||||
@@ -172,7 +171,7 @@ cache.wrapMany = async (keys, expiry, work, kf = keyfunc) => {
|
||||
.then(() => {
|
||||
debug('wrapMany: setMany complete');
|
||||
})
|
||||
.catch((err) => {
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
});
|
||||
});
|
||||
@@ -192,15 +191,15 @@ cache.wrapMany = async (keys, expiry, work, kf = keyfunc) => {
|
||||
* @param {Mixed} key Either an array of items composing a key or a string
|
||||
* @return {Promise}
|
||||
*/
|
||||
cache.get = async (key, kf = keyfunc) => cache.client.get(kf(key)).then((reply) => {
|
||||
if (typeof reply !== 'undefined' && reply !== null) {
|
||||
cache.get = async (key, kf = keyfunc) =>
|
||||
cache.client.get(kf(key)).then(reply => {
|
||||
if (typeof reply !== 'undefined' && reply !== null) {
|
||||
// Parse the stored cache value from JSON.
|
||||
return JSON.parse(reply);
|
||||
}
|
||||
|
||||
// Parse the stored cache value from JSON.
|
||||
return JSON.parse(reply);
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
return null;
|
||||
});
|
||||
|
||||
/**
|
||||
* Returns many replies.
|
||||
@@ -209,23 +208,22 @@ cache.get = async (key, kf = keyfunc) => cache.client.get(kf(key)).then((reply)
|
||||
* @param {Function} [kf=keyfunc] optional key function to use to turn the
|
||||
* provided key into a string for the cache.
|
||||
*/
|
||||
cache.getMany = async (keys, kf = keyfunc) => cache.client.mget(keys.map(kf)).then((replies) => {
|
||||
cache.getMany = async (keys, kf = keyfunc) =>
|
||||
cache.client.mget(keys.map(kf)).then(replies => {
|
||||
// Parse the replies.
|
||||
for (let i = 0; i < replies.length; i++) {
|
||||
let value = null;
|
||||
|
||||
// Parse the replies.
|
||||
for (let i = 0; i < replies.length; i++) {
|
||||
let value = null;
|
||||
if (typeof replies[i] !== 'undefined' && replies[i] !== null) {
|
||||
// Parse the stored cache value from JSON.
|
||||
value = JSON.parse(replies[i]);
|
||||
}
|
||||
|
||||
if (typeof replies[i] !== 'undefined' && replies[i] !== null) {
|
||||
|
||||
// Parse the stored cache value from JSON.
|
||||
value = JSON.parse(replies[i]);
|
||||
replies[i] = value;
|
||||
}
|
||||
|
||||
replies[i] = value;
|
||||
}
|
||||
|
||||
return replies;
|
||||
});
|
||||
return replies;
|
||||
});
|
||||
|
||||
/**
|
||||
* Sets many entries in the cache.
|
||||
@@ -238,7 +236,6 @@ cache.setMany = async (keys, values, expiry, kf = keyfunc) => {
|
||||
let multi = cache.client.multi();
|
||||
|
||||
keys.forEach((key, index) => {
|
||||
|
||||
// Serialize the value as JSON.
|
||||
let reply = JSON.stringify(values[index]);
|
||||
|
||||
@@ -255,7 +252,6 @@ cache.setMany = async (keys, values, expiry, kf = keyfunc) => {
|
||||
* @return {Promise}
|
||||
*/
|
||||
cache.invalidate = async (key, kf = keyfunc) => {
|
||||
|
||||
debug(`invalidate: ${kf(key)}`);
|
||||
|
||||
return cache.client.del(kf(key));
|
||||
@@ -270,7 +266,6 @@ cache.invalidate = async (key, kf = keyfunc) => {
|
||||
* @return {Promise}
|
||||
*/
|
||||
cache.set = async (key, value, expiry, kf = keyfunc) => {
|
||||
|
||||
// Serialize the value as JSON.
|
||||
let reply = JSON.stringify(value);
|
||||
|
||||
@@ -283,7 +278,6 @@ cache.set = async (key, value, expiry, kf = keyfunc) => {
|
||||
cache.h = {};
|
||||
|
||||
cache.h.get = async (key, field = '__default__') => {
|
||||
|
||||
// Get the current value from redis.
|
||||
const reply = await cache.client.hget(keyfunc(key), field);
|
||||
|
||||
@@ -295,7 +289,6 @@ cache.h.get = async (key, field = '__default__') => {
|
||||
};
|
||||
|
||||
cache.h.set = async (key, field = '__default__', value, expiry = 60) => {
|
||||
|
||||
// Serialize the value as JSON.
|
||||
let reply = JSON.stringify(value);
|
||||
|
||||
@@ -339,6 +332,8 @@ cache.h.wrap = async (key, field, expiry, work) => {
|
||||
return value;
|
||||
};
|
||||
|
||||
cache.h.incr = async (key, field = '__default__', expiry) => cache.client.hincrbyex(keyfunc(key), field, 1, expiry);
|
||||
cache.h.incr = async (key, field = '__default__', expiry) =>
|
||||
cache.client.hincrbyex(keyfunc(key), field, 1, expiry);
|
||||
|
||||
cache.h.decr = async (key, field = '__default__', expiry) => cache.client.hincrbyex(keyfunc(key), field, -1, expiry);
|
||||
cache.h.decr = async (key, field = '__default__', expiry) =>
|
||||
cache.client.hincrbyex(keyfunc(key), field, -1, expiry);
|
||||
|
||||
Reference in New Issue
Block a user