From e40bee5746e0e5ca8d9b16ac2bdecd53108adc57 Mon Sep 17 00:00:00 2001 From: Jared Wray Date: Sat, 12 Oct 2024 09:20:28 -0700 Subject: [PATCH] cacheable - adding hasMany to CacheableMemory --- packages/cacheable/README.md | 1 + packages/cacheable/src/memory.ts | 15 +++++++++++++++ packages/cacheable/test/memory.test.ts | 9 +++++++++ 3 files changed, 25 insertions(+) diff --git a/packages/cacheable/README.md b/packages/cacheable/README.md index d3aa98f6..8e938dd4 100644 --- a/packages/cacheable/README.md +++ b/packages/cacheable/README.md @@ -273,6 +273,7 @@ By default we use lazy expiration deletion which means on `get` and `getMany` ty * `getRaw(key)`: Gets a value from the cache as `CacheableStoreItem`. * `getManyRaw([keys])`: Gets multiple values from the cache as `CacheableStoreItem`. * `has(key)`: Checks if a value exists in the cache. +* `hasMany([keys])`: Checks if multiple values exist in the cache. * `delete(key)`: Deletes a value from the cache. * `deleteMany([keys])`: Deletes multiple values from the cache. * `take(key)`: Takes a value from the cache and deletes it. diff --git a/packages/cacheable/src/memory.ts b/packages/cacheable/src/memory.ts index dde049d5..3ef6696c 100644 --- a/packages/cacheable/src/memory.ts +++ b/packages/cacheable/src/memory.ts @@ -196,6 +196,21 @@ export class CacheableMemory { return Boolean(item); } + /** + * @function hasMany + * @param keys: string[] + * @returns boolean[] + */ + public hasMany(keys: string[]): boolean[] { + const result = new Array(); + for (const key of keys) { + const item = this.get(key); + result.push(Boolean(item)); + } + + return result; + } + public take(key: string): any { const item = this.get(key); if (!item) { diff --git a/packages/cacheable/test/memory.test.ts b/packages/cacheable/test/memory.test.ts index 581d228a..a57252bd 100644 --- a/packages/cacheable/test/memory.test.ts +++ b/packages/cacheable/test/memory.test.ts @@ -193,6 +193,15 @@ describe('CacheableMemory Has', async () => { const cache = new CacheableMemory(); expect(cache.has('key')).toBe(false); }); + test('should return for many keys', () => { + const cache = new CacheableMemory(); + cache.setMany(cacheItemList); + const result = cache.hasMany(['key', 'key1', 'key2', 'key3']); + expect(result[0]).toBe(true); + expect(result[1]).toBe(true); + expect(result[2]).toBe(true); + expect(result[3]).toBe(true); + }); }); describe('CacheableMemory Take', async () => {