1.0.9 • Published 4 years ago

cache-framework v1.0.9

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

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

NameCommentRequired
cacheNamecache name to usefalse
keyspecifying the key of the data saved in the cachefalse
ttlttl time, unit: msfalse

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

NameCommentRequired
cacheNamecache name to usefalse
keyspecifying the key of the data saved in the cachefalse
allEntrieswhether remove all entriesfalse
afterInvocationremove after function invocationfalse

@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,
});
1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago