소스 정보
- 저장소
- epicweb-dev/epic-stack
- 최근 소스 활동
- 2026년 1월 30일 00:59
- 감지된 SKILL.md 언어
- 영어
- 스타
- 5,546
- 포크
- 464
설치 방법
기본적으로 소스를 먼저 확인하는 Prompt가 선택됩니다. 직접 명령으로 전환하거나 로컬 사본을 다운로드할 수도 있습니다.
소스 파일 검토
설치 여부를 결정하기 전에 SKILL.md와 SkillsMP에 표시된 보조 파일을 읽어 보세요.
메뉴
기본적으로 소스를 먼저 확인하는 Prompt가 선택됩니다. 직접 명령으로 전환하거나 로컬 사본을 다운로드할 수도 있습니다.
설치 여부를 결정하기 전에 SKILL.md와 SkillsMP에 표시된 보조 파일을 읽어 보세요.
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
직접 명령은 검토 Prompt를 거치지 않습니다. 실행하기 전에 소스를 확인하세요.
npx skills add https://github.com/epicweb-dev/epic-stack --skill epic-caching명령은 한 줄로 유지됩니다. 복사하기 전에 가로로 스크롤해 전체 내용을 확인하세요.
로컬 사본을 원하시나요? SkillsMP에서 현재 제공할 수 있는 파일을 다운로드하세요.
SOC 직업 분류 기준
SKILL.md 표시 중
| name | epic-caching |
| description | Guide on caching with cachified, SQLite cache, and LRU cache for Epic Stack |
| categories | ["caching","performance","optimization"] |
Use this skill when you need to:
Following Epic Web principles:
Weigh the cost-benefit of performance optimizations - Caching adds complexity. Only add cache when there's a clear, measurable benefit. Don't cache "just in case" - cache when you have a real performance problem that caching solves.
When NOT to use cache:
Example - Evaluating cost-benefit:
// ✅ Good - Cache expensive external API call
export async function getGitHubEvents({
username,
timings,
}: {
username: string
timings?: Timings
}) {
return await cachified({
key: `github:${username}:events`,
cache,
timings,
getFreshValue: async () => {
// Expensive: External API call, rate limits, network latency
const response = await fetch(
`https://api.github.com/users/${username}/events/public`,
)
return await response.json()
},
checkValue: GitHubEventSchema.array(),
ttl: 1000 * 60 * 60, // 1 hour - reasonable for external data
})
}
// ❌ Avoid - Caching simple, fast database query
export async function getUser({ userId }: { userId: string }) {
// This query is already fast - caching adds complexity without benefit
return await cachified({
key: `user:${userId}`,
cache,
getFreshValue: async () => {
// Simple query, already fast
return await prisma.user.findUnique({
where: { id: userId },
select: { id: true, username: true },
})
},
ttl: 1000 * 60 * 5,
})
// Better: Just query directly without cache
}
Epic Stack provides two types of cache:
SQLite Cache - Long-lived, replicated with LiteFS
LRU Cache - Short-lived, in-memory
Epic Stack uses @epic-web/cachified as an abstraction for cache management.
Basic import:
import { cachified, cache } from '#app/utils/cache.server.ts'
import { type Timings } from '#app/utils/timing.server.ts'
Basic structure:
export async function getCachedData({
timings,
}: {
timings?: Timings
} = {}) {
return await cachified({
key: 'my-cache-key',
cache,
timings,
getFreshValue: async () => {
// Get fresh data
return await fetchDataFromAPI()
},
checkValue: z.object({
/* schema */
}), // Validation with Zod
ttl: 1000 * 60 * 60 * 24, // 24 hours
staleWhileRevalidate: 1000 * 60 * 60 * 24 * 30, // 30 days
})
}
Naming conventions:
entity:identifier:datauser:${userId}:profilenote:${noteId}:fullapi:github:eventstito:scheduled-eventsAvoid:
Define TTL:
await cachified({
key: 'my-key',
cache,
getFreshValue: () => fetchData(),
ttl: 1000 * 60 * 60 * 24, // 24 hours in milliseconds
})
Null TTL to never expire:
ttl: null, // Never expires (not recommended unless necessary)
SWR allows returning stale data while fresh data is fetched in the background.
Example:
await cachified({
key: 'my-key',
cache,
getFreshValue: () => fetchData(),
ttl: 1000 * 60 * 60 * 24, // 24 hours - after this it's considered stale
staleWhileRevalidate: 1000 * 60 * 60 * 24 * 30, // 30 days - up to here returns stale while revalidating
})
Behavior:
Always validate cached data with Zod:
import { z } from 'zod'
const EventSchema = z.object({
id: z.string(),
title: z.string(),
date: z.string(),
})
export async function getEvents({ timings }: { timings?: Timings } = {}) {
return await cachified({
key: 'events:all',
cache,
timings,
getFreshValue: async () => {
const response = await fetch('https://api.example.com/events')
return await response.json()
},
checkValue: EventSchema.array(), // Validates it's an array of events
ttl: 1000 * 60 * 60 * 24, // 24 hours
})
}
If cached data doesn't pass validation, fresh data is fetched.
Integrate cache with server timing for monitoring:
import { type Timings } from '#app/utils/timing.server.ts'
export async function loader({ request }: Route.LoaderArgs) {
const timings: Timings = {}
const events = await getEvents({ timings })
// Timings are automatically added to headers
return json(
{ events },
{
headers: combineServerTimings(timings),
},
)
}
Invalidate by key:
import { cache } from '#app/utils/cache.server.ts'
await cache.delete('user:123:profile')
Invalidate multiple keys:
// Search and delete matching keys
import { searchCacheKeys } from '#app/utils/cache.server.ts'
const keys = await searchCacheKeys('user:123', 100)
await Promise.all(keys.map((key) => cache.delete(key)))
Invalidate entire SQLite cache:
// Use admin dashboard or
await cache.clear() // If available
For temporary data, use LRU cache directly:
import { lru } from '#app/utils/cache.server.ts'
// LRU cache is useful for:
// - Request deduplication
// - Very temporary cache (< 5 minutes)
// - Data that doesn't need to persist
const cachedValue = lru.get('temp-key')
if (!cachedValue) {
const freshValue = await computeExpensiveValue()
lru.set('temp-key', freshValue, { ttl: 1000 * 60 * 5 }) // 5 minutes
return freshValue
}
return cachedValue
With LiteFS, SQLite cache is automatically replicated:
Behavior:
Best practices:
ensurePrimary() if you need to guarantee writesimport { ensurePrimary } from '#app/utils/litefs.server.ts'
export async function action({ request }: Route.ActionArgs) {
await ensurePrimary() // Ensure we're on primary instance
// Invalidate cache
await cache.delete('my-key')
// ...
}
Handle errors in getFreshValue:
await cachified({
key: 'my-key',
cache,
getFreshValue: async () => {
try {
return await fetchData()
} catch (error) {
console.error('Failed to fetch fresh data:', error)
throw error // Re-throw so cachified handles it
}
},
// If getFreshValue fails and there's stale cache, it returns it
fallbackToCache: true, // Default: true
})
Epic Stack includes a dashboard to manage cache:
Route: /admin/cache
Features:
// app/utils/api.server.ts
import { cachified, cache } from '#app/utils/cache.server.ts'
import { type Timings } from '#app/utils/timing.server.ts'
import { z } from 'zod'
const GitHubEventSchema = z.object({
id: z.string(),
type: z.string(),
actor: z.object({
login: z.string(),
}),
created_at: z.string(),
})
export async function getGitHubEvents({
username,
timings,
}: {
username: string
timings?: Timings
}) {
return await cachified({
key: `github:${username}:events`,
cache,
timings,
getFreshValue: async () => {
const response = await fetch(
`https://api.github.com/users/${username}/events/public`,
)
if (!response.ok) {
throw new Error(`GitHub API error: `)
}
data = response.()
data
},
: .(),
: * * ,
: * * * ,
})
}
// app/utils/user.server.ts
import { cachified, cache } from '#app/utils/cache.server.ts'
import { prisma } from '#app/utils/db.server.ts'
import { z } from 'zod'
const UserStatsSchema = z.object({
totalNotes: z.number(),
totalLikes: z.number(),
joinDate: z.string(),
})
export async function getUserStats({
userId,
timings,
}: {
userId: string
timings?: Timings
}) {
return await cachified({
key: `user:${userId}:stats`,
cache,
timings,
getFreshValue: async () => {
const [totalNotes, totalLikes, user] = await Promise.all([
prisma.note.count({ where: { ownerId: userId } }),
prisma.like.count({ where: { userId } }),
prisma.user.findUnique({
where: { id: userId },
select: { createdAt: },
}),
])
{
totalNotes,
totalLikes,
: user?..() ?? ,
}
},
: ,
: * * ,
: * * ,
})
}
// app/routes/users/$username/notes/new.tsx
export async function action({ request }: Route.ActionArgs) {
const userId = await requireUserId(request)
const formData = await request.formData()
// ... validate and create note
const note = await prisma.note.create({
data: {
title,
content,
ownerId: userId,
},
include: { owner: true },
})
// Invalidate related cache
await Promise.all([
cache.delete(`user:${userId}:notes`),
cache.delete(`user:${userId}:stats`),
cache.delete(`note:${note.id}:full`),
])
return redirect(`/users/${note.owner.username}/notes/${note.id}`)
}
export async function getUserWithNotes({
userId,
timings,
}: {
userId: string
timings?: Timings
}) {
const user = await cachified({
key: `user:${userId}:profile`,
cache,
timings,
getFreshValue: async () => {
return await prisma.user.findUnique({
where: { id: userId },
select: {
id: true,
username: true,
name: true,
},
})
},
checkValue: z
.object({
id: z.string(),
username: z.string(),
name: z.string().nullable(),
})
.nullable(),
ttl: 1000 * 60 * 30, // 30 minutes
})
const notes = await cachified({
key: `user:${userId}:notes`,
cache,
timings,
getFreshValue: async () => {
return prisma..({
: { : userId },
: {
: ,
: ,
: ,
},
: { : },
})
},
: z.(
z.({
: z.(),
: z.(),
: z.(),
}),
),
: * * ,
})
{ user, notes }
}
// Avoid multiple simultaneous requests to the same URL
const requestCache = new Map<string, Promise<any>>()
export async function fetchWithDedup(url: string) {
if (requestCache.has(url)) {
return requestCache.get(url)
}
const promise = fetch(url).then((res) => res.json())
requestCache.set(url, promise)
// Clean up after 1 second
setTimeout(() => {
requestCache.delete(url)
}, 1000)
return promise
}
checkValue with Zod to
validate dataapp/utils/cache.server.ts - Cache implementationapp/routes/admin/cache/ - Admin dashboardapp/utils/timing.server.ts - Server timing utilities