new LruCache(valueType, maxSize)
Cannot be instantiated directly! Use 'getCache' to get a cache instance.
By default, cache events are dispatched only for inserts/updates and deletes.
To dispatch also LRU removes and/or clear removes, use the corresponding setters.
Parameters:
Name | Type | Description |
---|---|---|
valueType |
string | The value type of this cache |
maxSize |
maxSize | The maximum number of entries for the given value type (default: 500) |
- Source:
Methods
(static) self.clear() → {undefined}
Clear the cache.
A corresponding cache changed event will be dispatched.
- Source:
Returns:
void
- Type
- undefined
(static) self.delete(key) → {boolean}
Delete entry from cache by key.
Here, no alternate key can be used.
A corresponding cache changed event will be dispatched.
Parameters:
Name | Type | Description |
---|---|---|
key |
string | The key of the to be deleted value |
- Source:
Returns:
true, if the key was in the cache.
- Type
- boolean
(static) self.dispatchClearRemoves(newValue) → {undefined}
Set whether the cache should also dispatch events for clear removes
Parameters:
Name | Type | Description |
---|---|---|
newValue |
boolean | true, if clear removes should be dispatched |
- Source:
Returns:
void
- Type
- undefined
(static) self.dispatchLruRemoves(newValue) → {undefined}
Set whether the cache should also dispatch events for LRU removes
Parameters:
Name | Type | Description |
---|---|---|
newValue |
boolean | true, if LRU removes should be dispatched |
- Source:
Returns:
void
- Type
- undefined
(static) self.forEach()
Iterate over the cache from oldest to newest entry.
The given callback gets a cache entry as argument (an object with 'key', 'value' and 'alternateKeys').
- Source:
(static) self.get(keyOrAlternateKey, notFromCache, customEntryGetter) → {value|Promise|undefined}
Get value from cache by either its key or one of its alternate keys (if exists).
If the value is not found in the cache and an entry-getter was set via setEntryGetter, then:
- If the entry getter returns a Promise, a Promise resolving to the value will be returned.
When the Promise resolves, the entry will be set to the cache. Until the Promise is not resolved,
subsequent calls to get will return the same Promise.
- If the entry getter returns a cache entry (keyValueAlternateKeys-object), this will be set to the cache
and the value will be returned.
- If the entry getter returns null or undefined, undefined will be returned.
If the key is not in the cache and no entry getter is set, undefined will be returned.
Makes the corresponding entry the most recently used (use 'getWithoutLruChange' to avoid this).
Parameters:
Name | Type | Description |
---|---|---|
keyOrAlternateKey |
string | The key or alternate key of the value |
notFromCache |
boolean | If true and an entry getter is set, then the value will not be taken from the cache, but from the entry getter. If no entry getter is set, an error will be thrown. (default: false) |
customEntryGetter |
function | function that takes a key as argument and returns corresponding entry or promised entry. Has precedence over entry gettter set via setEntryGetter. |
- Source:
Returns:
value, promised value or undefined
- Type
- value | Promise | undefined
(static) self.getEntries() → {Array}
Get an Array with all cache entries.
- Source:
Returns:
cache entries (objects with 'key', 'value' and 'alternateKeys')
- Type
- Array
(static) self.getMaxSize() → {int}
Return the current max size of this cache.
- Source:
Returns:
max size of this cache
- Type
- int
(static) self.getSize() → {int}
Get the number of currently cached objects.
- Source:
Returns:
current number of entries in this cache
- Type
- int
(static) self.getValueType() → {string}
Get the value type of this cache.
- Source:
Returns:
the value type
- Type
- string
(static) self.getWithoutLruChange(keyOrAlternateKey, notFromCache, customEntryGetter) → {value|Promise|undefined}
Like 'get', but not making the corresponding entry the most recently used.
If the value is retrieved via entry getter, it will of course still become the
most recently used.
Parameters:
Name | Type | Description |
---|---|---|
keyOrAlternateKey |
string | The key or alternate key of the value |
notFromCache |
boolean | If true and an entry getter is set, then the value will not be taken from the cache, but from the entry getter. If no entry getter is set, an error will be thrown. (default: false) |
customEntryGetter |
function | function that takes a key as argument and returns corresponding entry or promised entry. Has precedence over entry gettter set via setEntryGetter. |
- Source:
Returns:
value, promised value or undefined
- Type
- value | Promise | undefined
(static) self.has(keyOrAlternateKey) → {boolean}
Return whether the cache contains an entry for the given key or alternate key
Parameters:
Name | Type | Description |
---|---|---|
keyOrAlternateKey |
string | The entry key or alternate key |
- Source:
Returns:
true, if the given key or alternate key is in the cache
- Type
- boolean
(static) self.set(keyValueAlternateKeys) → {undefined}
Insert or update a cache entry.
If alternate keys are provided and an already existing entry already has alternate keys, these will be extended.
A corresponding cache changed event will be dispatched.
If an insert leads to cache max size being exceeded and dispatchLruRemoves is set to true, the cache change event will contain both, insert and remove.
Parameters:
Name | Type | Description |
---|---|---|
keyValueAlternateKeys |
object | object with 'key' and 'value' and optional 'alternateKeys' |
- Source:
Returns:
void
- Type
- undefined
(static) self.setAll(keyValueAlternateKeysArray) → {undefined}
Insert or update multiple cache entries.
If alternate keys are provided and an already existing entry already has alternate keys, these will be extended.
A corresponding cache changed event will be dispatched.
If inserts lead to cache max size being exceeded and dispatchLruRemoves is set to true, the cache change event will contain both, inserts and removes.
It is even possible that an entry from the inserts is also contained in the removes.
Parameters:
Name | Type | Description |
---|---|---|
keyValueAlternateKeysArray |
Array | array of objects with 'key', 'value' and optional 'alternateKeys' |
- Source:
Returns:
void
- Type
- undefined
(static) self.setAllAsync(keyValueAlternateKeysArray) → {Promise}
Like 'setAll', but returning a Promise that is executed in another event loop.
Parameters:
Name | Type | Description |
---|---|---|
keyValueAlternateKeysArray |
Array | array of objects with 'key', 'value' and optional 'alternateKeys' |
- Source:
Returns:
- Type
- Promise
(static) self.setAsync(keyValueAlternateKeys) → {Promise}
Like 'set', but returning a Promise that is executed in another event loop.
Parameters:
Name | Type | Description |
---|---|---|
keyValueAlternateKeys |
object | object with 'key' and 'value' and optional 'alternateKeys' |
- Source:
Returns:
- Type
- Promise
(static) self.setEntryGetter(newEntryGetter) → {undefined}
Set a getter that can be used to retrieve a cache entry (keyValueAlternateKeys-object) by key in
case it is not yet in the cache.
For values that might be called by alternate key, the getter should also be able to handle this.
Parameters:
Name | Type | Description |
---|---|---|
newEntryGetter |
function | function that takes a key as argument and returns corresponding entry or promise |
- Source:
Returns:
void
- Type
- undefined
(static) self.setMaxSize(newMaxSize) → {undefined}
Set a new max size for this cache.
If this leads to the removal of cache entries and dispatchLruRemoves is set true, a corresponding cache changed event will be dispatched.
Parameters:
Name | Type | Description |
---|---|---|
newMaxSize |
int | the new max number of entries for this cache. |
- Source:
Returns:
void
- Type
- undefined