Skip to content

DagonMetric/ng-cache

Repository files navigation

Caching Service for Angular

GitHub Actions Status Azure Pipelines Status Gitter

Caching service for Angular applications.

Get Started

Installation

npm

npm install @dagonmetric/ng-cache

or yarn

yarn add @dagonmetric/ng-cache

Latest npm package is npm version

Module Setup (app.module.ts)

import { CacheLocalStorageModule, CacheModule, MemoryCacheModule } from '@dagonmetric/ng-cache';

@NgModule({
  imports: [
    // Other module imports

    // ng-cache modules
    CacheModule,
    CacheLocalStorageModule,
    MemoryCacheModule
  ]
})
export class AppModule { }

Live edit app.module.ts in stackblitz

Usage (app.component.ts)

import { HttpClient } from '@angular/common/http';
import { Component, ViewEncapsulation } from '@angular/core';

import { Observable } from 'rxjs';

import { CacheService } from '@dagonmetric/ng-cache';

export interface AppOptions {
  name: string;
  lang: string;
  logEnabled: boolean;
  logLevel: number;
  num: number;
  arr: string[];
  child: {
    key1: string;
    key2: boolean;
  };
}

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class AppComponent {
  appOptions$: Observable<AppOptions>;

  constructor(private readonly cacheService: CacheService, private readonly httpClient: HttpClient) {
    this.appOptions$ = this.cacheService.getOrSet('configuration', () => {
      return this.httpClient.get<AppOptions>(
        'https://us-central1-ng-config-demo.cloudfunctions.net/configuration'
      );
    });
  }
}

Live edit app.component.ts in stackblitz

Samples

Build & Test Tools

We use lib-tools for bundling, testing and packaging our library projects.

Lib Tools

Feedback and Contributing

Check out the Contributing page.

License

This repository is licensed with the MIT license.