Skip to content

Commit

Permalink
Narrow down supported types for In-Memory cache configuration (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad committed Apr 16, 2023
1 parent 7ae96af commit 7209078
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
5 changes: 3 additions & 2 deletions lib/AbstractCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ export const DEFAULT_CACHE_ERROR_HANDLER: LoaderErrorHandler = (err, key, cache,

export type CommonCacheConfig<
LoadedValue,
CacheType extends Cache<LoadedValue> | GroupCache<LoadedValue> = Cache<LoadedValue>
CacheType extends Cache<LoadedValue> | GroupCache<LoadedValue> = Cache<LoadedValue>,
InMemoryCacheType extends InMemoryCacheConfiguration | InMemoryGroupCacheConfiguration = InMemoryCacheConfiguration
> = {
logger?: Logger
cacheUpdateErrorHandler?: LoaderErrorHandler
loadErrorHandler?: LoaderErrorHandler
inMemoryCache?: InMemoryCacheConfiguration | InMemoryGroupCacheConfiguration | false
inMemoryCache?: InMemoryCacheType | false
asyncCache?: CacheType
}

Expand Down
4 changes: 3 additions & 1 deletion lib/GroupLoader.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import type { GroupCache, GroupDataSource } from './types/DataSources'
import type { LoaderConfig } from './Loader'
import { AbstractGroupCache } from './AbstractGroupCache'
import type { InMemoryGroupCacheConfiguration } from './memory/InMemoryGroupCache'

export type GroupLoaderConfig<LoadedValue, LoaderParams = undefined> = LoaderConfig<
LoadedValue,
GroupCache<LoadedValue>,
LoaderParams,
GroupDataSource<LoadedValue, LoaderParams>
GroupDataSource<LoadedValue, LoaderParams>,
InMemoryGroupCacheConfiguration
>
export class GroupLoader<LoadedValue, LoaderParams = undefined> extends AbstractGroupCache<LoadedValue, LoaderParams> {
private readonly loaders: readonly GroupDataSource<LoadedValue, LoaderParams>[]
Expand Down
7 changes: 5 additions & 2 deletions lib/Loader.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import type { CommonCacheConfig } from './AbstractCache'
import type { Cache, DataSource, GroupCache } from './types/DataSources'
import { AbstractFlatCache } from './AbstractFlatCache'
import type { InMemoryCacheConfiguration } from './memory'
import type { InMemoryGroupCacheConfiguration } from './memory/InMemoryGroupCache'

export type LoaderConfig<
LoadedValue,
CacheType extends Cache<LoadedValue> | GroupCache<LoadedValue> = Cache<LoadedValue>,
LoaderParams = undefined,
LoaderType = DataSource<LoadedValue, LoaderParams>
LoaderType = DataSource<LoadedValue, LoaderParams>,
InMemoryCacheType extends InMemoryCacheConfiguration | InMemoryGroupCacheConfiguration = InMemoryCacheConfiguration
> = {
loaders?: readonly LoaderType[]
throwIfLoadError?: boolean
throwIfUnresolved?: boolean
} & CommonCacheConfig<LoadedValue, CacheType>
} & CommonCacheConfig<LoadedValue, CacheType, InMemoryCacheType>

export class Loader<LoadedValue, LoaderParams = undefined> extends AbstractFlatCache<LoadedValue, LoaderParams> {
private readonly loaders: readonly DataSource<LoadedValue, LoaderParams>[]
Expand Down
2 changes: 1 addition & 1 deletion lib/memory/InMemoryGroupCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const DEFAULT_GROUP_CONFIGURATION = {
export class InMemoryGroupCache<T> implements SynchronousGroupCache<T> {
private readonly groups: ToadCache<ToadCache<T | null> | undefined | null>
private readonly maxItemsPerGroup: number
name = 'In-memory cache'
name = 'In-memory group cache'
private readonly ttlInMsecs: number | undefined
public readonly ttlLeftBeforeRefreshInMsecs?: number
private readonly cacheConstructor: CacheConstructor<ToadCache<T>>
Expand Down
4 changes: 2 additions & 2 deletions test/GroupLoader-main.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { InMemoryCacheConfiguration } from '../lib/memory/InMemoryCache'
import type { User } from './types/testTypes'
import { GroupLoader } from '../lib/GroupLoader'
import { DummyGroupedCache } from './fakes/DummyGroupedCache'
Expand All @@ -10,8 +9,9 @@ import { CountingGroupedLoader } from './fakes/CountingGroupedLoader'
import type { DummyLoaderParams } from './fakes/DummyLoaderWithParams'
import { DummyGroupedLoaderWithParams } from './fakes/DummyGroupedLoaderWithParams'
import { setTimeout } from 'timers/promises'
import type { InMemoryGroupCacheConfiguration } from '../lib/memory/InMemoryGroupCache'

const IN_MEMORY_CACHE_CONFIG = { ttlInMsecs: 9999999 } satisfies InMemoryCacheConfiguration
const IN_MEMORY_CACHE_CONFIG = { ttlInMsecs: 9999999 } satisfies InMemoryGroupCacheConfiguration

const user1: User = {
companyId: '1',
Expand Down

0 comments on commit 7209078

Please sign in to comment.