Skip to content

Commit 87446c0

Browse files
committed
fix(types): expose an explicit type for the output of logic wrapped with extendable caching
1 parent e874165 commit 87446c0

File tree

2 files changed

+38
-36
lines changed

2 files changed

+38
-36
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export { withSimpleCaching, SimpleCache, KeySerializationMethod, SimpleCacheResolutionMethod, WithSimpleCachingOptions } from './withSimpleCaching';
2-
export { withExtendableCaching } from './withExtendableCaching';
2+
export { withExtendableCaching, LogicWithExtendableCaching } from './withExtendableCaching';

src/withExtendableCaching.ts

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,30 @@
11
import { withSimpleCaching } from '.';
2+
import { isAFunction } from './isAFunction';
23
import {
34
defaultKeySerializationMethod,
45
defaultValueSerializationMethod,
56
getCacheFromCacheOption,
67
WithSimpleCachingOptions,
78
} from './withSimpleCaching';
89

9-
/**
10-
* exposes the cache-wrapped method along with some primitives which enable extending the caching logic
11-
*
12-
* specifically
13-
* - exposes a way to `invalidate` the cache, for a given input (e.g., to support external triggers for invalidation)
14-
* - exposes a way to `update` the cache, for a given input (e.g., to support write-through caching and optimistic caching)
15-
*
16-
* relevance
17-
* - when wrapping logic to cache the user is able to specify several caching options (e.g., key serialization method, value serialization method, etc)
18-
* - in order to define their own `invalidation` and `update` methods, without this function, the user would need to access these caching options per function elsewhere
19-
* - this function makes it easy to utilize and extend cache invalidation + update commands for the wrapped logic, by managing the references to the caching options on behalf of the user
20-
*/
21-
export const withExtendableCaching = <LR extends any, CR extends any, L extends (...args: any[]) => LR>(
22-
logic: L,
23-
options: WithSimpleCachingOptions<LR, CR, L>,
24-
) => {
10+
export interface LogicWithExtendableCaching<LR extends any, CR extends any, L extends (...args: any[]) => LR> {
2511
/**
2612
* execute the logic with caching
2713
*/
28-
const execute = withSimpleCaching(logic, options);
14+
execute: L;
2915

3016
/**
3117
* invalidate the cached value for a given input
3218
*
3319
* note
3420
* - applies key serialization on the key input, just like execute
3521
*/
36-
const invalidate = async ({
37-
forInput,
38-
}: {
22+
invalidate: (args: {
3923
/**
4024
* invalidate the cache for this input
4125
*/
4226
forInput: Parameters<L>;
43-
}) => {
44-
const serializeKey = options.serialize?.key ?? defaultKeySerializationMethod;
45-
const cache = getCacheFromCacheOption({ forInput, cacheOption: options.cache });
46-
await cache.set(serializeKey(forInput), undefined as CR);
47-
};
27+
}) => Promise<void>;
4828

4929
/**
5030
* update the cached value for a given input
@@ -53,10 +33,7 @@ export const withExtendableCaching = <LR extends any, CR extends any, L extends
5333
* - applies key serialization on the key input, just like execute
5434
* - applies value serialization on the value output, just like execute
5535
*/
56-
const update = async ({
57-
forInput,
58-
toValue,
59-
}: {
36+
update: (args: {
6037
/**
6138
* update the cache for this input
6239
*/
@@ -65,16 +42,41 @@ export const withExtendableCaching = <LR extends any, CR extends any, L extends
6542
/**
6643
* update the cache to this value
6744
*/
68-
toValue: ReturnType<L>;
69-
}) => {
45+
toValue: ReturnType<L> | ((args: { cachedValue: CR | undefined }) => ReturnType<L>);
46+
}) => Promise<void>;
47+
}
48+
49+
/**
50+
* exposes the cache-wrapped method along with some primitives which enable extending the caching logic
51+
*
52+
* specifically
53+
* - exposes a way to `invalidate` the cache, for a given input (e.g., to support external triggers for invalidation)
54+
* - exposes a way to `update` the cache, for a given input (e.g., to support write-through caching and optimistic caching)
55+
*
56+
* relevance
57+
* - when wrapping logic to cache the user is able to specify several caching options (e.g., key serialization method, value serialization method, etc)
58+
* - in order to define their own `invalidation` and `update` methods, without this function, the user would need to access these caching options per function elsewhere
59+
* - this function makes it easy to utilize and extend cache invalidation + update commands for the wrapped logic, by managing the references to the caching options on behalf of the user
60+
*/
61+
export const withExtendableCaching = <LR extends any, CR extends any, L extends (...args: any[]) => LR>(
62+
logic: L,
63+
options: WithSimpleCachingOptions<LR, CR, L>,
64+
): LogicWithExtendableCaching<LR, CR, L> => {
65+
const execute: LogicWithExtendableCaching<LR, CR, L>['execute'] = withSimpleCaching(logic, options);
66+
67+
const invalidate: LogicWithExtendableCaching<LR, CR, L>['invalidate'] = async ({ forInput }) => {
68+
const serializeKey = options.serialize?.key ?? defaultKeySerializationMethod;
69+
const cache = getCacheFromCacheOption({ forInput, cacheOption: options.cache });
70+
await cache.set(serializeKey(forInput), undefined as CR);
71+
};
72+
73+
const update: LogicWithExtendableCaching<LR, CR, L>['update'] = async ({ forInput, toValue }) => {
7074
const serializeKey = options.serialize?.key ?? defaultKeySerializationMethod;
7175
const serializeValue = options.serialize?.value ?? defaultValueSerializationMethod;
7276
const cache = getCacheFromCacheOption({ forInput, cacheOption: options.cache });
73-
await cache.set(serializeKey(forInput), serializeValue(toValue), { secondsUntilExpiration: options.secondsUntilExpiration });
77+
const newValue = isAFunction(toValue) ? toValue({ cachedValue: await cache.get(serializeKey(forInput)) }) : toValue;
78+
await cache.set(serializeKey(forInput), serializeValue(newValue), { secondsUntilExpiration: options.secondsUntilExpiration });
7479
};
7580

76-
/**
77-
* return the extendable cache-wrapped logic methods
78-
*/
7981
return { execute, invalidate, update };
8082
};

0 commit comments

Comments
 (0)