Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/docs/api/CacheStore.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions lib/cache/memory-cache-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions test/cache-interceptor/memory-cache-store-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Loading