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

a4-observable-cache

v8.1.3

Published

Observable Cache

Downloads

9

Readme

A4ObservableCache

a4-observable-cache is an angular caching service for observable. A typical use case is caching HttpClient get for front-end optimization. HttpClient methods return observable and the app will be able to cache the observable based on the arguments and evict the cache when necessary. For example, the app can cache the get method and evict the cache when an update has been done to the same data object.

Install

npm i a4-observable-cache

Example

The API service

@Cache()
getBook(isbn: string) {
    return this.http.get<BookSummary>(`/api/books/${isbn}/summary`);
}

@Uncache({group: 'getBook', key: (isbn: string) => isbn})
updateBook(isbn: string, summary: string) {
    return this.http.put(`/api/books/${isbn}/summary`, {summary});
}

How does it work?

Cache

this.api.getBook(this.isbn).subscribe(bookSummary => console.log(bookSummary));

Calling this repeatedly will produce the same result but the Http Get will only be invoked once. The group of the cache defaults to the method name ie getBook. You may specify a group name by @cache({group: 'book'}). The key defaults to the arguments (as string). In this example the group is getBook and the key is the content of ISBN. Group and the key form a composite key as the hashing key for the cache.

Uncache

@Uncache({group: 'getBook', key: (isbn: string) => isbn})
updateBook(isbn: string, summary: string) {
    return this.http.put(`/api/books/${isbn}/summary`, {summary});
}

Uncache will evict the cache based on the composite key specified by the group and the key. The group is a required field and the key is optional. If there is no key function provided, the arguments to the function will be used to generate the key. In this case, isbn and summary will be used as the key. This will result in no cache being evicted as the composite key used by the cache is getBook (group) and isbn (key). Thus, we need to specify how to generate the key by specifying that we want to use isbn as part of the composite key but not summary. If key resolves to null or an empty string, it will evict the whole group instead. eg.

@Uncache({group: 'getBook', key: () => null})
updateBookSummary(isbn: string, summary: string) {
    return this.http.put(`/api/books/${isbn}/summary`, {summary});
}

@Uncache({group: 'getBook})
deleteBook(isbn: string) {
    return this.http.delete(`/api/books/${isbn}`);
}

Hook Into The Cache System

const original = window['__A4_OBSERVABLE_CACHE_HOOK__'];
    window['__A4_OBSERVABLE_CACHE_HOOK__'] = (eventType: EventType, key: string, cache: CacheStorage) => {
      console.log(`event: ${eventType}   key: ${key}`, Array.from(cache.keys()));
      if (original)
        original(eventType, key, cache);
    };

Service

Injecting the Service

constructor(private cache: ObservableCacheService) { }

Service Interface

init(options: Options<string, unknown>);
set<T>(key: string, value: T);
get<T>(key: string): T;
uncache(group: string, key?: string);
clear();