{"library":"ocache","title":"Standalone Caching Utilities","description":"ocache is a JavaScript/TypeScript library providing robust caching utilities for functions and HTTP handlers. It supports common caching strategies such as time-to-live (TTL), stale-while-revalidate (SWR), and multi-tier caching. Currently at version 0.1.4, it is under active development by the unjs organization, suggesting a focus on modularity and integration within their ecosystem, although it is designed to be standalone. Key differentiators include built-in request deduplication for cached functions, automatic HTTP response caching with `etag` and `last-modified` support, and a flexible multi-tier caching mechanism that allows for layered cache storage. The project appears to follow a rapid release cadence for minor enhancements and performance improvements, typical for libraries in their early 0.x development phase, with type definitions included for TypeScript users.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install ocache"],"cli":null},"imports":["import { defineCachedFunction } from 'ocache';","import { defineCachedHandler } from 'ocache';","import { invalidateCache } from 'ocache';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { defineCachedFunction } from \"ocache\";\n\nconst cachedFetch = defineCachedFunction(\n  async (url: string) => {\n    console.log(`Fetching data for: ${url}`);\n    const res = await fetch(url);\n    if (!res.ok) {\n      throw new Error(`HTTP error! Status: ${res.status}`);\n    }\n    return res.json();\n  },\n  {\n    maxAge: 10, // Cache for 10 seconds\n    name: \"api-fetch\", // Identifier for cache keys\n    swr: true, // Enable stale-while-revalidate\n    staleMaxAge: 60 // Serve stale content for up to 60 seconds while revalidating\n  },\n);\n\nasync function runExample() {\n  console.log('--- First call ---');\n  const data1 = await cachedFetch(\"https://jsonplaceholder.typicode.com/todos/1\");\n  console.log('Data 1:', data1.title);\n\n  console.log('\\n--- Second call (should be cached) ---');\n  const data2 = await cachedFetch(\"https://jsonplaceholder.typicode.com/todos/1\");\n  console.log('Data 2:', data2.title);\n\n  // Wait for cache to become stale but still valid for SWR\n  console.log('\\n--- Waiting for cache to expire... ---');\n  await new Promise(resolve => setTimeout(resolve, 11 * 1000));\n\n  console.log('\\n--- Third call (should trigger revalidation and serve stale first) ---');\n  const data3 = await cachedFetch(\"https://jsonplaceholder.typicode.com/todos/1\");\n  console.log('Data 3 (potentially stale, revalidating in background):', data3.title);\n\n  // Invalidate specific cache entry\n  console.log('\\n--- Invalidating cache for todos/1 ---');\n  await cachedFetch.invalidate(\"https://jsonplaceholder.typicode.com/todos/1\");\n\n  console.log('\\n--- Fourth call (after invalidation, should re-fetch) ---');\n  const data4 = await cachedFetch(\"https://jsonplaceholder.typicode.com/todos/1\");\n  console.log('Data 4:', data4.title);\n}\n\nrunExample().catch(console.error);\n","lang":"typescript","description":"This example demonstrates defining a cached function with TTL and stale-while-revalidate, showing how it deduplicates requests, serves cached data, and can be explicitly invalidated.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}