@luvio/service-cache
Advanced tools
Comparing version 5.25.0 to 5.26.0
@@ -48,2 +48,3 @@ import { type Key, type KeySet } from './keys'; | ||
record(): RecordableCache; | ||
filter(predicate: FilterPredicate): FilteredCache; | ||
}; | ||
@@ -55,1 +56,3 @@ export type RecordableCache = Cache & { | ||
}; | ||
export type FilteredCache = Cache; | ||
export type FilterPredicate = (key: Key, value: CacheEntry<unknown>) => boolean; |
@@ -1,3 +0,3 @@ | ||
import { type CacheEntry, type Cache, type RecordableCache } from './cache'; | ||
import { type Key, type KeySet } from './keys'; | ||
import type { CacheEntry, Cache, FilteredCache, RecordableCache, FilterPredicate } from './cache'; | ||
export declare class DefaultCache implements Cache { | ||
@@ -30,2 +30,3 @@ constructor(); | ||
record(): RecordableCache; | ||
filter(predicate: FilterPredicate): FilteredCache; | ||
} |
@@ -100,5 +100,58 @@ /** | ||
record() { | ||
return this.baseCache.record(); | ||
return new DefaultRecordableCache(this); | ||
} | ||
filter(predicate) { | ||
return new DefaultFilteredCache(this, predicate); | ||
} | ||
} | ||
/** | ||
A utility class for filtering cache entries based on a given predicate. | ||
*/ | ||
class DefaultFilteredCache { | ||
constructor(baseCache, predicate) { | ||
this.baseCache = baseCache; | ||
this.predicate = predicate; | ||
} | ||
delete(key) { | ||
this.baseCache.delete(key); | ||
} | ||
get(key) { | ||
const result = this.baseCache.get(key); | ||
// if we're chaining filtered caches together, then it's possible the result will be undefined here | ||
if (result && this.predicate(key, result)) { | ||
return result; | ||
} | ||
return undefined; | ||
} | ||
set(key, value) { | ||
this.baseCache.set(key, value); | ||
} | ||
length() { | ||
return this.getFilteredKeys().length; | ||
} | ||
keys() { | ||
return this.getFilteredKeys(); | ||
} | ||
toKeySet(keys) { | ||
return this.baseCache.toKeySet(keys); | ||
} | ||
record() { | ||
return new DefaultRecordableCache(this); | ||
} | ||
filter(predicate) { | ||
return new DefaultFilteredCache(this, predicate); | ||
} | ||
getFilteredKeys() { | ||
const filteredKeySet = new KeySetImpl(); | ||
this.baseCache | ||
.keys() | ||
.elements() | ||
.forEach((key) => { | ||
if (this.get(key)) { | ||
filteredKeySet.add(key); | ||
} | ||
}); | ||
return filteredKeySet; | ||
} | ||
} | ||
@@ -150,2 +203,5 @@ class DefaultCache { | ||
} | ||
filter(predicate) { | ||
return new DefaultFilteredCache(this, predicate); | ||
} | ||
} | ||
@@ -152,0 +208,0 @@ |
@@ -48,2 +48,3 @@ import { type Key, type KeySet } from './keys'; | ||
record(): RecordableCache; | ||
filter(predicate: FilterPredicate): FilteredCache; | ||
}; | ||
@@ -55,1 +56,3 @@ export type RecordableCache = Cache & { | ||
}; | ||
export type FilteredCache = Cache; | ||
export type FilterPredicate = (key: Key, value: CacheEntry<unknown>) => boolean; |
@@ -1,3 +0,3 @@ | ||
import { type CacheEntry, type Cache, type RecordableCache } from './cache'; | ||
import { type Key, type KeySet } from './keys'; | ||
import type { CacheEntry, Cache, FilteredCache, RecordableCache, FilterPredicate } from './cache'; | ||
export declare class DefaultCache implements Cache { | ||
@@ -30,2 +30,3 @@ constructor(); | ||
record(): RecordableCache; | ||
filter(predicate: FilterPredicate): FilteredCache; | ||
} |
{ | ||
"name": "@luvio/service-cache", | ||
"version": "5.25.0", | ||
"version": "5.26.0", | ||
"private": false, | ||
@@ -33,3 +33,3 @@ "description": "OneStore Cache Service definition", | ||
"dependencies": { | ||
"@luvio/utils": "5.25.0" | ||
"@luvio/utils": "5.26.0" | ||
}, | ||
@@ -36,0 +36,0 @@ "volta": { |
Sorry, the diff of this file is not supported yet
40596
25
551
+ Added@luvio/utils@5.26.0(transitive)
- Removed@luvio/utils@5.25.0(transitive)
Updated@luvio/utils@5.26.0