Setup eslint and enforce rules
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
37
src/support/cache/CacheFactory.ts
vendored
37
src/support/cache/CacheFactory.ts
vendored
@@ -5,20 +5,21 @@ import {
|
||||
PropertyDependency,
|
||||
isInstantiable,
|
||||
DEPENDENCY_KEYS_METADATA_KEY,
|
||||
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY
|
||||
} from "../../di"
|
||||
import {Collection, ErrorWithContext} from "../../util"
|
||||
import {Logging} from "../../service/Logging";
|
||||
import {Config} from "../../service/Config";
|
||||
import {Cache} from "../../util";
|
||||
import {MemoryCache} from "./MemoryCache";
|
||||
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, StaticClass, Instantiable,
|
||||
} from '../../di'
|
||||
import {Collection, ErrorWithContext} from '../../util'
|
||||
import {Logging} from '../../service/Logging'
|
||||
import {Config} from '../../service/Config'
|
||||
import {Cache} from '../../util'
|
||||
import {MemoryCache} from './MemoryCache'
|
||||
|
||||
/**
|
||||
* Dependency container factory that matches the abstract Cache token, but
|
||||
* produces an instance of whatever Cache driver is configured in the `server.cache.driver` config.
|
||||
*/
|
||||
export class CacheFactory extends AbstractFactory {
|
||||
export class CacheFactory extends AbstractFactory<Cache> {
|
||||
protected readonly logging: Logging
|
||||
|
||||
protected readonly config: Config
|
||||
|
||||
/** true if we have printed the memory-based cache driver warning once. */
|
||||
@@ -30,17 +31,19 @@ export class CacheFactory extends AbstractFactory {
|
||||
this.config = Container.getContainer().make<Config>(Config)
|
||||
}
|
||||
|
||||
produce(dependencies: any[], parameters: any[]): Cache {
|
||||
return new (this.getCacheClass())
|
||||
produce(): Cache {
|
||||
return new (this.getCacheClass())()
|
||||
}
|
||||
|
||||
match(something: any) {
|
||||
match(something: unknown): boolean {
|
||||
return something === Cache
|
||||
}
|
||||
|
||||
getDependencyKeys(): Collection<DependencyRequirement> {
|
||||
const meta = Reflect.getMetadata(DEPENDENCY_KEYS_METADATA_KEY, this.getCacheClass())
|
||||
if ( meta ) return meta
|
||||
if ( meta ) {
|
||||
return meta
|
||||
}
|
||||
return new Collection<DependencyRequirement>()
|
||||
}
|
||||
|
||||
@@ -50,7 +53,9 @@ export class CacheFactory extends AbstractFactory {
|
||||
|
||||
do {
|
||||
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
|
||||
if ( loadedMeta ) meta.concat(loadedMeta)
|
||||
if ( loadedMeta ) {
|
||||
meta.concat(loadedMeta)
|
||||
}
|
||||
currentToken = Object.getPrototypeOf(currentToken)
|
||||
} while (Object.getPrototypeOf(currentToken) !== Function.prototype && Object.getPrototypeOf(currentToken) !== Object.prototype)
|
||||
|
||||
@@ -61,7 +66,7 @@ export class CacheFactory extends AbstractFactory {
|
||||
* Get the configured cache driver and return some Instantiable<Cache>.
|
||||
* @protected
|
||||
*/
|
||||
protected getCacheClass() {
|
||||
protected getCacheClass(): StaticClass<Cache, Instantiable<Cache>> {
|
||||
const CacheClass = this.config.get('server.cache.driver', MemoryCache)
|
||||
if ( CacheClass === MemoryCache && !CacheFactory.loggedMemoryCacheWarningOnce ) {
|
||||
this.logging.warn(`You are using the default memory-based cache driver. It is recommended you configure a persistent cache driver instead.`)
|
||||
@@ -69,9 +74,9 @@ export class CacheFactory extends AbstractFactory {
|
||||
}
|
||||
|
||||
if ( !isInstantiable(CacheClass) || !(CacheClass.prototype instanceof Cache) ) {
|
||||
const e = new ErrorWithContext('Provided session class does not extend from @extollo/lib.Cache');
|
||||
const e = new ErrorWithContext('Provided session class does not extend from @extollo/lib.Cache')
|
||||
e.context = {
|
||||
config_key: 'server.cache.driver',
|
||||
configKey: 'server.cache.driver',
|
||||
class: CacheClass.toString(),
|
||||
}
|
||||
}
|
||||
|
||||
10
src/support/cache/MemoryCache.ts
vendored
10
src/support/cache/MemoryCache.ts
vendored
@@ -1,4 +1,4 @@
|
||||
import {Cache, Collection, Awaitable} from "../../util"
|
||||
import {Cache, Collection, Awaitable} from '../../util'
|
||||
|
||||
/**
|
||||
* An in-memory implementation of the Cache.
|
||||
@@ -23,17 +23,19 @@ export class MemoryCache extends Cache {
|
||||
existing.value = value
|
||||
existing.expires = expires
|
||||
} else {
|
||||
MemoryCache.cacheItems.push({ key, value, expires })
|
||||
MemoryCache.cacheItems.push({ key,
|
||||
value,
|
||||
expires })
|
||||
}
|
||||
}
|
||||
|
||||
public has(key: string): Awaitable<boolean> {
|
||||
const now = new Date()
|
||||
return !!MemoryCache.cacheItems
|
||||
return Boolean(MemoryCache.cacheItems
|
||||
.where('key', '=', key)
|
||||
.firstWhere(item => {
|
||||
return !item.expires || now < item.expires
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
public drop(key: string): Awaitable<void> {
|
||||
|
||||
Reference in New Issue
Block a user