2.0.2 • Published 9 months ago

ts-cache-decorator v2.0.2

Weekly downloads
1
License
ISC
Repository
-
Last release
9 months ago

ts-cache-decorator

usage:

only support async method. because the cache store is async.
if used on sync method, it will be cast to async function

0. install

yarn/npm install ts-cache-decorator

1. simple

import { CacheMethod } from 'ts-cache-decorator';

class ClassA {
 
  @CacheMethod({ ttl: 60 })
  async methodA(){
    return /*some data will be cached*/
  }

}

2. custom key or cache store

cache store support redis and built in memory

import { CacheMethod ,setDefaultStore,RedisCacheStore } from 'ts-cache-decorator';
import * as redis from 'redis'
function keyGenerator(objId: string, methodName: string, args: any[]){
  return 'some cache key'
}
const store = new RedisCacheStore(redis.createClient('redis://127.0.0.1:6379'))
// change default cache store
setDefaultStore(store)

class ClassA {
  
  // use special store
  @CacheMethod({ ttl: 60,key:keyGenerator },store)
  async methodA(){
    return /*some data will be cached*/
  }
  // use default cache store
  @CacheMethod({ ttl: 60 })
  async methodB(){
    return /*some data will be cached*/
  }

}

3 store usage

import { CacheMethod ,setDefaultStore,RedisCacheStore } from 'ts-cache-decorator';
import * as redis from 'redis'

const store = new RedisCacheStore(redis.createClient('redis://127.0.0.1:6379'))

await store.set('xxx',3600,{a:1}) //缓存一小时
const obj = await store.get('xxx')
2.0.2

9 months ago

2.0.1

9 months ago

2.0.0

9 months ago

1.0.6

5 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago