Caching
Caching plays an important role in repositories.
There are three kinds of caching-repositories:
- Repositories derived from CachingAllRepository
- Repositories derived from CachingSearchRepository
- Repositories with custom caching solutions (like the PortalRepository of GroupHive)
CachingAllRepository
Caching all repositories are repositories that store and connect to data sets that are small enough to be cached in total in the browsers IndexedDB.
For example the areas or component definitions of GroupHive.
If they provide functions that have the "Cached"-prefix (ie: getAllCached), this means the following:
- An observable will be returned that will first have a look in the cache:
- If the requested resource is not found, a http request will be generated to request the missing resource. The result of the request will be emitted.
- If the requested resource is found, this resource will be emitted
- If the "renewCache"-option is set, a http request will be generated to request a potentially changed resource. If the new value is different from the cached value, the value will be emitted also (distinctUntilChanged)
This logic is for example implemented in the getAllEntitiesCached function in the CachingAllRepository.
The renewCache option
This option is disabled by default, so you explicitly need to request that the cache is renewed after a value is returned from it.
If functions do not provide a renewCache option they are not renewing the cache like getEntityByIdCached that is explained next.
Exceptions from this rule need to be documented inside the repository.
For example look at the comment in PortalRepository (GroupHive)
Getting single values from cache
Sometimes just a single value is required from the cache.
In this case the repositories can contain functions that get just a single value matching a certain criteria.
For example getEntityByIdCached is a default-function of CachingAllRepository<EntityType>.
Those functions should only be called if the cache is up-to-date, because they will not renew the cache. They will just have a look in the cache of the existing entities and emit a found result.
If those functions return "undefined", this can mean two things:
- The value does not exist at all (if cache is up-to-date)
- The value does not exist in cache
To avoid this double semantics, please call the function only if you are sure the cache is up-to-date. In this way you avoid the second point to happen.