@melchyore/adonis-cache
Version:
Cache package for AdonisJS V5
165 lines (164 loc) • 6.31 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const manager_1 = require("@poppinss/manager");
const utils_1 = require("@poppinss/utils");
const Repository_1 = __importDefault(require("./Repository"));
const Memcached_1 = __importDefault(require("./Stores/Memcached"));
const InMemory_1 = __importDefault(require("./Stores/InMemory"));
const Redis_1 = __importDefault(require("./Stores/Redis"));
const DynamoDB_1 = __importDefault(require("./Stores/DynamoDB"));
const Database_1 = __importDefault(require("./Stores/Database"));
const File_1 = __importDefault(require("./Stores/File"));
class CacheManager extends manager_1.Manager {
constructor(app, config) {
super(app);
this.app = app;
this.config = config;
/**
* Cache all stores instances.
*/
this.singleton = true;
/**
* Find if cache is ready to be used
*/
this.isReady = false;
this.emitter = this.app.container.use('Adonis/Core/Event');
this.validateConfig();
}
createInMemory(_, __) {
return new InMemory_1.default();
}
createMemcached(_, __) {
return new Memcached_1.default(this.app.container.use('Adonis/Addons/Adonis5-MemcachedClient'));
}
createRedis(_, config) {
const AdonisRedis = this.app.container.use('Adonis/Addons/Redis');
const redisConnection = this.app.container.use('Adonis/Core/Env').get('REDIS_CONNECTION');
return new Redis_1.default(AdonisRedis, config.connection ?? redisConnection);
}
createDynamodb(_, config) {
return new DynamoDB_1.default(this.app.container.use('Adonis/Addons/DynamoDB').DynamoDB, config.table);
}
createDatabase(_, config) {
const Container = this.app.container;
return new Database_1.default(Container.use('Adonis/Lucid/Database').connection(config.connection ?? Container.use('Adonis/Core/Env').get('DB_CONNECTION')), config.table);
}
createFile(_, config) {
const disk = config.disk;
const driveConfig = this.app.container.use('Adonis/Core/Config').get('drive').disks[disk];
if (!driveConfig) {
throw new Error(`You must create and add the '${config.disk}' disk using local driver to the disks object inside config/drive.ts\n` +
`${config.disk}: {\n
driver: 'local',\n
visibility: 'private',\n
root: Application.tmpPath('the-path-you-want-to-use'),\n
},`);
}
return new File_1.default(this.app.container.use('Adonis/Core/Drive').use(disk));
}
use(store) {
if (!this.isReady) {
throw new utils_1.Exception('Missing configuration for cache. Visit https://github.com/Melchyore/adonis-cache for setup instructions', 500, 'E_MISSING_CACHE_CONFIG');
}
return super.use(store ?? this.getDefaultMappingName());
}
async get(key, fallback) {
return await this.use().get(key, fallback);
}
async many(keys) {
return await this.use().many(keys);
}
async add(key, value, ttl) {
return await this.use().add(key, value, ttl);
}
async put(key, value, ttl) {
return await this.use().put(key, value, ttl);
}
async set(key, value, ttl) {
return await this.use().set(key, value, ttl);
}
async increment(key, value = 1) {
return await this.use().increment(key, value);
}
async decrement(key, value = 1) {
return await this.use().decrement(key, value);
}
async has(key) {
return await this.use().has(key);
}
async missing(key) {
return await this.use().missing(key);
}
async putMany(list, ttl) {
return await this.use().putMany(list, ttl);
}
async putManyForever(list) {
return await this.use().putManyForever(list);
}
async forever(key, value) {
return await this.use().forever(key, value);
}
async pull(key) {
return await this.use().pull(key);
}
async remember(key, ttl, closure) {
return await this.use().remember(key, ttl, closure);
}
async sear(key, closure) {
return await this.use().sear(key, closure);
}
async rememberForever(key, closure) {
return await this.use().rememberForever(key, closure);
}
async forget(key) {
return await this.use().forget(key);
}
async forgetMultiple(keys) {
return await this.use().forgetMultiple(keys);
}
async flush() {
return await this.use().flush();
}
async clear() {
return await this.use().clear();
}
tags(names) {
return this.use().tags(names);
}
getDefaultMappingName() {
if (!this.config.store) {
throw new utils_1.Exception('Invalid "cache" config. Missing value for "store". Make sure to set it inside the "config/cache" file');
}
return this.config.store;
}
getMappingConfig(mappingName) {
return this.config.stores[mappingName];
}
getMappingDriver(mappingName) {
return this.getMappingConfig(mappingName)?.driver;
}
/**
* Since we don't expose the drivers instances directly, we wrap them
* inside the repository instance.
*/
wrapDriverResponse(mappingName, driver) {
const driverConfig = this.getMappingConfig(mappingName);
const repository = new Repository_1.default(driver, this.getPrefix(driverConfig));
repository.setConfig(this.config, driverConfig);
repository.setEventDispatcher(this.emitter);
return repository;
}
getPrefix(config) {
return config.prefix ?? this.config.prefix;
}
validateConfig() {
const validator = new utils_1.ManagerConfigValidator(this.config, 'cache', 'config/cache');
validator.validateDefault('store');
validator.validateList('stores', 'store');
this.isReady = true;
}
}
exports.default = CacheManager;