Zuplo
Caching APIs

MemoryZoneReadThroughCache

The MemoryZoneReadThroughCache class provides an in-memory caching solution with automatic read-through capabilities. This cache stores data in memory for fast access and automatically handles cache misses.

Constructor

new MemoryZoneReadThroughCache<T = unknown>(name: string, context: ZuploContext)
ts

Creates a new instance of the memory cache with read-through capabilities.

  • name - A unique identifier for the cache instance
  • context - The ZuploContext object
  • T - The type of data stored in the cache (defaults to unknown)

Methods

get

Retrieves a value from the cache by its key. Returns undefined if not found.

get(key: string): Promise<T | undefined>
ts

put

Stores a value in the cache with a time-to-live (TTL) in seconds.

put(key: string, data: T, ttlSeconds: number): void
ts

delete

Removes a value from the cache.

delete(key: string): Promise<void>
ts

Example

import { MemoryZoneReadThroughCache, ZuploContext, ZuploRequest, } from "@zuplo/runtime"; export default async function handler( request: ZuploRequest, context: ZuploContext, ) { // Create a cache instance for user data const userCache = new MemoryZoneReadThroughCache<{ name: string; email: string; }>("user-cache", context); // Try to get user from cache const userId = "user-123"; let user = await userCache.get(userId); if (!user) { // User not in cache, fetch from database user = await fetchUserFromDatabase(userId); // Cache the user data for 5 minutes (300 seconds) userCache.put(userId, user, 300); } return new Response(JSON.stringify(user)); }
ts

Best Practices

  • Use descriptive cache names to avoid collisions between different cache instances
  • Set appropriate TTL values based on your data freshness requirements
  • Consider memory usage when caching large objects
  • The cache is scoped to the current worker instance and not shared across instances

See Also