From 1f406021f1c5f3d8c25b462eff76667f03002cb7 Mon Sep 17 00:00:00 2001 From: kaperskyguru Date: Sat, 21 Mar 2020 01:15:22 +0100 Subject: [PATCH] Removing static and fixing bugs --- package.json | 2 +- providers/CacheProvider.ts | 2 +- src/CacheLoader.ts | 14 ++++++++------ src/Consumers/Cache.ts | 33 +++++++++++---------------------- 4 files changed, 21 insertions(+), 30 deletions(-) diff --git a/package.json b/package.json index 6ef335e..54564ba 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kap-adonis-cache", - "version": "2.0.1", + "version": "2.0.2", "description": "Adonis Cache Package", "main": "build/providers/CacheProvider", "scripts": { diff --git a/providers/CacheProvider.ts b/providers/CacheProvider.ts index 965f930..634f6a1 100644 --- a/providers/CacheProvider.ts +++ b/providers/CacheProvider.ts @@ -7,7 +7,7 @@ class CacheProvider extends ServiceProvider { this.app.singleton("Kaperskyguru/Adonis-Cache", app => { // const Config = app.use("Adonis/Src/Config"); const CacheLoader = require("../src/CacheLoader"); - return new CacheLoader(app); + return new CacheLoader(app).createCache(); }); this.app.alias("Kaperskyguru/Adonis-Cache", "Cache"); diff --git a/src/CacheLoader.ts b/src/CacheLoader.ts index c22a328..0fe6993 100644 --- a/src/CacheLoader.ts +++ b/src/CacheLoader.ts @@ -11,7 +11,6 @@ class CacheLoader { constructor(App: any) { this.app = App; this._setConfig("driver", this.cacheDriver); - this.initialize(this._getConfig("driver")); } _getConfig(name: String) { return this.app.use("Adonis/Src/Config").get(`cache.${name}`); @@ -23,28 +22,31 @@ class CacheLoader { } this.app.use("Adonis/Src/Config").set(`cache.${name}`, value); } - initialize(driver: String) { + createCache() { + const driver: string = this._getConfig("driver"); + let cache: any; switch (driver.toLowerCase()) { case "memcache": // Load MemCacheService - new Cache(new MemCacheService(this.app)); + cache = new Cache(new MemCacheService(this.app)); break; case "redis": // Load RedisCacheService - new Cache(new RedisCacheService(this.app)); + cache = new Cache(new RedisCacheService(this.app)); break; case "database": // Load DatabaseCacheService - new Cache(new DatabaseCacheService(this.app)); + cache = new Cache(new DatabaseCacheService(this.app)); break; default: // Load FileCacheService - new Cache(new FileCacheService(this.app)); + cache = new Cache(new FileCacheService(this.app)); break; } + return cache; } } diff --git a/src/Consumers/Cache.ts b/src/Consumers/Cache.ts index 863b61e..facf63c 100644 --- a/src/Consumers/Cache.ts +++ b/src/Consumers/Cache.ts @@ -2,12 +2,12 @@ import ServiceInterface from "../Contracts/ServiceInterface"; // @Implements() class Cache { - private static CacheService: ServiceInterface; + private CacheService: ServiceInterface; constructor(cacheService: ServiceInterface) { - Cache.CacheService = cacheService; + this.CacheService = cacheService; } - public static async get(name: String): Promise { + public async get(name: string): Promise { if (name) { const value = await this.CacheService.get(name); if (value) { @@ -16,7 +16,7 @@ class Cache { } } - public static async has(name: String): Promise { + public async has(name: string): Promise { const value = await this.CacheService.get(name); if (value == null) { return false; @@ -24,18 +24,14 @@ class Cache { return true; } - public static async set( - name: String, - data: any, - duration: Number - ): Promise { + public async set(name: string, data: any, duration: number): Promise { if (name && data) { data = JSON.stringify(data); return await this.CacheService.set(name, data, duration); } } - public static async delete(name: String): Promise { + public async delete(name: string): Promise { if (await this.has(name)) { await this.CacheService.delete(name); return true; @@ -43,18 +39,14 @@ class Cache { return false; } - public static async update( - name: String, - data: any, - duration: number - ): Promise { + public async update(name: string, data: any, duration: number): Promise { if (await this.has(name)) { await this.delete(name); return await this.set(name, data, duration); } else return await this.set(name, data, duration); } - public static async remember( + public async remember( name: string, duration: number, callback: Function @@ -68,10 +60,7 @@ class Cache { } } - public static async rememberForever( - name: string, - callback: Function - ): Promise { + public async rememberForever(name: string, callback: Function): Promise { if (await this.has(name)) { return await this.get(name); } else { @@ -81,7 +70,7 @@ class Cache { } } - public static async many(keys: Array): Promise { + public async many(keys: Array): Promise { let values = Promise.all(keys.map((key: string) => this.get(key))); let mappedValues: object = {}; for (let index: number = 0; index < keys.length; index++) { @@ -90,7 +79,7 @@ class Cache { return mappedValues; } - public static async setMany(data: object, minutes: number) { + public async setMany(data: object, minutes: number) { for (let prop in data) { await this.set(prop, data[prop], minutes); }