-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
416 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,100 @@ | ||
import { type CacheEntry, lruCacheAdapter } from '@epic-web/cachified' | ||
import { | ||
cachified as baseCachified, | ||
totalTtl, | ||
type CacheEntry, | ||
type CachifiedOptions, | ||
type Cache, | ||
} from '@epic-web/cachified' | ||
import { remember } from '@epic-web/remember' | ||
import { eq } from 'drizzle-orm' | ||
import { drizzle } from 'drizzle-orm/libsql' | ||
import { LRUCache } from 'lru-cache' | ||
import { z } from 'zod' | ||
import * as cacheDbSchema from '#drizzle/cache.ts' | ||
import { env } from './env.server' | ||
|
||
const lru = remember( | ||
'lru-cache', | ||
() => new LRUCache<string, CacheEntry<unknown>>({ max: 5000 }), | ||
export { longLivedCache, shortLivedCache, cachified } | ||
|
||
const cacheEntrySchema = z.object({ | ||
metadata: z.object({ | ||
createdTime: z.number(), | ||
ttl: z.number().nullable().optional(), | ||
swr: z.number().nullable().optional(), | ||
}), | ||
value: z.custom<{}>((val) => !!val), | ||
}) | ||
|
||
const cacheDb = remember('cacheDb', () => { | ||
const db = drizzle({ | ||
schema: cacheDbSchema, | ||
connection: { | ||
url: env.TURSO_DB_URL, | ||
authToken: env.TURSO_DB_AUTH_TOKEN, | ||
}, | ||
}) | ||
|
||
return db | ||
}) | ||
|
||
const cacheMemory = remember( | ||
'cacheMemory', | ||
() => new LRUCache<string, CacheEntry>({ max: 1000 }), | ||
) | ||
|
||
export const lruCache = lruCacheAdapter(lru) | ||
const longLivedCache: Cache = { | ||
async get(key) { | ||
const raw = await cacheDb.query.cacheTable.findFirst({ | ||
where: (cache) => eq(cache.key, key), | ||
}) | ||
|
||
if (!raw) { | ||
return null | ||
} | ||
|
||
const parsed = cacheEntrySchema.safeParse({ | ||
metadata: JSON.parse(raw.metadata), | ||
value: JSON.parse(raw.value), | ||
}) | ||
|
||
if (!parsed.success) { | ||
return null | ||
} | ||
|
||
return parsed.data | ||
}, | ||
set(key, cacheEnrtry) { | ||
void cacheDb | ||
.insert(cacheDbSchema.cacheTable) | ||
.values({ | ||
key, | ||
metadata: JSON.stringify(cacheEnrtry.metadata), | ||
value: JSON.stringify(cacheEnrtry.value), | ||
}) | ||
.then() | ||
}, | ||
delete(key) { | ||
void cacheDb | ||
.delete(cacheDbSchema.cacheTable) | ||
.where(eq(cacheDbSchema.cacheTable.key, key)) | ||
}, | ||
} | ||
|
||
const shortLivedCache: Cache = { | ||
set(key, value) { | ||
const ttl = totalTtl(value?.metadata) | ||
return cacheMemory.set(key, value, { | ||
ttl: ttl === Infinity ? undefined : ttl, | ||
start: value?.metadata?.createdTime, | ||
}) | ||
}, | ||
get(key) { | ||
return cacheMemory.get(key) | ||
}, | ||
delete(key) { | ||
return cacheMemory.delete(key) | ||
}, | ||
} | ||
|
||
function cachified<Value>(options: CachifiedOptions<Value>): Promise<Value> { | ||
return baseCachified(options) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
CREATE TABLE `cache` ( | ||
`key` text PRIMARY KEY NOT NULL, | ||
`metadata` text NOT NULL, | ||
`value` text NOT NULL | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { sqliteTable, text } from 'drizzle-orm/sqlite-core' | ||
|
||
export const cacheTable = sqliteTable('cache', { | ||
key: text('key').primaryKey(), | ||
metadata: text('metadata').notNull(), | ||
value: text('value').notNull(), | ||
}) |
Oops, something went wrong.