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.
35 lines
1.1 KiB
35 lines
1.1 KiB
import { CacheManager } from './lib/managers/cache-manager'
|
|
import { MemoryDriver } from './lib/drivers/memory-driver'
|
|
import { RedisDriver } from './lib/drivers/redis-driver'
|
|
import type { CacheDriver, CacheManagerOptions } from './lib/types'
|
|
import type { RedisDriverOptions } from './lib/drivers/redis-driver'
|
|
|
|
export * from './lib/types'
|
|
export { MemoryDriver } from './lib/drivers/memory-driver'
|
|
export { RedisDriver } from './lib/drivers/redis-driver'
|
|
export { CacheManager } from './lib/managers/cache-manager'
|
|
export type { RedisDriverOptions } from './lib/drivers/redis-driver'
|
|
|
|
export interface CacheFactoryOptions {
|
|
redis?: RedisDriverOptions
|
|
memory?: boolean
|
|
defaultTtl?: number
|
|
}
|
|
|
|
export function createCache(options: CacheFactoryOptions): CacheManager {
|
|
const drivers: CacheDriver[] = []
|
|
|
|
if (options.redis) {
|
|
drivers.push(new RedisDriver(options.redis))
|
|
}
|
|
|
|
if (options.memory !== false) {
|
|
drivers.push(new MemoryDriver())
|
|
}
|
|
|
|
if (drivers.length === 0) {
|
|
throw new Error('[cache] at least one driver (redis or memory) is required')
|
|
}
|
|
|
|
return new CacheManager({ drivers, defaultTtl: options.defaultTtl })
|
|
}
|