diff --git a/docs/docs/api/CacheStore.md b/docs/docs/api/CacheStore.md index 0f3b3eebc7f..00ceb960641 100644 --- a/docs/docs/api/CacheStore.md +++ b/docs/docs/api/CacheStore.md @@ -13,9 +13,9 @@ The `MemoryCacheStore` stores the responses in-memory. **Options** -- `maxSize` - The maximum total size in bytes of all stored responses. Default `Infinity`. -- `maxCount` - The maximum amount of responses to store. Default `Infinity`. -- `maxEntrySize` - The maximum size in bytes that a response's body can be. If a response's body is greater than or equal to this, the response will not be cached. Default `Infinity`. +- `maxSize` - The maximum total size in bytes of all stored responses. Default `104857600` (100MB). +- `maxCount` - The maximum amount of responses to store. Default `1024`. +- `maxEntrySize` - The maximum size in bytes that a response's body can be. If a response's body is greater than or equal to this, the response will not be cached. Default `5242880` (5MB). ### Getters diff --git a/lib/cache/memory-cache-store.js b/lib/cache/memory-cache-store.js index 2fb3dfabd19..dba29ae4de9 100644 --- a/lib/cache/memory-cache-store.js +++ b/lib/cache/memory-cache-store.js @@ -16,9 +16,9 @@ const { assertCacheKey, assertCacheValue } = require('../util/cache.js') * @extends {EventEmitter} */ class MemoryCacheStore extends EventEmitter { - #maxCount = Infinity - #maxSize = Infinity - #maxEntrySize = Infinity + #maxCount = 1024 + #maxSize = 104857600 // 100MB + #maxEntrySize = 5242880 // 5MB #size = 0 #count = 0 diff --git a/test/cache-interceptor/memory-cache-store-tests.js b/test/cache-interceptor/memory-cache-store-tests.js index 740cbdc31ce..e41fb9ad256 100644 --- a/test/cache-interceptor/memory-cache-store-tests.js +++ b/test/cache-interceptor/memory-cache-store-tests.js @@ -7,6 +7,56 @@ const { cacheStoreTests } = require('./cache-store-test-utils.js') cacheStoreTests(MemoryCacheStore) +test('default limits prevent memory leaks', async () => { + const store = new MemoryCacheStore() // Uses new defaults + + // Test that maxCount default (1024) is enforced + for (let i = 0; i < 1025; i++) { + const writeStream = store.createWriteStream( + { origin: 'test', path: `/test-${i}`, method: 'GET' }, + { + statusCode: 200, + statusMessage: 'OK', + headers: {}, + cachedAt: Date.now(), + staleAt: Date.now() + 60000, + deleteAt: Date.now() + 120000 + } + ) + writeStream.write('test data') + writeStream.end() + } + + // Should be full after exceeding maxCount default of 1024 + equal(store.isFull(), true, 'Store should be full after exceeding maxCount default') +}) + +test('default maxEntrySize prevents large entries', async () => { + const store = new MemoryCacheStore() // Uses new defaults + + // Create entry larger than default maxEntrySize (5MB) + const largeData = Buffer.allocUnsafe(5242881) // 5MB + 1 byte + + const writeStream = store.createWriteStream( + { origin: 'test', path: '/large', method: 'GET' }, + { + statusCode: 200, + statusMessage: 'OK', + headers: {}, + cachedAt: Date.now(), + staleAt: Date.now() + 60000, + deleteAt: Date.now() + 120000 + } + ) + + writeStream.write(largeData) + writeStream.end() + + // Entry should not be cached due to maxEntrySize limit + const result = store.get({ origin: 'test', path: '/large', method: 'GET', headers: {} }) + equal(result, undefined, 'Large entry should not be cached due to maxEntrySize limit') +}) + test('size getter returns correct total size', async () => { const store = new MemoryCacheStore() const testData = 'test data'