Skip to content

Commit

Permalink
upgrade cachified (#618)
Browse files Browse the repository at this point in the history
Co-authored-by: Kent C. Dodds <[email protected]>
  • Loading branch information
Xiphe and kentcdodds authored Feb 12, 2024
1 parent 43d5c92 commit bf559f5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
42 changes: 29 additions & 13 deletions app/utils/cache.server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import fs from 'fs'
import {
cachified as baseCachified,
lruCacheAdapter,
verboseReporter,
mergeReporters,
type CacheEntry,
type Cache as CachifiedCache,
type CachifiedOptions,
type Cache,
totalTtl,
type CreateReporter,
} from '@epic-web/cachified'
import { remember } from '@epic-web/remember'
import Database from 'better-sqlite3'
Expand Down Expand Up @@ -52,7 +54,19 @@ const lru = remember(
() => new LRUCache<string, CacheEntry<unknown>>({ max: 5000 }),
)

export const lruCache = lruCacheAdapter(lru)
export const lruCache = {
name: 'app-memory-cache',
set: (key, value) => {
const ttl = totalTtl(value?.metadata)
lru.set(key, value, {
ttl: ttl === Infinity ? undefined : ttl,
start: value?.metadata?.createdTime,
})
return value
},
get: key => lru.get(key),
delete: key => lru.delete(key),
} satisfies Cache

const cacheEntrySchema = z.object({
metadata: z.object({
Expand Down Expand Up @@ -153,15 +167,17 @@ export async function searchCacheKeys(search: string, limit: number) {
}
}

export async function cachified<Value>({
timings,
reporter = verboseReporter(),
...options
}: CachifiedOptions<Value> & {
timings?: Timings
}): Promise<Value> {
return baseCachified({
...options,
reporter: mergeReporters(cachifiedTimingReporter(timings), reporter),
})
export async function cachified<Value>(
{
timings,
...options
}: CachifiedOptions<Value> & {
timings?: Timings
},
reporter: CreateReporter<Value> = verboseReporter<Value>(),
): Promise<Value> {
return baseCachified(
options,
mergeReporters(cachifiedTimingReporter(timings), reporter),
)
}
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"dependencies": {
"@conform-to/react": "^1.0.0",
"@conform-to/zod": "^1.0.0",
"@epic-web/cachified": "^4.0.0",
"@epic-web/cachified": "^5.1.1",
"@epic-web/client-hints": "^1.2.2",
"@epic-web/invariant": "^1.0.0",
"@epic-web/remember": "^1.0.2",
Expand Down

0 comments on commit bf559f5

Please sign in to comment.