tspace-mysql
Version:
Tspace MySQL is a promise-based ORM for Node.js, designed with modern TypeScript and providing type safety for schema databases.
118 lines • 3.1 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TCache = exports.Cache = void 0;
const MemoryCache_1 = require("./MemoryCache");
const DBCache_1 = require("./DBCache");
const RedisCache_1 = require("./RedisCache");
const options_1 = __importDefault(require("../../options"));
class Cache {
_driver = new MemoryCache_1.MemoryCache();
constructor() {
this._chooseDriver(options_1.default.CACHE);
return this;
}
provider() {
return this._driver.provider();
}
/**
* The 'driver' method is used to pick a driver for the cache
*
* @param {string} driver 'db' | 'memory' | 'redis'
* @returns {this} this
*/
driver(driver) {
return this._chooseDriver(driver);
}
/**
* The 'all' method is used get all cache
*
* @returns {Promise<array>} array
*/
async all() {
return await this._driver.all();
}
/**
* The 'exists' method is used get all cache
*
* @param {string} key
* @returns {Promise<array>} array
*/
async exists(key) {
try {
return await this._driver.exists(key);
}
catch (e) {
return false;
}
}
/**
* The 'get' method is used get cache by key
* @param {string} key
* @returns {any} any
*/
async get(key) {
try {
const cache = await this._driver.get(key);
if (cache == null || cache === '')
return null;
return cache;
}
catch (e) {
return null;
}
}
/**
* The 'set' method is used set the cache
*
* @param {string} key
* @param {unknown} value
* @param {number} ms
* @returns {Promise<void>} void
*/
async set(key, value, ms) {
try {
await this._driver.set(key, value, ms);
return;
}
catch (e) {
return;
}
}
/**
* The 'clear' method is used clear all cache
*
* @returns {Promise<void>} void
*/
async clear() {
return await this._driver.clear();
}
/**
* The 'clear' method is used delete cache by key
*
* @returns {Promise<void>} void
*/
async delete(key) {
await this._driver.delete(key);
return;
}
_chooseDriver(driver) {
if (driver === 'db') {
this._driver = new DBCache_1.DBCache();
return this;
}
if (driver != null && driver.includes('redis')) {
this._driver = new RedisCache_1.RedisCache(String(options_1.default.CACHE));
return this;
}
this._driver = new MemoryCache_1.MemoryCache();
return this;
}
}
exports.TCache = Cache;
const cacheInstance = new Cache();
exports.Cache = cacheInstance;
exports.default = cacheInstance;
//# sourceMappingURL=index.js.map