|
| 1 | +--- |
| 2 | +sidebar_position: 6 |
| 3 | +--- |
| 4 | + |
| 5 | +# Cache |
| 6 | + |
| 7 | +Yelix provides a built-in in-memory caching system that helps improve performance by storing and reusing frequently accessed data. The cache system supports TTL (Time-To-Live) and is type-safe. |
| 8 | + |
| 9 | +Yelix cache made for endpoints so focus on that but able to use in other cases. |
| 10 | + |
| 11 | +## Features |
| 12 | + |
| 13 | +- 🚀 Type-safe generic cache implementation |
| 14 | +- ⏰ Flexible TTL (Time-To-Live) support |
| 15 | +- 🎯 Built-in cache hit/miss tracking |
| 16 | +- 🧹 Automatic cache cleanup |
| 17 | + |
| 18 | +## Basic Usage |
| 19 | + |
| 20 | +Here's a simple example of how to use the cache: |
| 21 | + |
| 22 | +```ts title="basic-cache-example.ts" |
| 23 | +import { YelixCache } from "@/src/utils/cache.ts"; |
| 24 | + |
| 25 | +// Create a type-safe cache instance |
| 26 | +const cache = new YelixCache<string>(); |
| 27 | + |
| 28 | +// Store a value for 5 seconds |
| 29 | +cache.set( |
| 30 | + null, // if you in a endpoint, pass ctx here otherwise pass null |
| 31 | + "greeting", "Hello World", "5s"); |
| 32 | + |
| 33 | +// Retrieve the value |
| 34 | +const value = cache.get(null, "greeting"); |
| 35 | +``` |
| 36 | + |
| 37 | +## Complete Example |
| 38 | + |
| 39 | +Here's a complete example showing cache implementation in an endpoint: |
| 40 | + |
| 41 | +```ts title="cache-example.ts" {7,12-15,19} |
| 42 | +import type { Ctx, ValidationType } from "@/mod.ts"; |
| 43 | +import z from "zod"; |
| 44 | +import type { QueryType } from "@/src/types/types.d.ts"; |
| 45 | +import { YelixCache } from "@/src/utils/cache.ts"; |
| 46 | + |
| 47 | +// Create a cache instance for storing strings |
| 48 | +export const cache = new YelixCache<string>(); |
| 49 | + |
| 50 | +export async function GET(ctx: Ctx) { |
| 51 | + const requestData = ctx.get("dataValidation").user; |
| 52 | + // Check if data exists in cache |
| 53 | + if (cache.has(ctx, query.name)) { |
| 54 | + const cachedData = cache.get(ctx, query.name)!; |
| 55 | + return await ctx.text(cachedData + " - (cached)", 200); |
| 56 | + } |
| 57 | + |
| 58 | + // If not in cache, compute and store |
| 59 | + const data = "Hello, " + query.name; |
| 60 | + cache.set(ctx, query.name, data, "5s"); // Cache for 5 seconds |
| 61 | + await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 62 | + |
| 63 | + return await ctx.text(data, 200); |
| 64 | +} |
| 65 | + |
| 66 | +export const path = "/api/hello"; |
| 67 | +export const middlewares = ["dataValidation"]; |
| 68 | + |
| 69 | +export const validation: ValidationType = { |
| 70 | + query: { |
| 71 | + name: z.string().min(1).required(), |
| 72 | + }, |
| 73 | +}; |
| 74 | +``` |
| 75 | + |
| 76 | +## Cache Performance |
| 77 | + |
| 78 | +When running the example above, you'll see the cache in action: |
| 79 | + |
| 80 | +```rust title="Terminal" |
| 81 | + 𝕐 Yelix 0.1.25 |
| 82 | + - Local : http://localhost:3030 |
| 83 | + - Watching : ./testing/api |
| 84 | + - OpenAPI Docs: /docs |
| 85 | + |
| 86 | +// highlight-next-line |
| 87 | +GET Cache: MISS /api/hello 200 in 1008ms // First request - cache miss |
| 88 | +GET Cache: HIT /api/hello 200 in 133μs // Subsequent requests - cache hit |
| 89 | +GET Cache: HIT /api/hello 200 in 485μs |
| 90 | +GET Cache: HIT /api/hello 200 in 99μs |
| 91 | +GET Cache: HIT /api/hello 200 in 101μs |
| 92 | +// highlight-next-line |
| 93 | +GET Cache: MISS /api/hello 200 in 1008ms // After 5s - cache expired |
| 94 | +``` |
| 95 | + |
| 96 | +## TTL Options |
| 97 | + |
| 98 | +The cache supports various TTL (Time-To-Live) options: |
| 99 | + |
| 100 | +```ts |
| 101 | +// Common TTL values |
| 102 | +cache.set(ctx, "key", "value", "5s"); // 5 seconds |
| 103 | +cache.set(ctx, "key", "value", "1m"); // 1 minute |
| 104 | +cache.set(ctx, "key", "value", "1h"); // 1 hour |
| 105 | +cache.set(ctx, "key", "value", "1d"); // 1 day |
| 106 | + |
| 107 | +// Custom milliseconds |
| 108 | +cache.set(ctx, "key", "value", 7000); // 7 seconds |
| 109 | + |
| 110 | +// No expiration |
| 111 | +cache.set(ctx, "key", "value", -1); // Never expires |
| 112 | +``` |
| 113 | + |
| 114 | +## Cache Methods |
| 115 | + |
| 116 | +The `YelixCache` class provides several useful methods: |
| 117 | + |
| 118 | +```ts |
| 119 | +const cache = new YelixCache<string>(); |
| 120 | + |
| 121 | +// If you in a endpoint, |
| 122 | +// pass `ctx`, |
| 123 | +// otherwise pass `null`. |
| 124 | + |
| 125 | +// Basic operations |
| 126 | +cache.set(ctx, "key", "value", "5s"); // Store value |
| 127 | +cache.get(ctx, "key"); // Retrieve value |
| 128 | +cache.has(ctx, "key"); // Check existence |
| 129 | +cache.delete(ctx, "key"); // Remove item |
| 130 | + |
| 131 | +// Maintenance |
| 132 | +cache.clear(ctx); // Clear all items |
| 133 | +cache.cleanup(ctx); // Remove expired items |
| 134 | +``` |
| 135 | + |
| 136 | +## Error Handling |
| 137 | + |
| 138 | +If you don't implement the cache properly, you'll see: |
| 139 | + |
| 140 | +```rust title="Terminal" |
| 141 | +// highlight-next-line |
| 142 | +GET Cache: not implemented /api/hello-no-cache 200 in 1006ms |
| 143 | +``` |
0 commit comments