Skip to content

CustomLocalStorage

Mihael Safaric edited this page Aug 11, 2022 · 3 revisions

Custom local storage

By default, the library is using SimpleHalStorage which stores models locally and overrides them every time a new version of the resource is fetched.

If cacheStrategy is set to CacheStrategy.ETAG, e-tag based caching is used, see more in Etag based caching docs.

When there is a need for custom behaviour of local storage, a custom implementation of HalStorage can be provided in DatastoreConfig decorator:

@DatastoreConfig({
	...
  cacheStrategy: CacheStrategy.CUSTOM,
	storage: new SomeCustomStorage()
})

In this case, cacheStrategy must be set to CacheStrategy.CUSTOM, and an instance of HalStorage must be provided in storage property.

class SomeCustomStorage extends HalStorage {}

Use case #1

If there is a need for some requests to be returned from local cache instead of making a request, SomeCustomStorage could implement makeGetRequestWrapper in a way that for those requests intercepts the original request, cancels it, and returns the value from the local cache.

Use case #2

One of the implementations of a custom HalStorage can be seen in Cacheable storage pull request