|
| 1 | +import type { Queue, QueueMessage } from "@opennextjs/aws/types/overrides"; |
| 2 | + |
| 3 | +interface QueueCachingOptions { |
| 4 | + /** |
| 5 | + * Enables a regional cache for the queue. |
| 6 | + * When enabled, the first request to the queue is cached for `regionalCacheTtlSec` seconds. |
| 7 | + * Subsequent similar requests during this period will bypass processing and use the cached result. |
| 8 | + * **Note:** Ensure the `MAX_REVALIDATE_CONCURRENCY` environment variable is appropriately increased before enabling this feature. |
| 9 | + * In case of an error, cache revalidation may be delayed by up to `regionalCacheTtlSec` seconds. |
| 10 | + * @default false |
| 11 | + */ |
| 12 | + enableRegionalCache?: boolean; |
| 13 | + /** |
| 14 | + * The TTL for the regional cache in seconds. |
| 15 | + * @default 5 |
| 16 | + */ |
| 17 | + regionalCacheTtlSec?: number; |
| 18 | + |
| 19 | + /** |
| 20 | + * Whether to wait for the queue ack before returning. |
| 21 | + * When set to false, the cache will be populated asap and the queue will be called after. |
| 22 | + * When set to true, the cache will be populated only after the queue ack is received. |
| 23 | + * @default false |
| 24 | + */ |
| 25 | + waitForQueueAck?: boolean; |
| 26 | +} |
| 27 | + |
| 28 | +const DEFAULT_QUEUE_CACHE_TTL_SEC = 5; |
| 29 | + |
| 30 | +class QueueCache implements Queue { |
| 31 | + readonly name; |
| 32 | + readonly enableRegionalCache: boolean; |
| 33 | + readonly regionalCacheTtlSec: number; |
| 34 | + readonly waitForQueueAck: boolean; |
| 35 | + cache: Cache | undefined; |
| 36 | + |
| 37 | + constructor( |
| 38 | + private originalQueue: Queue, |
| 39 | + options: QueueCachingOptions |
| 40 | + ) { |
| 41 | + this.name = `cached-${originalQueue.name}`; |
| 42 | + this.enableRegionalCache = options.enableRegionalCache ?? false; |
| 43 | + this.regionalCacheTtlSec = options.regionalCacheTtlSec ?? DEFAULT_QUEUE_CACHE_TTL_SEC; |
| 44 | + this.waitForQueueAck = options.waitForQueueAck ?? false; |
| 45 | + } |
| 46 | + |
| 47 | + async send(msg: QueueMessage) { |
| 48 | + if (this.enableRegionalCache) { |
| 49 | + const isCached = await this.isInCache(msg); |
| 50 | + if (isCached) { |
| 51 | + return; |
| 52 | + } |
| 53 | + if (!this.waitForQueueAck) { |
| 54 | + await this.putToCache(msg); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + await this.originalQueue.send(msg); |
| 59 | + if (this.waitForQueueAck) { |
| 60 | + await this.putToCache(msg); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private async getCache() { |
| 65 | + if (!this.cache) { |
| 66 | + this.cache = await caches.open("durable-queue"); |
| 67 | + } |
| 68 | + return this.cache; |
| 69 | + } |
| 70 | + |
| 71 | + private getCacheKey(msg: QueueMessage) { |
| 72 | + return new Request( |
| 73 | + new URL(`queue/${msg.MessageGroupId}/${msg.MessageDeduplicationId}`, "http://local.cache") |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + private async putToCache(msg: QueueMessage) { |
| 78 | + const cacheKey = this.getCacheKey(msg); |
| 79 | + const cache = await this.getCache(); |
| 80 | + await cache.put( |
| 81 | + cacheKey, |
| 82 | + new Response(null, { status: 200, headers: { "Cache-Control": `max-age=${this.regionalCacheTtlSec}` } }) |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + private async isInCache(msg: QueueMessage) { |
| 87 | + const cacheKey = this.getCacheKey(msg); |
| 88 | + const cache = await this.getCache(); |
| 89 | + return await cache.match(cacheKey); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +export default (originalQueue: Queue, opts: QueueCachingOptions = {}) => new QueueCache(originalQueue, opts); |
0 commit comments