MCPcopy Create free account
hub / github.com/comuline/api / Cache

Class Cache

src/modules/v1/cache.ts:3–34  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1import { Redis } from "@upstash/redis/cloudflare"
2
3export class Cache<T> {
4 protected kv: Redis
5 public key: string
6
7 constructor(
8 protected env: {
9 UPSTASH_REDIS_REST_TOKEN: string
10 UPSTASH_REDIS_REST_URL: string
11 },
12 key: string,
13 ) {
14 this.key = key
15 this.kv = Redis.fromEnv(env)
16 }
17
18 async get(): Promise<T | null> {
19 const data = await this.kv.get<T>(this.key)
20 return data ?? null
21 }
22
23 async set(value: T, ttl?: number): Promise<void> {
24 await this.kv.set(
25 this.key,
26 JSON.stringify(value),
27 ttl
28 ? {
29 ex: ttl,
30 }
31 : undefined,
32 )
33 }
34}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected