/** * Simple Cloudflare Worker for testing CloudflareCache functionality */ import { CloudflareCache } from '../src/Cache/CloudflareCache'; import type { TypeCacheKey } from '@cachehub/core'; export default { async fetch(request: Request, _env: any, _ctx: ExecutionContext): Promise { const url = new URL(request.url); if (url.pathname === '/health') { return new Response('OK', { status: 200 }); } if (url.pathname === '/test-pattern-deletion') { const cache = new CloudflareCache('https://test.cache.com'); try { // Step 1: Set multiple cache entries with different actions console.log('=== Setting up test data ==='); const key1: TypeCacheKey = { namespace: 'pattern', id: 'user123', context: { action: 'activation', version: 'v1' } }; const key2: TypeCacheKey = { namespace: 'pattern', id: 'user123', context: { action: 'validation', version: 'v1' } }; const key3: TypeCacheKey = { namespace: 'pattern', id: 'user456', context: { action: 'activation', version: 'v1' } }; const key4: TypeCacheKey = { namespace: 'other', id: 'user123', context: { action: 'activation', version: 'v1' } }; await cache.set(key1, 'activation-data-123'); await cache.set(key2, 'validation-data-123'); await cache.set(key3, 'activation-data-456'); await cache.set(key4, 'other-namespace-data'); console.log('✅ Set 4 cache entries'); // Step 2: Verify all entries exist const val1 = await cache.get(key1); const val2 = await cache.get(key2); const val3 = await cache.get(key3); const val4 = await cache.get(key4); console.log('Values before deletion:', { val1, val2, val3, val4 }); // Step 3: Delete by pattern - all entries with namespace 'pattern' and action 'activation' console.log('=== Testing pattern deletion ==='); const deleted = await cache.delete({ namespace: 'pattern', context: { action: 'activation' } }); console.log('Pattern deletion result:', deleted); // Step 4: Wait 3 seconds for TTL expiration console.log('Waiting 3 seconds for TTL expiration...'); await new Promise(resolve => setTimeout(resolve, 3000)); // Step 5: Check what remains const afterVal1 = await cache.get(key1); // Should be gone (pattern + activation) const afterVal2 = await cache.get(key2); // Should remain (pattern + validation) const afterVal3 = await cache.get(key3); // Should be gone (pattern + activation) const afterVal4 = await cache.get(key4); // Should remain (other namespace) console.log('Values after deletion:', { afterVal1, afterVal2, afterVal3, afterVal4 }); const results = { success: true, deleted, before: { val1, val2, val3, val4 }, after: { afterVal1, afterVal2, afterVal3, afterVal4 }, expected: { afterVal1: undefined, // Should be deleted afterVal2: 'validation-data-123', // Should remain afterVal3: undefined, // Should be deleted afterVal4: 'other-namespace-data' // Should remain } }; return new Response(JSON.stringify(results, null, 2), { headers: { contentType: 'application/json' } }); } catch (error) { console.error('Test failed:', error); return new Response(JSON.stringify({ success: false, error: error.message, stack: error.stack }, null, 2), { status: 500, headers: { contentType: 'application/json' } }); } } return new Response('Not Found', { status: 404 }); } };