This module provides a wrapper for Cache data structures to add a feature of persistent data storing.
To describe how long should keep an item in the persistent cache, use the persistentTTL parameter.
The value for persistentTTL should be provided in milliseconds.
// Cause we use the same instance for the local data storing, // this cache will have all values from the previous (it will be loaded from the storage during initialization)
The option specifies the default TTL to keep items within the persistent storage in milliseconds.
This value is used when you don't provide the persistentTTL parameter when saving an item.
// If we "reload" the cache from the storage by using the browser reloading or another way, // these saved values can be "restored" from the storage only for the next 60 seconds awaitpersistentCache.set('foo', 'bar'); awaitpersistentCache.set('foo2', 'bar2');
There is more than one way to initialize a cache from persistent storage.
The most obvious way to do it is to load all data from the storage to RAM during the cache's initialization.
This strategy is simple but not effective cause if we can have a huge amount of data in the storage,
so we have to load it all at the same time. This can be very expensive. We need another way.
But what if we load an item from the cache only when it is requested the first time.
In that case, we haven't to load the whole stored data on cache initialization,
but all cache methods will change API - they will become return promises instead of the raw results.
Some consumers cannot be ready for changing API, so there is no silver bullet. We have to keep both strategies.
The whole stored data will be loaded during the cache initialization
Notice, cause the loading from the storage is an asynchronous operation, addPersistent will return a promise
if used the onInit strategy.
Each stored item will be loaded from the cache only on the first touch and only if there is no internet connection.
The strategy is useful to create net-first offline storages.
// Because we use the same instance for the local data storing, // this cache will have all values from the previous (it will be loaded from the storage during initialization)
core/cache/decorators/persistent
This module provides a wrapper for Cache data structures to add a feature of persistent data storing. To describe how long should keep an item in the persistent cache, use the
persistentTTL
parameter. The value forpersistentTTL
should be provided in milliseconds.Example
Options
persistentTTL
The option specifies the default TTL to keep items within the persistent storage in milliseconds. This value is used when you don't provide the
persistentTTL
parameter when saving an item.loadFromStorage
There is more than one way to initialize a cache from persistent storage. The most obvious way to do it is to load all data from the storage to RAM during the cache's initialization. This strategy is simple but not effective cause if we can have a huge amount of data in the storage, so we have to load it all at the same time. This can be very expensive. We need another way. But what if we load an item from the cache only when it is requested the first time. In that case, we haven't to load the whole stored data on cache initialization, but all cache methods will change API - they will become return promises instead of the raw results. Some consumers cannot be ready for changing API, so there is no silver bullet. We have to keep both strategies.
onInit
The whole stored data will be loaded during the cache initialization Notice, cause the loading from the storage is an asynchronous operation,
addPersistent
will return a promise if used theonInit
strategy.onDemand
Each stored item will be loaded from the cache only on the first touch, i.e. on-demand or lazily. This is the default strategy.
onOfflineDemand
Each stored item will be loaded from the cache only on the first touch and only if there is no internet connection. The strategy is useful to create net-first offline storages.