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

@d4h/angular-http-cache

v1.1.2

Published

Network caching interceptor and service for Angular HttpClient.

Downloads

11

Readme

GitHubStatus for D4H/angular-http-cache npm

@d4h/angular-http-cache

angular-http-cache offers a pair of tools:

  • A network caching interceptor for Angular's HttpClient. It works by storing request success request responses in an internal cache, and returning this response from the cache so long as the TTL has not expired.
  • An Intersection Observer service to test for element visibility.

You really should not ever have need to either of these tools. Conventional HTTP cache mechanisms are frankly superior to the interceptor, and ng-lazyload-image offers more robust lazy loading. You should only have to resort these tools in special circumstances. What these are you will know best yourself.

Installation

npm install --save @d4h/angular-http-cache

Configuration

angular-http-cache does not work without further configuration. It requires a finder function which accepts a HttpRequest and returns an object with two pieces of data:

  1. A string key, under which to store the request.
  2. An integer ttl in seconds, how long to store and return a successful response. Error responses are never cached, which permits retry.

Both the key and TTL are entirely up to the implementing developer.

export interface HttpRequestCache {
  key: string;
  ttl: number;
}

export interface HttpCacheConfig {
  finder(req: HttpRequest<any>): HttpRequestCache;
}

Configuration Example

In this example, the key is the full request URL, while TTL comes from a regular expression test. Requests to /foo/bar/:id will be cached for 300 seconds, and requests to /fizz/buzz/:id cached for 10.

import { HttpCacheConfig, HttpCacheModule, HttpRequestCache } from '@d4h/angular-http-cache';

export interface CachedRoute {
  regex: RegExp;
  ttl: number;
}

export const cachedRoutes: Array<CachedRoute> = [
  { regex: /\/foo\/bar\/\d+/, ttl: 300 },
  { regex: /\/fizz\/buzz\/\d+/, ttl: 10 }
];

export const config: HttpCacheConfig = {
  finder(req: HttpRequest<any>): HttpRequestCache {
    const route = cachedRoutes.find((r: CachedRoute) => r.regex.test(req.url));

    return {
      key: req.url,
      ttl: route ? route.ttl : null
    };
  }
};

@NgModule({
  imports: [
    HttpCacheModule.forRoot(config)
  ]
})
export class AppInterceptorModule {}

Element Interserction Observer Service

IntersectionService is quite straigtfoward: import it and pass in any ElementRef. In the below example GooseComponent performs mischief when it is visible.

import { IntersectionService } from '@d4h/angular-http-cache';

export class GooseComponent implements OnInit {
  mischief$: Observable<Mischief>;

  constructor(
    private readonly gooseService: GooseService,
    private readonly host: ElementRef,
    private readonly intersectionService: IntersectionService
  ) {}

  ngOnInit(): void {
    this.mischief$ = this.intersectionService.visible(this.host).pipe(
      filter(Boolean),
      switchMap(() => this.gooseService.mischief(this.goose).pipe(
        startWith({ type: 'honk' })
      )),
    );
  }
}

Support and Feedback

Feel free to email [email protected], open an issue or tweet @d4h.

License

Copyright (C) 2019 D4H

Licensed under the MIT license.