tspace-mysql
Version:
Tspace MySQL is a promise-based ORM for Node.js, designed with modern TypeScript and providing type safety for schema databases.
87 lines • 2.9 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RedisCache = void 0;
const tools_1 = require("../../tools");
class RedisCache {
constructor(url) {
this._redis = tools_1.Tool.import('redis');
this.client = this._redis.createClient({
url
});
this.client.on("error", (err) => {
if (err) {
console.log(err);
this.client.quit();
}
});
this.client.connect();
}
provider() {
return 'redis';
}
all() {
return __awaiter(this, void 0, void 0, function* () {
const cacheds = yield this.client.keys('*');
const values = [];
for (const cached in cacheds) {
values.push(cached == null
? null
: this._safetyJsonParse(cached));
}
return values;
});
}
exists(key) {
return __awaiter(this, void 0, void 0, function* () {
const cached = yield this.client.get(key);
return cached != null;
});
}
get(key) {
return __awaiter(this, void 0, void 0, function* () {
const cached = yield this.client.get(key);
if (cached == null)
return null;
return this._safetyJsonParse(cached);
});
}
set(key, value, ms) {
return __awaiter(this, void 0, void 0, function* () {
const expiredAt = Date.now() + ms;
yield this.client.setEx(key, expiredAt, JSON.stringify(value));
return;
});
}
clear() {
return __awaiter(this, void 0, void 0, function* () {
yield this.client.flushAll();
return;
});
}
delete(key) {
return __awaiter(this, void 0, void 0, function* () {
yield this.client.del(key);
return;
});
}
_safetyJsonParse(value) {
try {
return JSON.parse(value);
}
catch (e) {
return value;
}
}
}
exports.RedisCache = RedisCache;
exports.default = RedisCache;
//# sourceMappingURL=RedisCache.js.map