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

@jfkz/ngx-toolkit-cache

v14.0.1

Published

Angular cache with Universal support

Downloads

6

Readme

npm version MIT License Build Status Coverage Join the chat at https://gitter.im/ngx-toolkit/Lobby

@ngx-toolkit/cache

Angular Cache implementation for Browser & Server platforms.

Table of contents:


Installation

Install the npm package.

# To get the latest stable version and update package.json file:
npm install @ngx-toolkit/cache --save
# or
yarn add @ngx-toolkit/cache

Registered CacheModule in the root Module of your application with forRoot(caches: Cache[]) static method.

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { CacheModule, MemoryCache } from '@ngx-toolkit/cache';

import { AppComponent }  from './app.component';

@NgModule({
  imports: [ BrowserModule, CacheModule.forRoot([
    new MemoryCache('myMemoryCache')
  ]) ],
  declarations: [ AppComponent ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

Annotations

CacheDefaults

/**
 * Allows the configuration of defaults for `CacheResult`, `CachePut`, `CacheRemove`, and `CacheRemoveAll` at the class level.
 * Without the method level annotations this annotation has no effect.
 * @param cacheName
 */
@CacheDefaults(cacheName: string)

CacheResult

/**
 * When a method annotated with `CacheResult` is invoked a cache key will be generated
 * and *Cache.get(key)* is called before the annotated method actually executes.
 * If a value is found in the cache it is returned and the annotated method is never actually executed.
 * If no value is found the annotated method is invoked and the returned value is stored in the cache with the generated key.
 *
 * @param params (Optional) {cacheName?: string}
 */
@CacheResult(params?: { cacheName?: string })

CachePut

/**
 * When a method annotated with `CachePut` is invoked a cache key will be generated
 * and *Cache.put(key, value)* will be invoked on the specified cache storing the value marked with `CacheValue`.
 *
 * @param params (Optional) {cacheName?: string, afterInvocation: boolean = true}
 */
@CachePut(params?: {cacheName?: string, afterInvocation: boolean = true})

CacheKey

/**
 * Marks a method argument as part of the cache key.
 * If no arguments are marked all arguments are used.
 * The exception is for a method annotated with `CachePut` where the `CacheValue` parameter is never included in the key.
 */
@CacheKey()

CacheValue

/**
 * Marks the parameter to be cached for a method annotated with `CachePut`.
 */
@CacheValue()

CacheRemove

/**
 * When a method annotated with `CacheRemove` is invoked a cache key will be generated
 * and *Cache.remove(key)* will be invoked on the specified cache.
 * The default behavior is to call *Cache.evict(key)* after the annotated method is invoked,
 * this behavior can be changed by setting *`afterInvocation`* to false in which case *Cache.evict(key)*
 * will be called before the annotated method is invoked.
 *
 * @param params (Optional) {cacheName?: string, afterInvocation: boolean = true}
 */
@CacheRemove(params?: {cacheName?: string, afterInvocation: boolean = true})

CacheRemoveAll

/**
 * When a method annotated with `CacheRemoveAll` is invoked all elements in the specified cache will be removed via the *Cache.clear()* method.
 * The default behavior is to call *Cache.clear()* after the annotated method is invoked,
 * this behavior can be changed by setting *`afterInvocation`* to false in which case *Cache.clear()* will be called before the annotated method is invoked.
 *
 * @param params (Optional) {cacheName?: string, afterInvocation: boolean = true}
 */
@CacheRemoveAll(params?: {cacheName?: string, afterInvocation: boolean = true})

Example:

@CacheDefaults('myCacheBean')
class CacheBean {

  @CachePut()
  myMethod(@CacheKey() id: number, @CacheValue() value: any): void {
    ...
  }

  @CacheResult()
  get(id: number): any {
    ...
  }

  @CacheRemove()
  refresh(id: number): void {
    ...
  }

  @CacheRemoveAll()
  refreshAll(): void {
    ...
  }
}

Cache

MemoryCache

Cache in memory

import { MemoryCache } from '@ngx-toolkit/cache';
new MemoryCache('myCacheName');

StorageCache

Cache in a storage

import { StorageCache } from '@ngx-toolkit/cache';
new StorageCache('myCacheName', window.sessionStorage || window.localStorage);

NoOpCache

No cache, do nothing...

import { NoOpCache } from '@ngx-toolkit/cache';
new NoOpCache('myCacheName');

Custom implementation

You can create your own implementation. You simply need to implement the Cache interface:

export interface Cache {

  /**
   * Return the cache name.
   */
  readonly name: string;

  /**
   * Return the value to which this cache maps the specified key
   */
  get<T>(key: string): T;

  /**
   * Associate the specified value with the specified key in this cache.
   * If the cache previously contained a mapping for this key, the old
   * value is replaced by the specified value.
   */
  put<T>(key: string, value: T): void;

  /**
   * Evict the mapping for this key from this cache if it is present.
   */
  evict(key: string): void;

  /**
   * Remove all mappings from the cache.
   */
  clear(): void;
}

License

© 2018 Dewizz

MIT