@javien/mikro-orm-redis-cache-adapter
Advanced tools
Comparing version 0.0.8 to 0.0.9
@@ -23,3 +23,4 @@ "use strict"; | ||
__export(src_exports, { | ||
Options: () => Options | ||
Options: () => Options, | ||
RedisCacheAdapter: () => RedisCacheAdapter | ||
}); | ||
@@ -59,5 +60,100 @@ module.exports = __toCommonJS(src_exports); | ||
}; | ||
// src/redisCacheAdapter.ts | ||
var import_node_v8 = require("node:v8"); | ||
var RedisCacheAdapter = class { | ||
options; | ||
serializer = import_node_v8.serialize; | ||
deserializer = import_node_v8.deserialize; | ||
constructor(options) { | ||
this.options = options instanceof Options ? options : new Options(options); | ||
} | ||
async get(name) { | ||
try { | ||
const key = this.generateKey(name); | ||
const data = await this.options.client.getBuffer(key); | ||
if (!data) { | ||
return void 0; | ||
} | ||
const deserializedData = this.deserializer(data); | ||
this.debug("Get", key, deserializedData); | ||
return deserializedData; | ||
} catch (error) { | ||
this.options.logger("Failed to fetch data."); | ||
this.options.logger(error); | ||
return void 0; | ||
} | ||
} | ||
async set(name, data, _origin, expiration) { | ||
try { | ||
const key = this.generateKey(name); | ||
const serializedData = this.serializer(data); | ||
if (expiration) { | ||
this.options.client.set(key, serializedData, "PX", expiration); | ||
} else { | ||
this.options.client.set(key, serializedData); | ||
} | ||
this.debug("Set", key, data); | ||
} catch (error) { | ||
this.options.logger("Failed to store data."); | ||
this.options.logger(error); | ||
} | ||
} | ||
async remove(name) { | ||
try { | ||
const key = this.generateKey(name); | ||
await this.options.client.del(key); | ||
this.debug("Remove", key, void 0); | ||
} catch (error) { | ||
this.options.logger("Failed to remove data."); | ||
this.options.logger(error); | ||
} | ||
} | ||
clear() { | ||
return new Promise((resolve, reject) => { | ||
const stream = this.options.client.scanStream({ | ||
match: `${this.options.prefix}${this.options.prefixDelimiter}*` | ||
}); | ||
const pipeline = this.options.client.pipeline(); | ||
stream.on("data", (keys) => { | ||
if (keys.length === 0) { | ||
return; | ||
} | ||
for (const key of keys) { | ||
pipeline.del(key); | ||
} | ||
}); | ||
stream.on("end", () => { | ||
pipeline.exec((err) => { | ||
if (err) { | ||
this.options.logger("Failed to clear data."); | ||
this.options.logger(err); | ||
return reject(err); | ||
} | ||
this.options.logger("Cache has been successfully cleared."); | ||
resolve(); | ||
}); | ||
}); | ||
}); | ||
} | ||
close() { | ||
if (!this.options.gracefulShutdown) { | ||
return; | ||
} | ||
this.options.client.disconnect(); | ||
} | ||
debug(method, key, data) { | ||
if (!this.options.debug) { | ||
return; | ||
} | ||
this.options.logger(`${method} ${key}: ${JSON.stringify(data)}`); | ||
} | ||
generateKey(name) { | ||
return `${this.options.prefix}:${name}`; | ||
} | ||
}; | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
Options | ||
Options, | ||
RedisCacheAdapter | ||
}); |
import type { CacheAdapter } from '@mikro-orm/core'; | ||
import type { RedisCacheAdapterOptions } from './options'; | ||
export default class RedisCacheAdapter implements CacheAdapter { | ||
export declare class RedisCacheAdapter implements CacheAdapter { | ||
private readonly options; | ||
@@ -5,0 +5,0 @@ private readonly serializer; |
@@ -5,3 +5,3 @@ { | ||
"description": "", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"author": "Javien Lee <kraccoon@dimipay.io> (https://github.com/SnowMarble/)", | ||
@@ -8,0 +8,0 @@ "keywords": ["mikro-orm", "redis", "cache", "adapter"], |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10022
202