Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cacheable - adding hasMany to CacheableMemory #833

Merged
merged 1 commit into from
Oct 12, 2024
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
1 change: 1 addition & 0 deletions packages/cacheable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions packages/cacheable/src/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>();
for (const key of keys) {
const item = this.get(key);
result.push(Boolean(item));
}

return result;
}

public take<T>(key: string): any {
const item = this.get(key);
if (!item) {
Expand Down
9 changes: 9 additions & 0 deletions packages/cacheable/test/memory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
Loading