1- import { fifo , fifoObject , lru , lruObject , ToadCache } from 'toad-cache'
2- import { SynchronousCache , SynchronousGroupedCache } from '../types/SyncDataSources'
3- import { CacheConfiguration } from '../types/DataSources'
1+ import type { CacheConstructor , ToadCache } from 'toad-cache'
2+ import { FifoMap , FifoObject , LruMap , LruObject } from 'toad-cache'
3+ import type { SynchronousCache , SynchronousGroupedCache } from '../types/SyncDataSources'
4+ import type { CacheConfiguration } from '../types/DataSources'
5+
6+ type CacheTypeId = 'lru-map' | 'fifo-map' | 'lru-object' | 'fifo-object'
47
58export interface InMemoryCacheConfiguration extends CacheConfiguration {
6- cacheType ?: 'lru' | 'fifo' | 'lru-object' | 'fifo-object'
7- groupCacheType ?: 'lru' | 'fifo' | 'lru-object' | 'fifo-object'
9+ cacheType ?: CacheTypeId
10+ groupCacheType ?: CacheTypeId
811 maxItems ?: number
912 maxGroups ?: number
1013 maxItemsPerGroup ?: number
@@ -25,31 +28,31 @@ export class InMemoryCache<T> implements SynchronousCache<T>, SynchronousGrouped
2528 name = 'In-memory cache'
2629 private readonly ttlInMsecs : number | undefined
2730 public readonly ttlLeftBeforeRefreshInMsecs ?: number
28- private readonly cacheConstructor : < T = any > ( max ?: number , ttl ?: number ) => ToadCache < T >
29- private readonly groupCacheConstructor : < T = any > ( max ?: number , ttl ?: number ) => ToadCache < T >
31+ private readonly cacheConstructor : CacheConstructor < ToadCache < T > >
32+ private readonly groupCacheConstructor : CacheConstructor < ToadCache < ToadCache < T | null > > >
3033
3134 constructor ( config : InMemoryCacheConfiguration ) {
32- this . cacheConstructor = this . resolveCacheConstructor ( config . cacheType ?? DEFAULT_CONFIGURATION . cacheType )
33- this . groupCacheConstructor = this . resolveCacheConstructor (
35+ this . cacheConstructor = this . resolveCacheConstructor < T > ( config . cacheType ?? DEFAULT_CONFIGURATION . cacheType )
36+ this . groupCacheConstructor = this . resolveCacheConstructor < ToadCache < T | null > > (
3437 config . groupCacheType ?? DEFAULT_CONFIGURATION . groupCacheType
3538 )
3639
37- this . cache = this . cacheConstructor ( config . maxItems ?? DEFAULT_CONFIGURATION . maxItems , config . ttlInMsecs ?? 0 )
38- this . groups = this . groupCacheConstructor ( config . maxGroups ?? DEFAULT_CONFIGURATION . maxGroups )
40+ this . cache = new this . cacheConstructor ( config . maxItems ?? DEFAULT_CONFIGURATION . maxItems , config . ttlInMsecs ?? 0 )
41+ this . groups = new this . groupCacheConstructor ( config . maxGroups ?? DEFAULT_CONFIGURATION . maxGroups )
3942 this . maxItemsPerGroup = config . maxItemsPerGroup ?? DEFAULT_CONFIGURATION . maxItemsPerGroup
4043 this . ttlInMsecs = config . ttlInMsecs
4144 this . ttlLeftBeforeRefreshInMsecs = config . ttlLeftBeforeRefreshInMsecs
4245 }
4346
44- private resolveCacheConstructor ( cacheTypeId : 'lru' | 'fifo' | 'lru-object' | 'fifo-object' ) {
45- if ( cacheTypeId === 'fifo' ) {
46- return fifo
47- } else if ( cacheTypeId === 'lru' ) {
48- return lru
47+ private resolveCacheConstructor < T > ( cacheTypeId : CacheTypeId ) : CacheConstructor < ToadCache < T > > {
48+ if ( cacheTypeId === 'fifo-map ' ) {
49+ return FifoMap
50+ } else if ( cacheTypeId === 'lru-map ' ) {
51+ return LruMap
4952 } else if ( cacheTypeId === 'fifo-object' ) {
50- return fifoObject
53+ return FifoObject
5154 } else {
52- return lruObject
55+ return LruObject
5356 }
5457 }
5558
@@ -59,7 +62,7 @@ export class InMemoryCache<T> implements SynchronousCache<T>, SynchronousGrouped
5962 return groupCache
6063 }
6164
62- const newGroupCache = this . cacheConstructor ( this . maxItemsPerGroup , this . ttlInMsecs )
65+ const newGroupCache = new this . cacheConstructor ( this . maxItemsPerGroup , this . ttlInMsecs )
6366 this . groups . set ( groupId , newGroupCache )
6467 return newGroupCache
6568 }
0 commit comments