tspace-mysql
Version:
Tspace MySQL is a promise-based ORM for Node.js, designed with modern TypeScript and providing type safety for schema databases.
76 lines • 2.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RedisCache = void 0;
const Package_1 = require("../Package");
class RedisCache {
_redis = Package_1.Package.redis;
client;
constructor(url) {
this.client = this._redis.createClient({
socket: {
reconnectStrategy: () => false
},
url,
});
try {
this.client.on("error", (err) => {
console.log(`\n\x1b[31mERROR: Redis error failed caused by '${err.message}'\x1b[0m`);
});
this.client.connect().catch((err) => {
console.log(`\n\x1b[31mERROR: Redis connection failed caused by '${err.message}'\x1b[0m`);
});
}
catch (err) {
console.log(`\n\x1b[31mERROR: Redis catch caused by '${err.message}'. Please try installing the package using : npm install redis@5.6.0 --save\x1b[0m`);
}
}
provider() {
return "redis";
}
async all() {
const cacheds = await this.client.keys("*");
const values = [];
for (const cached in cacheds) {
values.push(cached == null ? null : this._safetyJsonParse(cached));
}
return values;
}
async exists(key) {
const cached = await this.client.get(key);
return cached != null;
}
async get(key) {
const cached = await this.client.get(key);
if (cached == null)
return null;
return this._safetyJsonParse(cached);
}
async set(key, value, ms) {
await this.client.set(key, JSON.stringify(value), { PX: ms });
return;
}
async clear() {
await this.client.flushAll();
return;
}
async delete(key) {
await this.client.del(key);
return;
}
_safetyJsonParse(value) {
if (typeof value !== 'string')
return value;
const v = value.trim();
if (!v || (v[0] !== '{' && v[0] !== '['))
return value;
try {
return JSON.parse(v);
}
catch {
return value;
}
}
}
exports.RedisCache = RedisCache;
exports.default = RedisCache;
//# sourceMappingURL=RedisCache.js.map