Skip to content

Commit

Permalink
Remove module exports
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaperskyguru committed Mar 13, 2020
1 parent ec6ff2a commit 9895940
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 149 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ database/development.sqlite
.idea

TODO

build
20 changes: 15 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
{
"name": "kap-adonis-cache",
"version": "1.0.7",
"version": "1.0.8",
"description": "Adonis Cache Package",
"main": "providers/CacheProvider",
"main": "build/providers/CacheProvider",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"mrm": "mrm --preset=@adonisjs/mrm-preset",
"clean": "del build",
"compile": " npm run clean && tsc",
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc"
},
"keywords": [
"adonis",
Expand All @@ -24,7 +28,13 @@
"ts-node": "^8.6.2",
"typescript": "^3.8.3",
"mrm": "^2.1.0",
"reflect-metadata": "^0.1.13"
"reflect-metadata": "^0.1.13",
"@adonisjs/mrm-preset": "^2.2.4"
},
"dependencies": {}
"dependencies": {},
"files": [
"build/providers",
"build/src",
"build/config"
]
}
16 changes: 0 additions & 16 deletions providers/CacheProvider.js

This file was deleted.

17 changes: 17 additions & 0 deletions providers/CacheProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use strict";

import { IocContract } from "@adonisjs/fold";

export class CacheProvider {
constructor(protected $container: IocContract) {}

register() {
this.$container.singleton("Kaperskyguru/Adonis-Cache", () => {
const Config = this.$container.use("Adonis/Src/Config");
const CacheLoader = require("../src/CacheLoader");
return new CacheLoader(Config);
});

this.$container.alias("Kaperskyguru/Adonis-Cache", "Cache");
}
}
4 changes: 2 additions & 2 deletions src/CacheLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ const { MemCacheService } = require("../src/Services/MemCacheService");
class CacheLoader {
cacheDriver = "file";
private config: any;
constructor(Config) {
constructor(Config: any) {
this.config = Config.merge("cache", {
cacheDriver: this.cacheDriver
});
this.initialize(this.config);
}

initialize(config) {
initialize(config: any) {
switch (config.cacheDriver.toLowerCase()) {
case "memcache":
// Load MemCacheService
Expand Down
21 changes: 13 additions & 8 deletions src/Consumers/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Kache implements CacheInterface {
this.CacheEngine = CacheEngine;
}

public async get(name) {
public async get(name: String): Promise<any> {
if (name) {
const value = await this.CacheEngine.get(name);
if (value) {
Expand All @@ -17,36 +17,41 @@ class Kache implements CacheInterface {
}
}

public async has(name) {
public async has(name: String): Promise<Boolean> {
const value = await this.CacheEngine.get(name);
if (value == null) {
return false;
}
return true;
}

public async set(name, data, duration) {
public async set(name: String, data: any, duration: Number): Promise<any> {
if (name && data) {
data = JSON.stringify(data);
return await this.CacheEngine.set(name, data, duration);
}
}

public async delete(name) {
public async delete(name: String): Promise<Boolean> {
if (await this.has(name)) {
await this.CacheEngine.delete(name);
return true;
}
return false;
}

public async update(name, data, duration) {
public async update(name: String, data: any, duration: Number): Promise<any> {
if (await this.has(name)) {
await this.delete(name);
return await this.set(name, data, duration);
} else return await this.set(name, data, duration);
}

async remember(name, duration, callback) {
async remember(
name: String,
duration: Number,
callback: Function
): Promise<any> {
if (await this.has(name)) {
return await this.get(name);
} else {
Expand All @@ -56,12 +61,12 @@ class Kache implements CacheInterface {
}
}

public async rememberForever(name, callback) {
public async rememberForever(name: String, callback: Function): Promise<any> {
if (await this.has(name)) {
return await this.get(name);
} else {
const data = await callback();
await this.set(name, data, null);
await this.set(name, data, 5888888888888);
return data;
}
}
Expand Down
19 changes: 14 additions & 5 deletions src/Engines/DatabaseCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@
class DatabaseCache {
private defaultMinutes = 60;

constructor(config) {}
public async get(name) {
constructor(config: any) {
console.log(config);
}
public async get(name: String): Promise<any> {
if (name) {
// Implement Database get here
console.log(name);
const value = "";
if (value) {
return JSON.parse(value);
}
}
}

public async set(name, data, duration = this.defaultMinutes) {
public async set(
name: String,
data: any,
duration: Number = this.defaultMinutes
): Promise<any> {
if (name && data) {
// Implement Set method
console.log(name, data, duration);
// data = JSON.stringify(data);
// if (duration == null) {
// return await this._addCache(name, data);
Expand All @@ -25,10 +33,11 @@ class DatabaseCache {
}
}

public async delete(name) {
public async delete(name: String): Promise<any> {
// Implement Delete function
console.log(name);

return true;
}
}
module.exports = DatabaseCache;
export default DatabaseCache;
24 changes: 16 additions & 8 deletions src/Engines/FileCache.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
// const DatabaseCache = use("DatabaseCache");
// const FileCache = use("FileCache");

class FileCache {
private defaultMinutes = 60;

constructor(config) {}
public async get(name) {
constructor(config: any) {
console.log(config);
}
public async get(name: String): Promise<any> {
if (name) {
// Implement Database get here
// Implement File get here
console.log(name);
const value = "";
if (value) {
return JSON.parse(value);
}
}
}

public async set(name, data, duration = this.defaultMinutes) {
public async set(
name: String,
data: any,
duration: Number = this.defaultMinutes
): Promise<any> {
if (name && data) {
console.log(name, data, duration);
// Implement Set method
// data = JSON.stringify(data);
// if (duration == null) {
Expand All @@ -25,10 +33,10 @@ class FileCache {
}
}

public async delete(name) {
public async delete(name: String): Promise<any> {
// Implement Delete function

console.log(name);
return true;
}
}
module.exports = FileCache;
export default FileCache;
24 changes: 16 additions & 8 deletions src/Engines/MemCache.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
// const DatabaseCache = use("DatabaseCache");
// const MemCache = use("MemCache");

class MemCache {
private defaultMinutes = 60;

constructor(config) {}
public async get(name) {
constructor(config: any) {
console.log(config);
}
public async get(name: String): Promise<any> {
if (name) {
// Implement Database get here
// Implement Mem get here
console.log(name);
const value = "";
if (value) {
return JSON.parse(value);
}
}
}

public async set(name, data, duration = this.defaultMinutes) {
public async set(
name: String,
data: any,
duration: Number = this.defaultMinutes
): Promise<any> {
if (name && data) {
console.log(name, data, duration);
// Implement Set method
// data = JSON.stringify(data);
// if (duration == null) {
Expand All @@ -25,10 +33,10 @@ class MemCache {
}
}

public async delete(name) {
public async delete(name: String): Promise<any> {
// Implement Delete function

console.log(name);
return true;
}
}
module.exports = MemCache;
export default MemCache;
23 changes: 16 additions & 7 deletions src/Engines/RedisCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ const { Redis } = require("@adonisjs/redis");
class RedisCache {
private defaultMinutes = 60;

constructor(config) {
constructor(config: any) {
console.log(config);
if (!Redis) throw "Cannot find Redis driver";
}

public async get(name) {
public async get(name: String): Promise<any> {
if (name) {
// Implement Database get here
const value = await Redis.get(name);
Expand All @@ -17,7 +18,11 @@ class RedisCache {
}
}

public async set(name, data, duration = this.defaultMinutes) {
public async set(
name: String,
data: any,
duration: Number = this.defaultMinutes
): Promise<any> {
if (name && data) {
// Implement Set method
data = JSON.stringify(data);
Expand All @@ -28,20 +33,24 @@ class RedisCache {
}
}

public async delete(name) {
public async delete(name: String): Promise<Boolean> {
// Implement Delete function
await Redis.del(name);
return true;
}

private async _addExpiredCache(name, data, duration) {
private async _addExpiredCache(
name: String,
data: any,
duration: Number
): Promise<any> {
await Redis.set(name, data, "EX", duration);
return data;
}

private async _addCache(name, data) {
private async _addCache(name: String, data: any): Promise<any> {
await Redis.set(name, data);
return data;
}
}
module.exports = RedisCache;
export default RedisCache;
Loading

0 comments on commit 9895940

Please sign in to comment.