@dazejs/framework
Version:
Daze.js - A powerful web framework for Node.js
81 lines • 2.69 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Cache = void 0;
const store_1 = require("./store");
const helpers_1 = require("../../helpers");
const fake_base_class_1 = require("../../utils/fake-base-class");
class Cache extends (0, fake_base_class_1.fakeBaseClass)() {
constructor() {
super();
this.stores = new Map();
return new Proxy(this, this.proxy);
}
get proxy() {
return {
get(target, p, receiver) {
if (typeof p === 'string') {
if (Reflect.has(target, p)) {
return Reflect.get(target, p, receiver);
}
return target.store()[p];
}
return Reflect.get(target, p, receiver);
}
};
}
store(store, connection) {
const _store = store !== null && store !== void 0 ? store : this.getDefaultStore();
switch (_store) {
case 'redis':
return this.createRedisStore(connection);
case 'fs':
return this.createFsStore();
default:
return this.createMemoryStore();
}
}
getDefaultStore() {
if (!this.defaultStore) {
this.defaultStore = (0, helpers_1.config)('cache.store', 'memory');
}
return this.defaultStore;
}
getDefaultRedisConnection() {
if (!this.defaultConnection) {
if ((0, helpers_1.config)().has('redis.cache')) {
this.defaultConnection = 'cache';
}
else {
this.defaultConnection = 'default';
}
}
return this.defaultConnection;
}
createMemoryStore() {
const key = `memory`;
if (!this.stores.has(key)) {
const store = new store_1.MemoryCacheStore();
this.stores.set(key, store);
}
return this.stores.get(key);
}
createFsStore() {
const key = `fs`;
if (!this.stores.has(key)) {
const store = new store_1.FsCacheStore();
this.stores.set(key, store);
}
return this.stores.get(key);
}
createRedisStore(connection) {
const _connection = connection !== null && connection !== void 0 ? connection : this.getDefaultRedisConnection();
const key = `redis.${_connection}`;
if (!this.stores.has(key)) {
const store = new store_1.RedisCacheStore(_connection);
this.stores.set(key, store);
}
return this.stores.get(key);
}
}
exports.Cache = Cache;
//# sourceMappingURL=cache.js.map