8.0.3 • Published 1 year ago

@appolo/cache v8.0.3

Weekly downloads
94
License
MIT
Repository
github
Last release
1 year ago

Cache module for appolo.

Cache methods results using appolo-cache​ with optional redis store

Installation

npm i @appolo/cache

Options

keyDescriptionTypeDefault
idcacheProvider injector idstringcacheProvider
connectionredis connection stringstring
memorytrue to use memory storebooleantrue
dbtrue to use redis storebooleanfalse
maxSizemax memory store itemsnumber1000
keyPrefixredis prefix keystringc

all option are optional and will be added as defaults to cache options in config/modules/all.ts

import {CacheModule} from '@appolo/cache';

export = async function (app: App) {
    await app.module(new CacheModule({maxSize:100}));

   // or with redis store
   await app.module(new CacheModule({db:true,connection:"redis://redis-connection-string"}));

}

Cache Options

keyDescriptionTypeDefault
idcustom cache idstringclassName_methodName
maxSizemax cache sizenumber1000
maxAgeset maximum age in ms of all cache itemsnumberunlimited
cloneclone the cache resultbooleanfalse
intervalset cache refresh interval in msnumberundefined
resolverfunction to get the cache key by default the first argument will be used as the cache key.functionundefined
multiif no resolver defined use all the arguments as key else use the first argument as keybooleanfalse
peekuse peek method instead of getbooleanfalse
refreshrefresh cache on half maxAge expirebooleanfalse
keyPrefixredis prefix keystringc
memorytrue to use memory storebooleantrue
dbtrue to use redis storebooleanfalse
dbMaxAgeset maximum age in ms of all cache items in db if not defined maxAge will be usednumberunlimited

Usage

import { cache,define } from 'appolo';

@define()
export class SomeClass {
    private counter = 0;

    @cache()
    method() {
       return ++this.counter
    }

    // will be refreshed every 5 sec
    @cache({interval:5000})
    async method2(key:string) {
        let result = await doSomeThingAsync(key)
        return result;
    }

     // will try to get items from memroy with expire
     // of 1 minute then from redis with expire of one hour
    @cache({db:true,maxAge:60*1000,:dbMaxAge:60*1000*60})
        async method2(key:string) {
            let result = await doSomeThingAsync(key)
            return result;
        }
}

CacheProvider

createCache(options: ICacheOptions, valueFn: Function, scope?: any)

create new cache wrapper

  • options - cache options
  • valueFn - value function will be called to get the value
  • scope - scope of the value function

getCacheById(id:string):Cache

return cache wrapper by id

Cache

cache wrapper instance

get<T>(...args: any[]): Promise<T> | T

get value from cache if not found the value fn will be called

get cache

return appolo-cache​ instance

8.0.3

1 year ago

8.0.2

3 years ago

8.0.1

3 years ago

8.0.0

3 years ago

7.0.16

3 years ago

7.0.15

4 years ago

7.0.14

4 years ago

7.0.13

4 years ago

7.0.11

4 years ago

7.0.10

4 years ago

7.0.8

4 years ago

7.0.9

4 years ago

7.0.7

4 years ago

7.0.6

4 years ago

7.0.5

4 years ago

7.0.4

4 years ago

7.0.3

4 years ago

7.0.2

4 years ago

7.0.1

4 years ago

7.0.0

4 years ago

0.0.40

4 years ago

0.0.39

4 years ago

0.0.38

4 years ago

0.0.37

5 years ago

0.0.36

5 years ago

0.0.35

5 years ago

0.0.34

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago