-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace factory functions to class rename CacheManager to CacheProvider remove keysStorageIn MemoryStorage (now is using keys from map)
- Loading branch information
1 parent
1299d6c
commit 7c0c11d
Showing
14 changed files
with
163 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
lib/cache/cacheManager/CacheManager.ts → lib/cache/cacheProvider/CacheProvider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { ClassType } from '../../interfaces/class'; | ||
import { Cache } from '../caches/Cache'; | ||
|
||
export interface CacheManager<K = any> { | ||
export interface CacheProvider<K = any> { | ||
get(instance: ClassType): Cache<K>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Cache } from '../caches/Cache'; | ||
import { CacheFactory } from '../caches/factory'; | ||
import { CacheProvider } from './CacheProvider'; | ||
|
||
export class ClassCacheProvider<K = any> implements CacheProvider<K> { | ||
|
||
private cache: Cache<K> = null; | ||
|
||
constructor(private readonly cacheFactory: CacheFactory) { } | ||
|
||
public get(): Cache<K> { | ||
if (!this.cache) { | ||
this.cache = this.cacheFactory.create(); | ||
} | ||
|
||
return this.cache; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { ClassType } from '../../interfaces/class'; | ||
import { Cache } from '../caches/Cache'; | ||
import { CacheFactory } from '../caches/factory'; | ||
import { CacheProvider } from './CacheProvider'; | ||
|
||
export class InstanceCacheProvider<K = any> implements CacheProvider<K> { | ||
|
||
private instanceCaches = new WeakMap<ClassType, Cache<K>>(); | ||
|
||
constructor(private readonly cacheFactory: CacheFactory) { } | ||
|
||
public get(instance: ClassType): Cache<K> { | ||
const hasCache = !this.instanceCaches.has(instance); | ||
if (hasCache) { | ||
const cache = this.cacheFactory.create(); | ||
this.instanceCaches.set(instance, cache); | ||
} | ||
|
||
return this.instanceCaches.get(instance); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Factory } from '../../interfaces/factory'; | ||
import { CacheFactory } from '../caches/factory'; | ||
import { CacheProvider } from './CacheProvider'; | ||
import { ClassCacheProvider } from './ClassCacheProvider'; | ||
import { InstanceCacheProvider } from './InstanceCacheProvider'; | ||
|
||
export class CacheProviderFactory implements Factory<CacheProvider> { | ||
|
||
constructor( | ||
private readonly scope: 'class' | 'instance', | ||
private readonly cacheFactory: CacheFactory, | ||
) { } | ||
|
||
public create() { | ||
switch (this.scope) { | ||
case 'class': | ||
return this.classCacheProvider(); | ||
|
||
case 'instance': | ||
return this.instanceCacheProvider(); | ||
|
||
default: | ||
throw new Error(`@cache invalid scope option: ${this.scope}.`); | ||
} | ||
} | ||
|
||
private classCacheProvider(): ClassCacheProvider { | ||
return new ClassCacheProvider(this.cacheFactory); | ||
} | ||
|
||
private instanceCacheProvider(): InstanceCacheProvider { | ||
return new InstanceCacheProvider(this.cacheFactory); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,22 @@ | ||
import { Factory } from '../../interfaces/factory'; | ||
import { HashService } from '../../utils/hash'; | ||
import { CacheOptions } from '../CacheOptions'; | ||
import { expirationFactory } from '../expirations/factory'; | ||
import { storageFactory } from '../storages/factory'; | ||
import { ExpirationFactory } from '../expirations/factory'; | ||
import { StorageFactory } from '../storages/factory'; | ||
import { Cache } from './Cache'; | ||
|
||
export function cacheFactory<K = any>(timeout: number, options: CacheOptions): Cache<K> { | ||
const storage = storageFactory(options); | ||
const expiration = expirationFactory(timeout, options); | ||
const hash = new HashService(); | ||
export class CacheFactory<K = any> implements Factory<Cache<K>> { | ||
|
||
constructor( | ||
private readonly hash: HashService, | ||
private readonly expirationFactory: ExpirationFactory, | ||
private readonly storageFactory: StorageFactory, | ||
) { } | ||
|
||
public create(): Cache<K> { | ||
const storage = this.storageFactory.create(); | ||
const expiration = this.expirationFactory.create(); | ||
|
||
return new Cache(storage, expiration, this.hash); | ||
} | ||
|
||
return new Cache<K>(storage, expiration, hash); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,34 @@ | ||
import { CacheOptions } from '..'; | ||
import { Factory } from '../../interfaces/factory'; | ||
import { AbsoluteExpiration } from './AbsoluteExpiration'; | ||
import { Expiration } from './Expiration'; | ||
import { SlidingExpiration } from './SlidingExpiration'; | ||
|
||
const expirationFactories: ReadonlyMap<'absolute' | 'sliding', (timeout: number) => Expiration> = | ||
new Map<'absolute' | 'sliding', (timeout: number) => Expiration>() | ||
.set('absolute', timeout => new AbsoluteExpiration(timeout)) | ||
.set('sliding', timeout => new SlidingExpiration(timeout)); | ||
export class ExpirationFactory implements Factory<Expiration> { | ||
|
||
export function expirationFactory(timeout: number, options: CacheOptions): Expiration { | ||
const { expiration } = options; | ||
constructor( | ||
private readonly timeout: number, | ||
private readonly expiration: 'absolute' | 'sliding', | ||
) { } | ||
|
||
const factory = expirationFactories.get(expiration); | ||
public create(): Expiration { | ||
switch (this.expiration) { | ||
case 'absolute': | ||
return this.absoluteExpirtation(); | ||
|
||
if (!factory) { | ||
throw new Error(`@cache Expiration type is not supported: ${expiration}.`); | ||
case 'sliding': | ||
return this.slidingExpiration(); | ||
|
||
default: | ||
throw new Error(`@cache Expiration type is not supported: ${this.expiration}.`); | ||
} | ||
} | ||
|
||
private absoluteExpirtation(): AbsoluteExpiration { | ||
return new AbsoluteExpiration(this.timeout); | ||
} | ||
|
||
private slidingExpiration(): SlidingExpiration { | ||
return new SlidingExpiration(this.timeout); | ||
} | ||
|
||
return factory(timeout); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,26 @@ | ||
import { CacheOptions } from '..'; | ||
import { Factory } from '../../interfaces/factory'; | ||
import { MemoryStorage } from './MemoryStorage'; | ||
import { Storage } from './Storage'; | ||
|
||
const storeFactories: ReadonlyMap<'memory', (limit: number) => Storage> = | ||
new Map<'memory', (limit: number) => Storage>() | ||
.set('memory', limit => new MemoryStorage(limit)); | ||
export class StorageFactory implements Factory<Storage> { | ||
|
||
export function storageFactory(options: CacheOptions): Storage { | ||
const { size, storage } = options; | ||
constructor( | ||
private readonly limit: number, | ||
private readonly storage: 'memory', | ||
) { } | ||
|
||
const factory = storeFactories.get(storage); | ||
public create(): Storage { | ||
switch (this.storage) { | ||
case 'memory': | ||
return this.memoryStorage(); | ||
|
||
if (!factory) { | ||
throw new Error(`@cache Storage type is not supported: ${storage}.`); | ||
default: | ||
throw new Error(`@cache Storage type is not supported: ${this.storage}.`); | ||
} | ||
} | ||
|
||
private memoryStorage(): MemoryStorage { | ||
return new MemoryStorage(this.limit); | ||
} | ||
|
||
return factory(size); | ||
} |
Oops, something went wrong.