mikro-orm-cache-adapter-redis
Advanced tools
Comparing version 0.1.4 to 0.1.5
import { CacheAdapter } from '@mikro-orm/core'; | ||
import { RedisOptions } from 'ioredis'; | ||
export interface RedisCacheAdapterOptions extends RedisOptions { | ||
expiration?: number; | ||
debug?: boolean; | ||
@@ -9,6 +10,7 @@ } | ||
private readonly debug; | ||
private readonly expiration?; | ||
constructor(options: RedisCacheAdapterOptions); | ||
get(key: string): Promise<any>; | ||
set(name: string, data: any, origin: string, expiration?: number): Promise<void>; | ||
set(name: string, data: any, origin: string, expiration?: number | undefined): Promise<void>; | ||
clear(): Promise<void>; | ||
} |
@@ -57,5 +57,9 @@ "use strict"; | ||
function RedisCacheAdapter(options) { | ||
var _a = options.debug, debug = _a === void 0 ? false : _a, redisOpt = __rest(options, ["debug"]); | ||
var _a = options.debug, debug = _a === void 0 ? false : _a, expiration = options.expiration, redisOpt = __rest(options, ["debug", "expiration"]); | ||
this.client = new ioredis_1.default(redisOpt); | ||
this.debug = debug; | ||
this.expiration = expiration; | ||
if (this.debug) { | ||
console.log("redis client created", redisOpt); | ||
} | ||
} | ||
@@ -81,15 +85,18 @@ RedisCacheAdapter.prototype.get = function (key) { | ||
RedisCacheAdapter.prototype.set = function (name, data, origin, expiration) { | ||
if (expiration === void 0) { expiration = this.expiration; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var stringData; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
stringData = JSON.stringify(data); | ||
if (this.debug) { | ||
console.log("set \"" + name + "\": \"" + data + "\" with expiration " + expiration); | ||
console.log("set \"" + name + "\": \"" + stringData + "\" with expiration " + expiration); | ||
} | ||
if (!expiration) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, this.client.set(name, JSON.stringify(data), 'PX', expiration)]; | ||
return [4 /*yield*/, this.client.set(name, stringData, 'PX', expiration)]; | ||
case 1: | ||
_a.sent(); | ||
return [3 /*break*/, 4]; | ||
case 2: return [4 /*yield*/, this.client.set(name, JSON.stringify(data))]; | ||
case 2: return [4 /*yield*/, this.client.set(name, stringData)]; | ||
case 3: | ||
@@ -96,0 +103,0 @@ _a.sent(); |
{ | ||
"name": "mikro-orm-cache-adapter-redis", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "A redis cache adapter for mikro-orm", | ||
@@ -12,3 +12,4 @@ "main": "dist/index.js", | ||
"prepare": "npm run build", | ||
"build": "tsc" | ||
"build": "tsc", | ||
"build-watch": "tsc -w" | ||
}, | ||
@@ -15,0 +16,0 @@ "dependencies": { |
9246
155