-
Notifications
You must be signed in to change notification settings - Fork 465
Expand file tree
/
Copy pathcache.server.ts
More file actions
231 lines (210 loc) · 5.83 KB
/
cache.server.ts
File metadata and controls
231 lines (210 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import fs from 'node:fs'
import path from 'node:path'
import { DatabaseSync } from 'node:sqlite'
import {
cachified as baseCachified,
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 { LRUCache } from 'lru-cache'
import { z } from 'zod'
import { updatePrimaryCacheValue } from '#app/routes/admin/cache/sqlite.server.ts'
import { getInstanceInfo, getInstanceInfoSync } from './litefs.server.ts'
import { cachifiedTimingReporter, type Timings } from './timing.server.ts'
const CACHE_DATABASE_PATH = process.env.CACHE_DATABASE_PATH
const cacheDb = remember('cacheDb', createDatabase)
function createDatabase(tryAgain = true): DatabaseSync {
const databasePath = CACHE_DATABASE_PATH
if (!databasePath) {
throw new Error('CACHE_DATABASE_PATH is not set')
}
const parentDir = path.dirname(databasePath)
fs.mkdirSync(parentDir, { recursive: true })
const db = new DatabaseSync(databasePath)
const { currentIsPrimary } = getInstanceInfoSync()
if (!currentIsPrimary) return db
try {
// create cache table with metadata JSON column and value JSON column if it does not exist already
db.exec(`
CREATE TABLE IF NOT EXISTS cache (
key TEXT PRIMARY KEY,
metadata TEXT,
value TEXT
)
`)
} catch (error: unknown) {
try {
fs.rmSync(databasePath, { force: true })
} catch (unlinkError) {
if (
typeof unlinkError !== 'object' ||
unlinkError === null ||
!('code' in unlinkError) ||
unlinkError.code !== 'ENOENT'
) {
throw unlinkError
}
}
if (tryAgain) {
console.error(
`Error creating cache database, deleting the file at "${databasePath}" and trying again...`,
)
return createDatabase(false)
}
throw error
}
return db
}
const lru = remember(
'lru-cache',
() => new LRUCache<string, CacheEntry<unknown>>({ max: 5000 }),
)
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 isBuffer = (obj: unknown): obj is Buffer =>
Buffer.isBuffer(obj) || obj instanceof Uint8Array
function bufferReplacer(_key: string, value: unknown) {
if (isBuffer(value)) {
return {
__isBuffer: true,
data: value.toString('base64'),
}
}
return value
}
function bufferReviver(_key: string, value: unknown) {
if (
value &&
typeof value === 'object' &&
'__isBuffer' in value &&
(value as any).data
) {
return Buffer.from((value as any).data, 'base64')
}
return value
}
const cacheEntrySchema = z.object({
metadata: z.object({
createdTime: z.number(),
ttl: z.number().nullable().optional(),
swr: z.number().nullable().optional(),
}),
value: z.unknown(),
})
const cacheQueryResultSchema = z.object({
metadata: z.string(),
value: z.string(),
})
const getStatement = cacheDb.prepare(
'SELECT value, metadata FROM cache WHERE key = ?',
)
const setStatement = cacheDb.prepare(
'INSERT OR REPLACE INTO cache (key, value, metadata) VALUES (?, ?, ?)',
)
const deleteStatement = cacheDb.prepare('DELETE FROM cache WHERE key = ?')
const getAllKeysStatement = cacheDb.prepare('SELECT key FROM cache LIMIT ?')
const searchKeysStatement = cacheDb.prepare(
'SELECT key FROM cache WHERE key LIKE ? LIMIT ?',
)
export const cache: CachifiedCache = {
name: 'SQLite cache',
async get(key) {
const result = getStatement.get(key)
const parseResult = cacheQueryResultSchema.safeParse(result)
if (!parseResult.success) return null
const parsedEntry = cacheEntrySchema.safeParse({
metadata: JSON.parse(parseResult.data.metadata),
value: JSON.parse(parseResult.data.value, bufferReviver),
})
if (!parsedEntry.success) return null
const { metadata, value } = parsedEntry.data
if (!value) return null
return { metadata, value }
},
async set(key, entry) {
const { currentIsPrimary, primaryInstance } = await getInstanceInfo()
if (currentIsPrimary) {
const value = JSON.stringify(entry.value, bufferReplacer)
setStatement.run(key, value, JSON.stringify(entry.metadata))
} else {
// fire-and-forget cache update
void updatePrimaryCacheValue({
key,
cacheValue: entry,
}).then((response) => {
if (!response.ok) {
console.error(
`Error updating cache value for key "${key}" on primary instance (${primaryInstance}): ${response.status} ${response.statusText}`,
{ entry },
)
}
})
}
},
async delete(key) {
const { currentIsPrimary, primaryInstance } = await getInstanceInfo()
if (currentIsPrimary) {
deleteStatement.run(key)
} else {
// fire-and-forget cache update
void updatePrimaryCacheValue({
key,
cacheValue: undefined,
}).then((response) => {
if (!response.ok) {
console.error(
`Error deleting cache value for key "${key}" on primary instance (${primaryInstance}): ${response.status} ${response.statusText}`,
)
}
})
}
},
}
export async function getAllCacheKeys(limit: number) {
return {
sqlite: getAllKeysStatement
.all(limit)
.map((row) => (row as { key: string }).key),
lru: [...lru.keys()],
}
}
export async function searchCacheKeys(search: string, limit: number) {
return {
sqlite: searchKeysStatement
.all(`%${search}%`, limit)
.map((row) => (row as { key: string }).key),
lru: [...lru.keys()].filter((key) => key.includes(search)),
}
}
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),
)
}