Skip to content

Commit

Permalink
fix: rename type alias Ttl as Milliseconds
Browse files Browse the repository at this point in the history
  • Loading branch information
zzau13 committed Feb 3, 2023
1 parent 4af9487 commit daa5fa8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
18 changes: 10 additions & 8 deletions src/caching.ts
@@ -1,18 +1,19 @@
import { MemoryCache, MemoryConfig, memoryStore } from './stores';

export type Config = {
ttl?: Ttl;
ttl?: Milliseconds;
isCacheable?: (val: unknown) => boolean;
};

export type Ttl = number;
export type Milliseconds = number;
export type Ttl = Milliseconds;

export type Store = {
get<T>(key: string): Promise<T | undefined>;
set<T>(key: string, data: T, ttl?: Ttl): Promise<void>;
set<T>(key: string, data: T, ttl?: Milliseconds): Promise<void>;
del(key: string): Promise<void>;
reset(): Promise<void>;
mset(args: [string, unknown][], ttl?: Ttl): Promise<void>;
mset(args: [string, unknown][], ttl?: Milliseconds): Promise<void>;
mget(...args: string[]): Promise<unknown[]>;
mdel(...args: string[]): Promise<void>;
keys(pattern?: string): Promise<string[]>;
Expand All @@ -33,11 +34,11 @@ export type Stores<S extends Store, T extends object> =
export type CachingConfig<T> = MemoryConfig | StoreConfig | FactoryConfig<T>;

export type Cache<S extends Store = Store> = {
set: (key: string, value: unknown, ttl?: Ttl) => Promise<void>;
set: (key: string, value: unknown, ttl?: Milliseconds) => Promise<void>;
get: <T>(key: string) => Promise<T | undefined>;
del: (key: string) => Promise<void>;
reset: () => Promise<void>;
wrap<T>(key: string, fn: () => Promise<T>, ttl?: Ttl): Promise<T>;
wrap<T>(key: string, fn: () => Promise<T>, ttl?: Milliseconds): Promise<T>;
store: S;
};

Expand Down Expand Up @@ -74,7 +75,7 @@ export async function caching<S extends Store, T extends object = never>(
* const result = await cache.wrap('key', () => Promise.resolve(1));
*
*/
wrap: async <T>(key: string, fn: () => Promise<T>, ttl?: Ttl) => {
wrap: async <T>(key: string, fn: () => Promise<T>, ttl?: Milliseconds) => {
const value = await store.get<T>(key);
if (value === undefined) {
const result = await fn();
Expand All @@ -86,7 +87,8 @@ export async function caching<S extends Store, T extends object = never>(
store: store as S,
del: (key: string) => store.del(key),
get: <T>(key: string) => store.get<T>(key),
set: (key: string, value: unknown, ttl?: Ttl) => store.set(key, value, ttl),
set: (key: string, value: unknown, ttl?: Milliseconds) =>
store.set(key, value, ttl),
reset: () => store.reset(),
};
}
14 changes: 11 additions & 3 deletions src/multi-caching.ts
@@ -1,4 +1,4 @@
import { Cache, Ttl } from './caching';
import { Cache, Milliseconds } from './caching';

export type MultiCache = Omit<Cache, 'store'>;

Expand All @@ -16,7 +16,11 @@ export function multiCaching<Caches extends Cache[]>(
} catch (e) {}
}
};
const set = async <T>(key: string, data: T, ttl?: Ttl | undefined) => {
const set = async <T>(
key: string,
data: T,
ttl?: Milliseconds | undefined,
) => {
await Promise.all(caches.map((cache) => cache.set(key, data, ttl)));
};
return {
Expand All @@ -25,7 +29,11 @@ export function multiCaching<Caches extends Cache[]>(
del: async (key) => {
await Promise.all(caches.map((cache) => cache.del(key)));
},
async wrap<T>(key: string, fn: () => Promise<T>, ttl?: Ttl): Promise<T> {
async wrap<T>(
key: string,
fn: () => Promise<T>,
ttl?: Milliseconds,
): Promise<T> {
let value: T | undefined;
let i = 0;
for (; i < caches.length; i++) {
Expand Down

0 comments on commit daa5fa8

Please sign in to comment.