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

cache-framework

v1.0.9

Published

A easy-to-use cache resolution for node application inspired by spring cache. The caching abstraction allows consistent use of various caching solutions with minimal impact on the code.

Downloads

2

Readme

node-cache-framework

GitHub stars GitHub forks license Build Status Coverage Status

A easy-to-use cache resolution for node application inspired by spring cache. The caching abstraction allows consistent use of various caching solutions with minimal impact on the code.

LICENSE

NOTE: This project is licensed under The MIT License. Completely free, you can arbitrarily use and modify this plugin. If this plugin is useful to you, you can Star this repo, your support is my biggest motive force, thanks.

How to use it

  1. install package

    yarn add cache-framework
    // npm install --save cache-framework
  2. enable cache-framework in your application

    EnableCaching({
    ttl:1000*60,
    })
  3. Add annotation for your service

    @CacheConfig('user')
    class UserService{
    
        @Cacheable()
        getUser(){}
    }

example

You could check these 2 example below to get started.

Core API

Declarative annotation-based caching

For caching declaration, the abstraction provides a set of js annotations:

  • @CacheConfig shares some common cache-related settings at class-level
  • @Cacheable triggers cache population
  • @CacheEvict triggers cache eviction
  • @CachePut updates the cache without interfering with the method execution Let us take a closer look at each annotation:

@CacheConfig annotation

specifying the name of the cache to use for every cache operation of the class. This is where @CacheConfig comes into play.

@CacheConfig("hello")
export class UserService {}

params

  • name: cache name to use, if not specified, the default cache will be used.

@Cacheable annotation

As the name implies, @Cacheable is used to demarcate methods that are cacheable - that is, methods for whom the result is stored into the cache so on subsequent invocations (with the same arguments), the value in the cache is returned without having to actually execute the method.

    @Cacheable({key: '${id}_${name}'})
    find(id: number, name: string) {
        console.log('load from method.')
        return 'hello'
    }

params

| Name | Comment | Required | | --------- | ------------------------------------------------- | -------- | | cacheName | cache name to use | false | | key | specifying the key of the data saved in the cache | false | | ttl | ttl time, unit: ms | false |

key example

  • ${id}
  • ${id}_${user.name}

@CacheEvict annotation

The cache abstraction allows not just population of a cache store but also eviction. This process is useful for removing stale or unused data from the cache. Opposed to @Cacheable, annotation @CacheEvict demarcates methods that perform cache eviction, that is methods that act as triggers for removing data from the cache.

params

| Name | Comment | Required | | --------------- | ------------------------------------------------- | -------- | | cacheName | cache name to use | false | | key | specifying the key of the data saved in the cache | false | | allEntries | whether remove all entries | false | | afterInvocation | remove after function invocation | false |

@CachePut

This annotation is used to remove unused entry from the cache for a given key.

params | Name | Comment | Required | | --------- | ------------------------------------------------- | -------- | | cacheName | cache name to use | false | | key | specifying the key of the data saved in the cache | false |

customize cache

you could use your cache implementation via implement Cache interface.

import {Cache} from 'cache-framework'

class PromiseCache implements Cache {

    readonly name: string;
    private cache: Map<string, any> = new Map<string, any>();
    private lock: boolean;

    constructor(name: string) {
        this.name = name;
    }

    keys(): Promise<string[]> {
        const self = this
        return new Promise((resolve, reject) => {
            setInterval(() => {
                if (!self.lock) {
                    resolve(Array.from(self.cache.keys()));
                }
            }, 100)
        })
    }


    clear(): void {
        this.cache.clear();
    }

    evict(key: string): void {
        this.lock = true
        setTimeout(() => {
            this.cache.delete(key);
            this.lock = false
        }, 100)
    }

    get<T>(key: string): Promise<T> {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve(this.cache.get(key))
            }, 100)
        })
    }

    put<T>(key: string, value: T): void {
        this.lock = true
        setTimeout(() => {
            this.lock = false
            this.cache.set(key, value);
        }, 100)
    }

}

and specifying the cache implement when EnableCaching

EnableCaching({
  cache: PromiseCache,
});