Skip to content

Commit

Permalink
structure update, helper npm script
Browse files Browse the repository at this point in the history
  • Loading branch information
havsar committed Oct 23, 2019
1 parent 9858811 commit 236a74f
Show file tree
Hide file tree
Showing 21 changed files with 156 additions and 121 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { Cache, RedisStorage, ExpirationStrategy, FsJsonStorage, MemoryStorage } from './src';
export { Cache, RedisStorage, ExpirationStrategy, FsJsonStorage, MemoryStorage } from './src'
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"description": "Simple and extensible caching module supporting decorators",
"main": "src/index.js",
"scripts": {
"test": "mocha src/*.spec.js src/**/*.spec.js",
"tdd": "mocha -w src/*.spec.js src/**/*.spec.js",
"tdd-debug-brk": "mocha --inspect-brk src/**/*.spec.js",
"test": "mocha",
"tdd": "mocha -w",
"tdd-debug-brk": "mocha --inspect-brk",
"build": "tsc -p .",
"prepublish": "tsc -p .",
"clean": "git clean -fdx src",
"prepare": "tsc -p .",
"dev": "tsc -p . -w"
},
Expand Down Expand Up @@ -43,7 +44,8 @@
"@types/bluebird": "3.5.27",
"@types/node": "10.14.17",
"@types/redis": "^2.8.13",
"bluebird": "3.5.5"
"bluebird": "3.5.5",
"redis": "^2.8.0"
},
"devDependencies": {
"@types/mocha": "5.2.5",
Expand Down
6 changes: 3 additions & 3 deletions src/CacheDecorator.ts → src/decorator/cache.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IKeyStrategy } from './strategies/key/IKeyStrategy'
import { AbstractBaseStrategy } from './strategies/caching/AbstractBaseStrategy'
import { JSONStringifyKeyStrategy } from './strategies/key/JSONStringifyStrategy'
import { JSONStringifyKeyStrategy } from '../strategy/key/json.stringify.strategy'
import { AbstractBaseStrategy } from '../strategy/caching/abstract.base.strategy'
import { IKeyStrategy } from '..'

const defaultKeyStrategy = new JSONStringifyKeyStrategy()

Expand Down
16 changes: 8 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {RedisStorage} from './storages/RedisStorage';
import {FsJsonStorage} from './storages/FsJsonStorage';
import {MemoryStorage} from './storages/MemoryStorage';
import {ExpirationStrategy} from './strategies/caching/ExpirationStrategy';
import {Cache} from './CacheDecorator';
import {IKeyStrategy} from './strategies/key/IKeyStrategy';
import {ICacheStrategy} from './strategies/caching/ICacheStrategy';
import { RedisStorage } from './storage/redis.storage'
import { FsJsonStorage } from './storage/fs.json.storage'
import { MemoryStorage } from './storage/memory.storage'
import { ExpirationStrategy } from './strategy/caching/expiration.strategy'
import { IKeyStrategy } from './strategy/key/key.strategy.types'
import { ICacheStrategy } from './strategy/caching/cache.strategy.types'
import { Cache } from './decorator/cache.decorator'

export {Cache, ExpirationStrategy, MemoryStorage, FsJsonStorage, RedisStorage, IKeyStrategy, ICacheStrategy};
export { Cache, ExpirationStrategy, MemoryStorage, FsJsonStorage, RedisStorage, IKeyStrategy, ICacheStrategy }
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { IStorage } from './IStorage'
import { StorageTypes } from './storage.types'
import * as Bluebird from 'bluebird'

const Fs = Bluebird.promisifyAll(require('fs'))

export class FsJsonStorage implements IStorage {
export class FsJsonStorage implements StorageTypes {

constructor(public jsonFilePath: string) {
if (!Fs.existsSync(this.jsonFilePath)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IStorage } from './IStorage'
import { StorageTypes } from './storage.types'

export class MemoryStorage implements IStorage {
export class MemoryStorage implements StorageTypes {

private memCache: any = {}

Expand Down
6 changes: 3 additions & 3 deletions src/storages/RedisStorage.ts → src/storage/redis.storage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IStorage } from './IStorage'
import { StorageTypes } from './storage.types'
import * as Bluebird from 'bluebird'
import * as Redis from 'redis'
import { ClientOpts } from 'redis'
Expand All @@ -7,7 +7,7 @@ import { RedisClient } from '../custom'
Bluebird.promisifyAll(Redis.RedisClient.prototype)
Bluebird.promisifyAll(Redis.Multi.prototype)

export class RedisStorage implements IStorage {
export class RedisStorage implements StorageTypes {

private client: RedisClient

Expand Down Expand Up @@ -37,4 +37,4 @@ export class RedisStorage implements IStorage {
public async clear(): Promise<void> {
return this.client.flushdbAsync()
}
}
}
4 changes: 2 additions & 2 deletions src/storages/IStorage.ts → src/storage/storage.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ interface ICacheEntry {
meta: any;
}

export interface IStorage {
export interface StorageTypes {
getItem<T>(key: string): Promise<T>;

setItem(key: string, content: ICacheEntry): Promise<void>;

clear(): Promise<void>;
}
}
60 changes: 0 additions & 60 deletions src/strategies/caching/ExpirationStrategy.spec.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { IStorage } from '../../storages/IStorage';
import { ICacheStrategy } from './ICacheStrategy';
import { StorageTypes } from '../../storage/storage.types'
import { ICacheStrategy } from './cache.strategy.types'

export abstract class AbstractBaseStrategy implements ICacheStrategy {

constructor(protected storage: IStorage) { }
constructor(protected storage: StorageTypes) {
}

public async abstract getItem<T>(key: string): Promise<T>;

public async abstract setItem(key: string, content: any, options: any): Promise<void>;

public async abstract clear(): Promise<void>;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export interface ICacheStrategy {
getItem<T>(key: string): Promise<T>;

setItem(key: string, content: any, options: any): Promise<void>;

clear(): Promise<void>;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IStorage } from '../../storages/IStorage';
import { AbstractBaseStrategy } from './AbstractBaseStrategy';
import { StorageTypes } from '../../storage/storage.types'
import { AbstractBaseStrategy } from './abstract.base.strategy'

interface IExpiringCacheItem {
content: any;
Expand All @@ -17,48 +17,48 @@ interface IOptions {

export class ExpirationStrategy extends AbstractBaseStrategy {

constructor(storage: IStorage) {
super(storage);
constructor(storage: StorageTypes) {
super(storage)
}

public async getItem<T>(key: string): Promise<T> {
const item = await this.storage.getItem<IExpiringCacheItem>(key);
const item = await this.storage.getItem<IExpiringCacheItem>(key)
if (item && item.meta && item.meta.ttl && this.isItemExpired(item)) {
await this.storage.setItem(key, undefined);
return undefined;
await this.storage.setItem(key, undefined)
return undefined
}
return item ? item.content : undefined;
return item ? item.content : undefined
}

public async setItem(key: string, content: any, options: IOptions): Promise<void> {
options = { ttl: 60, isLazy: true, isCachedForever: false, ...options }
options = {ttl: 60, isLazy: true, isCachedForever: false, ...options}

let meta = {};
let meta = {}

if (!options.isCachedForever) {
meta = {
ttl: options.ttl * 1000,
createdAt: Date.now()
};
}

if (!options.isLazy) {
setTimeout(() => {
this.unsetKey(key);
}, options.ttl);
this.unsetKey(key)
}, options.ttl)
}
}
await this.storage.setItem(key, {meta, content});
await this.storage.setItem(key, {meta, content})
}

public async clear(): Promise<void> {
await this.storage.clear();
await this.storage.clear()
}

private isItemExpired(item: IExpiringCacheItem): boolean {
return Date.now() > item.meta.createdAt + item.meta.ttl;
return Date.now() > item.meta.createdAt + item.meta.ttl
}

private async unsetKey(key: string): Promise<void> {
await this.storage.setItem(key, undefined);
await this.storage.setItem(key, undefined)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { IKeyStrategy } from './IKeyStrategy'
import { IKeyStrategy } from '../../index'

export class JSONStringifyKeyStrategy implements IKeyStrategy {
class JSONStringifyKeyStrategy implements IKeyStrategy {
public getKey(className: string, methodName: string, args: any[]): Promise<string> | string {
return `${className}:${methodName}:${JSON.stringify(args)}`
}
}

export { JSONStringifyKeyStrategy }
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export interface IKeyStrategy {
interface IKeyStrategy {
getKey(className: string, methodName: string, args: any[]): Promise<string> | string;
}
}

export { IKeyStrategy }
5 changes: 1 addition & 4 deletions src/CacheDecorator.spec.ts → test/cache.decorator.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { MemoryStorage } from './storages/MemoryStorage'
import { ExpirationStrategy } from './strategies/caching/ExpirationStrategy'
import * as Assert from 'assert'
import { Cache } from './CacheDecorator'
import { IKeyStrategy } from './strategies/key/IKeyStrategy'
import { Cache, ExpirationStrategy, IKeyStrategy, MemoryStorage } from '../src'

const strategy = new ExpirationStrategy(new MemoryStorage())
const data = ['user', 'max', 'test']
Expand Down
Loading

0 comments on commit 236a74f

Please sign in to comment.