Skip to content
Open
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
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"prettier/@typescript-eslint"
],
"rules": {
"no-shadow": "off",
"import/prefer-default-export": "off",
"class-methods-use-this": "off",
"import-helpers/order-imports": [
Expand Down
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ module.exports = {
// moduleNameMapper: {},

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],
modulePathIgnorePatterns: ['dist'],

// Activates notifications for test results
// notify: false,
Expand Down Expand Up @@ -175,5 +175,5 @@ module.exports = {

// Whether to use watchman for file crawling
// watchman: true,
testTimeout: 30000,
testTimeout: 60000,
};
24 changes: 16 additions & 8 deletions src/cacheable.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Redis } from 'ioredis';
import { Cluster, Redis } from 'ioredis';
import winston, { Logger } from 'winston';

type RedisFactory = () => Redis;
type RedisAbstract = Redis | Cluster;

type RedisFactory = () => RedisAbstract;

type ConstructorOptions = {
logger?: Logger;
Expand All @@ -16,7 +18,7 @@ type CallOptions = {
type Result = string | unknown;

export class PromiseCacheable {
private redis: Redis;
private redis: RedisAbstract;

private logger: Logger;

Expand All @@ -40,10 +42,16 @@ export class PromiseCacheable {
const resultAsString =
typeof result === 'string' ? result : JSON.stringify(result);
try {
// atomic remove lock and setting cache
// try first set on cache, less problems if something goes wrong.
// it doesn't use transaction to be possible to work with cluster.
await this.redis.set(
key,
resultAsString,
'PX',
options.cacheTimeout
);
await this.redis
.multi()
.set(key, resultAsString, 'PX', options.cacheTimeout)
.pipeline()
.publish(notifyKey, resultAsString)
.del(lockKey)
.exec();
Expand Down Expand Up @@ -83,7 +91,7 @@ export class PromiseCacheable {
this.safeDisconnectRedis(this.redis);
}

private safeDisconnectRedis(redis: Redis) {
private async safeDisconnectRedis(redis: RedisAbstract) {
try {
redis.disconnect();
} catch (e) {
Expand All @@ -110,7 +118,7 @@ export class PromiseCacheable {
}

private safeSubscribe(
subscriberConnection: Redis,
subscriberConnection: RedisAbstract,
key: string,
notifyKey: string,
timeout: number
Expand Down
Loading