@fullerstack/ngx-cachify
A simple Angular caching store that can also fetch and cache http requests
Overview
Description
This library helps implementing a single source of truth with http fetch capabilities.
@fullerstack/ngx-cachify attempts to streamline the fetching and caching http request, with immutability of data of your Angular application, while promoting DRY DRY.
How to install
npm i @fullerstack/ngx-cachify |OR| yarn add @fullerstack/ngx-cachify
How to use
import { ApplicationConfig } from '@fullerstack/ngx-config';
import { CachifyConfig, CachifyFetchPolicy } from '@fullerstack/ngx-cachify';
const cachify: CachifyConfig = {
disabled: false,
policies: [CachifyFetchPolicy.CacheFirst],
immutable: true,
ttl: 30,
} as const;
export const environment: Readonly<ApplicationConfig> = {
version: '0.0.1',
production: true,
appName: 'FullerStack',
cachify,
} as const;
import { CfgModule } from '@fullerstack/ngx-config';
import { CachifyModule, CachifyInterceptor } from '@fullerstack/ngx-cachify';
import { environment } from '../environments/environment';
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
CfgModule.forRoot(environment),
CachifyModule,
],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: CachifyInterceptor,
multi: true
},
],
bootstrap: [AppComponent],
})
export class AppModule {}
import { Component } from '@angular/core';
import {
ConfigService,
DefaultApplicationConfig,
makeCachifyContext,
CachifyFetchPolicy,
CACHIFY_AUTO_KEY } from '@fullerstack/ngx-config';
import { CachifyService } from '@fullerstack/ngx-cachify';
@Component({
selector: 'fullerstack-root',
template: `<h1>Welcome to {{ title }}!</h1>`,
})
export class AppComponent {
title = 'Fullerstack';
constructor(
readonly config: ConfigService,
readonly cachify: CachifyService
) {
this.fetchSomeData();
setTimeout(() => {
this.fetchSomeData();
}, 2900)
setTimeout(() => {
this.fetchSomeData();
}, 3100)
}
fetchSomeData() {
const context: CachifyContextMeta = makeCachifyContext({
policy: CachifyFetchPolicy.CacheFirst
ttl: 60,
key: CACHIFY_AUTO_KEY
});
this.httpClient.get('/api/some-data', { context }).pipe(first()).subscribe(data => {
console.log(data)
});
}
}
Advanced Usage
Available Network Policies
By default all the following policies are enabled. You can also only enable a limited number of polices as per your requirements.
export enum CachifyFetchPolicy {
CacheOff = 'cache-off',
CacheOnly = 'cache-only',
CacheFirst = 'cache-first',
NetworkOnly = 'network-only',
NetworkFirst = 'network-first',
CacheAndNetwork = 'cache-and-network',
}
Custom cache key per each call
Should you require a custom key make specifically for a http call, you can create such key and pass it through as follow.
const cacheKey = new OrderedStatePath()
.append('user', 1000)
.append('portfolio', 2)
.append('symbols', 'all')
.toString();
const context: CachifyContextMeta = makeCachifyContext({
policy: CachifyFetchPolicy.NetworkFirst,
ttl: 60,
key: cacheKey
});
this.httpClient.get('/api/some-data', { context }).pipe(first()).subscribe(data => {
console.log(data)
});
License
Released under a (MIT) license.
Version
X.Y.Z Version
`MAJOR` version -- making incompatible API changes
`MINOR` version -- adding functionality in a backwards-compatible manner
`PATCH` version -- making backwards-compatible bug fixes