Skip to content

Commit

Permalink
cache
Browse files Browse the repository at this point in the history
  • Loading branch information
nichtsam committed Dec 2, 2024
1 parent acd6c02 commit 787db73
Show file tree
Hide file tree
Showing 9 changed files with 416 additions and 21 deletions.
100 changes: 95 additions & 5 deletions app/utils/cache.server.ts
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)
}
5 changes: 2 additions & 3 deletions app/utils/mdx/compile-mdx.server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { cachified } from '@epic-web/cachified'
import { remember } from '@epic-web/remember'
import { bundleMDX as _bundleMDX } from 'mdx-bundler'
import PQueue from 'p-queue'
import { lruCache } from '../cache.server.ts'
import { cachified, longLivedCache } from '../cache.server.ts'
import { type MdxBundleSource } from './mdx.server.ts'

async function bundleMDX({ source, files }: MdxBundleSource) {
Expand Down Expand Up @@ -44,7 +43,7 @@ const cachedBundleMDX = ({
const key = `${slug}:compiled`
const compileMdx = cachified({
key,
cache: lruCache,
cache: longLivedCache,
getFreshValue: () => queuedBundleMDX(bundle),
})

Expand Down
2 changes: 1 addition & 1 deletion drizzle.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const env = z

export default {
dialect: 'turso',
schema: './drizzle/schema.ts',
schema: ['./drizzle/schema.ts', './drizzle/cache.ts'],
out: './drizzle',
dbCredentials: {
url: env.TURSO_DB_URL,
Expand Down
5 changes: 5 additions & 0 deletions drizzle/0001_fat_monster_badoon.sql
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
);
7 changes: 7 additions & 0 deletions drizzle/cache.ts
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(),
})
Loading

0 comments on commit 787db73

Please sign in to comment.