A Simple Async Cache Module In Angular 6+ Apps

angular-async-cache is a simple utility to help cache promises and notes to enable an easy offline first approach in angular 6+ applications.

async data binding angular, async calls in angular, is angular subscribe async, import async in angular, angular use async pipe in component, angular async pipe input

How to make use of it:


npm install --save angular-async-cache

1. This setting will first release the cached data (for faster load times + first support offline), then it will find and resend live data and refresh the cache for future requests.

import { NgModule, Component, Injectable } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import {
} from 'angular-async-cache';

export function asyncCacheOptionsFactory(): AsyncCacheOptions {
  return new AsyncCacheOptions({
    // Default cache driver to use. Default in memory.
    // You can also roll your own by implementing the CacheDriver interface
    driver: new LocalStorageDriver(),

    // this is the special sauce - first emit the data from localstorage,
    // then re-fetch the live data from the API and emit a second time.
    // The async pipe will then re-render and update the UI. Default: false
    fromCacheAndReplay: true

// declare in your module
  imports: [
    // this configures the default options. Just using `AsyncCacheModule.forRoot()` will use
    // the defaults of an in memory cache and not replaying from the api
      provide: AsyncCacheOptions,
      useFactory: asyncCacheOptionsFactory
class MyModule {}

// finally use with the async pipe in your components template
  template: `
    <div *ngFor="let car of cars | async">
      {{ car.model }}
class MyComponent {
  cars: Observable<Car[]>;

  constructor(private cachedHttp: CachedHttp) {
    // only the get method is supported (as other http verbs are destructive)
    // The second argument can be any options you would pass to a normal http get call
    // The third argument is a `AsyncCacheOptions` subset
    this.cars = this.cachedHttp.get('/cars');

2. There is also a low-level AsyncCache service that you can use to manually control the caching of notes or promises.

class CarService {
    private http: Http,
    private asyncCache: AsyncCache,
    private memoryDriver: MemoryDriver
  ) {}

  getCars(): Observable<Car[]> {
    const cars$: Observable<Car[]> = this.http.get('/cars');
    return asyncCache.wrap(cars$, '/cars', {
      driver: this.memoryDriver // override the default and cache the data in memory

3. There is also a pipe that you can use to instantiate the spool in your template.

  template: `
    <div *ngFor="let car of cars | asyncCache:'/cars' | async">
      {{ car.model }}
class MyComponent {
  cars: Observable<Car[]>;

  constructor(http: Http) {
    this.cars = http.get('/cars');

angular 4+ async cache module, angular async cache Plugin/Github, angular async promise, async observable

See Demo And Download

Official Website(mattlewis92): Click Here

This superior jQuery/javascript plugin is developed by mattlewis92. For extra advanced usage, please go to the official website.

Related Posts


Google Translate Dropdown Customize With Country Flag | GT API

Flag google translates jQuery text that takes advantage of the Google Cloud Translation API to translate web content between languages by selecting a country from the dropdown…


HTML 5 File Input Optimized for Bootstrap 4.x./3.x with File Preview | Bootstrap Fileinput

bootstrap-fileinput is an improved HTML 5 file input  Bootstrap 5.x, 4.x and 3.x with file preview for different files, provides multiple selections, resumable section uploads, and more….


CSS Layout Components Horizontal/Vertical Stack | HStack and VStack

HStack and VStack in CSS – CSS layout components that (basically) stack anything horizontally and vertically. A pure CSS library that makes it easy to stack elements…


How to Add Floating Whatsapp Chat Button In HTML | venom-button

Venom Button is a very simple plugin for the jQuery floating WhatsApp button. Adds a floating button to your site that calls WhatsApp Click to Chat API. It will automatically start the WhatsApp…


Interactive Data Table Generator with JS/jQuery and JSON | Tabulator

Tabulator allows you to create interactive tables in seconds from any HTML Table, JavaScript array, AJAX data source, or JSON format data. Just include the library in your…


Simple Alert, Confirm, Prompt Popup Using Vanilla JavaScript Library | attention.js

JavaScript provides various built-in functionality to display popup messages for different purposes. Attention JS is a vanillaJS plugin used to create a custom alert, confirm, or Prompt…

Leave a Reply

Your email address will not be published. Required fields are marked *