npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rx-cache-observer

v1.0.14

Published

This package is intended to make caching easier for Angular HttpClient calls. It can be used to ensure a user interface is presented as quickly as possible and it can minimize API calls to a backend.

Downloads

8

Readme

Cache Observer

This package is intended to make caching easier for Angular HttpClient calls. It can be used to ensure a user interface is presented as quickly as possible and it can minimize API calls to a backend.

Installation

npm install rx-cache-observer

Adding To A Project

In the code where you are using HttpClient add the following import:

import { CacheObserver, CacheOptions, CacheStrategy, CacheStorageProvider } from 'rx-cache-observer';`

Lets add caching to this API call:

  public getJoke(): Observable<any> {
    const url = 'https://api.chucknorris.io/jokes/random';
    return this.httpClient.get(url);
  }

We add the function CacheObserver which takes a key (in this case the url), our original observable and a caching strategy:

  public getJoke(): Observable<any> {
    const url = 'https://api.chucknorris.io/jokes/random';
    return CacheObserver(url, this.httpClient.get(url), CacheStrategy.OneMinute);
  }

Now, whenever our api call to getJoke is made it will get a joke from the server the first time, the second time it is called it will return the cached joke. If it has been longer than 1 minute the joke will be obtained from the server again.

Caching Observer

CacheObserver takes 3 parameters:

  • key - This is a unique string to identify the cached data. For example if your api gets orders then you can name this orders. If there are multiple
  • _observable - This is the observable you want to cache values from. Typically this will be for get API calls.
  • options - This is an optional parameter of type CacheOptions which specifies how values are cached.
  • storageProvider - This is an optional parameter of type CacheStorageProvider (see below)

Caching Options

Here are some examples of CacheOptions you can use:

CacheStrategy.OneMinute

This CacheOptions object will cache values for at most 1 minute.

CacheStrategy.OneHour

This CacheOptions object will cache values for at most 1 hour.

CacheStrategy.OneDay

This CacheOptions object will cache values for at most 1 day.

CacheStrategy.Fresh

This CacheOptions object will cache values and will emit the cached value, it will also call the API and also emit the new value if the new value is different. This means that your observable may emit 2 values. This strategy is great if you always want to display data to the user as fast as possible and ensure that if there is new data it is also shown.

{ expiresMs: 5000 }

This CacheOptions object will cache values for 5 seconds. The value for expiresMs is in milliseconds.

{ alwaysGetValue: true }

This CacheOptions object will emit the cached value then get the new value and emit to also.

{ alwaysGetValue: true, emitDuplicates: false }

This CacheOptions object is the same as CacheStrategy.Fresh: It will emit the cached value and get a new value which if the cached value and new value are different then it will emit the new value as well.

Examples

Always Cached

  public getJoke(): Observable<any> {
    return CacheObserver(url, this.httpClient.get('https://api.chucknorris.io/jokes/random')}

Fast and Fresh

Return cached data but also get fresh data and return that too.

public getJoke(): Observable<any> {
  return CacheObserver(url, this.httpClient.get('https://api.chucknorris.io/jokes/random'), { alwaysGetValue: true });
}

Cache for 30 seconds

Return cached data if not requested in 30 seconds.

public getJoke(): Observable<any> {
  return CacheObserver(url, this.httpClient.get('https://api.chucknorris.io/jokes/random'), { expiresMs: 30000 });
}

Creating a Custom Storage Provider

If a cache storage provider is not specified then cached values are stored in memory.

A custom CacheStorageProvider can be created to provide an alternative way of storing cached values. The below example shows how to store values in local storage.

private storageProvider: CacheStorageProvider = {
    readValue: this.read,
    writeValue: this.write
};;

read(key: string): CacheValue {
  try {
    return JSON.parse(localStorage[key]);
  } catch {
    return undefined;
  }
}

write(key: string, value: CacheValue) {
  localStorage[key] = JSON.stringify(value);
}

public getJoke(): Observable<any> {
  return CacheObserver(
    'joke',
    this.httpClient.get('https://api.chucknorris.io/jokes/random'), 
    CacheStrategy.OneMinute, 
    this.storageProvider)
    ;
}