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

upgrade to latest ioredis v5 + switch to typescript #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions dist/index.d.ts
@@ -0,0 +1,20 @@
import { Store } from "cache-manager";
export declare class RedisStore implements Store {
private redisCache;
private storeArgs;
name: string;
isCacheableValue: any;
constructor(...args: any[]);
getClient(): any;
set(key: any, value: any, options: any, cb: any): Promise<unknown>;
get(key: any, options: any, cb: any): Promise<unknown>;
del(key: any, options: any, cb: any): any;
reset(cb: any): any;
keys(pattern: any, cb: any): Promise<unknown>;
ttl(key: any, cb: any): any;
}
declare const _default: {
create: (...args: any[]) => RedisStore;
};
export default _default;
//# sourceMappingURL=index.d.ts.map
1 change: 1 addition & 0 deletions dist/index.d.ts.map

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

212 changes: 94 additions & 118 deletions dist/index.js
@@ -1,123 +1,99 @@
'use strict';

const Redis = require('ioredis');

const redisStore = (...args) => {
let redisCache = null;

if (args.length > 0 && args[0].redisInstance) {
redisCache = args[0].redisInstance;
} else if (args.length > 0 && args[0].clusterConfig) {
const {
nodes,
options
} = args[0].clusterConfig;

redisCache = new Redis.Cluster(nodes, options || {});
} else {
redisCache = new Redis(...args);
}

const storeArgs = redisCache.options;

let self = {
name: 'redis',
isCacheableValue: storeArgs.isCacheableValue || (value => value !== undefined && value !== null),
};

self.getClient = () => redisCache;

self.set = (key, value, options, cb) => (
new Promise((resolve, reject) => {
if (typeof options === 'function') {
cb = options;
options = {};
}
options = options || {};

if (!cb) {
cb = (err, result) => (err ? reject(err) : resolve(result));
}

if (!self.isCacheableValue(value)) {
return cb(new Error(`"${value}" is not a cacheable value`));
}

const ttl = (options.ttl || options.ttl === 0) ? options.ttl : storeArgs.ttl;
const val = JSON.stringify(value) || '"undefined"';

if (ttl) {
redisCache.setex(key, ttl, val, handleResponse(cb));
} else {
redisCache.set(key, val, handleResponse(cb));
}
})
);

self.get = (key, options, cb) => (
new Promise((resolve, reject) => {
if (typeof options === 'function') {
cb = options;
}

if (!cb) {
cb = (err, result) => (err ? reject(err) : resolve(result));
}

redisCache.get(key, handleResponse(cb, { parse: true }));
})
);

self.del = (key, options, cb) => {
if (typeof options === 'function') {
cb = options;
import Redis from 'ioredis';

class RedisStore {
constructor(...args) {
this.name = "redis";
if (args.length > 0 && args[0].redisInstance) {
this.redisCache = args[0].redisInstance;
}
else if (args.length > 0 && args[0].clusterConfig) {
const { nodes, options } = args[0].clusterConfig;
this.redisCache = new Redis.Cluster(nodes, options || {});
}
else {
this.redisCache = new Redis(...args);
}
this.storeArgs = this.redisCache.options;
this.isCacheableValue = this.storeArgs.isCacheableValue || ((value) => value !== undefined && value !== null);
}

redisCache.del(key, handleResponse(cb));
};

self.reset = cb => redisCache.flushdb(handleResponse(cb));

self.keys = (pattern, cb) => (
new Promise((resolve, reject) => {
if (typeof pattern === 'function') {
cb = pattern;
pattern = '*';
}

if (!cb) {
cb = (err, result) => (err ? reject(err) : resolve(result));
}

redisCache.keys(pattern, handleResponse(cb));
})
);

self.ttl = (key, cb) => redisCache.ttl(key, handleResponse(cb));

return self;
};

function handleResponse(cb, opts = {}) {
return (err, result) => {
if (err) {
return cb && cb(err);
getClient() {
return this.redisCache;
}

if (opts.parse) {
try {
result = JSON.parse(result);
} catch (e) {
return cb && cb(e);
}
set(key, value, options, cb) {
return new Promise((resolve, reject) => {
if (typeof options === "function") {
cb = options;
options = {};
}
options = options || {};
if (!cb) {
cb = (err, result) => (err ? reject(err) : resolve(result));
}
if (!this.isCacheableValue(value)) {
return cb(new Error(`"${value}" is not a cacheable value`));
}
const ttl = options.ttl || options.ttl === 0 ? options.ttl : this.storeArgs.ttl;
const val = JSON.stringify(value) || '"undefined"';
if (ttl) {
this.redisCache.setex(key, ttl, val, handleResponse(cb));
}
else {
this.redisCache.set(key, val, handleResponse(cb));
}
});
}
get(key, options, cb) {
return new Promise((resolve, reject) => {
if (typeof options === "function") {
cb = options;
}
if (!cb) {
cb = (err, result) => (err ? reject(err) : resolve(result));
}
this.redisCache.get(key, handleResponse(cb, { parse: true }));
});
}
del(key, options, cb) {
if (typeof options === "function") {
cb = options;
}
return this.redisCache.del(key, handleResponse(cb));
}
reset(cb) {
return this.redisCache.flushdb(handleResponse(cb));
}
keys(pattern, cb) {
return new Promise((resolve, reject) => {
if (typeof pattern === "function") {
cb = pattern;
pattern = "*";
}
if (!cb) {
cb = (err, result) => (err ? reject(err) : resolve(result));
}
this.redisCache.keys(pattern, handleResponse(cb));
});
}
ttl(key, cb) {
return this.redisCache.ttl(key, handleResponse(cb));
}

return cb && cb(null, result);
};
}
function handleResponse(cb, opts = {}) {
return (err, result) => {
if (err) {
return cb && cb(err);
}
if (opts.parse) {
try {
result = JSON.parse(result);
}
catch (e) {
return cb && cb(e);
}
}
return cb && cb(null, result);
};
}
var index = { create: (...args) => new RedisStore(...args) };

const methods = {
create: (...args) => redisStore(...args),
};

module.exports = methods;
export { RedisStore, index as default };
2 changes: 2 additions & 0 deletions dist/index.spec.d.ts
@@ -0,0 +1,2 @@
export {};
//# sourceMappingURL=index.spec.d.ts.map
1 change: 1 addition & 0 deletions dist/index.spec.d.ts.map

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

121 changes: 0 additions & 121 deletions index.js

This file was deleted.

4 changes: 4 additions & 0 deletions jest.config.js
@@ -0,0 +1,4 @@
/** @type {import('ts-jest').InitialOptionsTsJest} */
module.exports = {
preset: "ts-jest",
};