You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lib/src/support/cache/CacheFactory.ts

87 lines
3.0 KiB

import {
AbstractFactory,
Container,
DependencyRequirement,
PropertyDependency,
isInstantiable,
DEPENDENCY_KEYS_METADATA_KEY,
StaticClass, Instantiable, getPropertyInjectionMetadata,
} 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<Cache> {
protected readonly logging: Logging
protected readonly config: Config
/** true if we have printed the memory-based cache driver warning once. */
private static loggedMemoryCacheWarningOnce = false
constructor() {
super({})
this.logging = Container.getContainer().make<Logging>(Logging)
this.config = Container.getContainer().make<Config>(Config)
}
produce(): Cache {
return new (this.getCacheClass())()
}
match(something: unknown): boolean {
return something === Cache
}
getDependencyKeys(): Collection<DependencyRequirement> {
const meta = Reflect.getMetadata(DEPENDENCY_KEYS_METADATA_KEY, this.getCacheClass())
if ( meta ) {
return meta
}
return new Collection<DependencyRequirement>()
}
getInjectedProperties(): Collection<PropertyDependency> {
const meta = new Collection<PropertyDependency>()
let currentToken = this.getCacheClass()
do {
const loadedMeta = getPropertyInjectionMetadata(currentToken)
if ( loadedMeta ) {
meta.concat(loadedMeta)
}
currentToken = Object.getPrototypeOf(currentToken)
} while (Object.getPrototypeOf(currentToken) !== Function.prototype && Object.getPrototypeOf(currentToken) !== Object.prototype)
return meta
}
/**
* Get the configured cache driver and return some Instantiable<Cache>.
* @protected
*/
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.`)
CacheFactory.loggedMemoryCacheWarningOnce = true
}
if ( !isInstantiable(CacheClass) || !(CacheClass.prototype instanceof Cache) ) {
const e = new ErrorWithContext('Provided session class does not extend from @extollo/lib.Cache')
e.context = {
configKey: 'server.cache.driver',
class: CacheClass.toString(),
}
}
return CacheClass
}
}