@mwcp/cache
TypeScript icon, indicating that this package has built-in type declarations

25.2.3 • Public • Published

@mwcp/cache

Declarative Cache Component for Midway.js

GitHub tag Version License ci codecov Conventional Commits lerna

Note

ESM build only, requires @midwayjs >= 3.12 and set "type": "module" in packages.json

Install

npm i @mwcp/cache

Configuration

Update project src/configuration.ts

import { Configuration } from '@midwayjs/decorator'
import * as koa from '@midwayjs/koa'
import * as cache from '@mwcp/cache'

@Configuration({
  imports: [
    koa,
    cache,
  ],
  importConfigs: [join(__dirname, 'config')],
})
export class ContainerConfiguration implements ILifeCycle {
}

Change default config via src/config/config.{default | prod | unittest}.ts

import { CacheManagerConfig } from '@mwcp/cache'

export const cacheManagerConfig: CacheManagerConfig = {
  clients: {
    default: {
      store: 'memory',
      options: {
        max: 512,
        ttl: 10,  // Second!
      },
    }
  }
}

Usage

Normal Cache-Docs-EN

How to determine an object result is from cache?

assert(retrieveCacheMetaFrom(data))

Generation rule of Cache entry

  • none of cacheName and key: {className}.{methodName}
  • cacheName string
    • key string | number | bigint: {className}.{methodName}:{key.toString()}
    • key undefined: {className}.{methodName}
    • key false: no cache operation
    • key KeyGenerator
      • string: {className}.{methodName}:{key.toString()}
      • undefined: {className}.{methodName}
      • false: no cache operation

Generation of Cache entry

  • none of cacheName and key: {className}.{methodName}
  • cacheName string
    • key string | number | bigint: {className}.{methodName}:{key.toString()}
    • key undefined: {className}.{methodName}
    • key false: no cache operation
    • key KeyGenerator
      • undefined: {className}.{methodName}
      • string: {className}.{methodName}:{key.toString()}
      • false: no cache operation

Cacheable Decorator

supports class and method

CacheableArgs Parameters

name type default value
cacheName string | undefined {className}.{methodName}
key string | number | bigint | KeyGenerator | undefined | false undefined
ttl number | undefined 10(sec)
condition CacheConditionFn | boolean | undefined undefined (always cache)
import { Cacheable } from '@mwcp/cache'

@Controller('/')
export class FooController {

  async hello(): Promise<string> {
    return this._hello()
  }

  /* cacheName will be `{class name}.{method name}` => "FooController.hello" */
  @Cacheable()
  async _hello(): Promise<string> {
    return 'world'
  }

  @Cacheable({ ttl: 5 })
  async _hello2(): Promise<string> {
    return 'world'
  }
}
import { Cacheable } from '@mwcp/cache'

@Cacheable() 
export class FooService {

  async hello(): Promise<string> {
    return 'hello'
  }

  @Cacheable({ ttl: 5 })  // override parameters of class decorator
  async hello2(): Promise<string> {
    return 'world'
  }

  @Cacheable({ key: 'bar' }) // cacheKey will be `FooService.hello2:bar`
  async hello2(): Promise<string> {
    return 'world'
  }

  @Cacheable({ key: (input: UserDTO) => input.uid.toString() }) // cacheKey will be `FooService.world:${uid}`
  async world(input: UserDTO): Promise<string> {
    return 'world'
  }

}

CacheEvict Decorator

supports method

CacheEvictArgs Parameters

name type default value
cacheName string | undefined {className}.{methodName}
key string | number | bigint | KeyGenerator | undefined undefined
beforeInvocation boolean | undefined false
condition CacheConditionFn | boolean | undefined undefined (always evict)
result any | undefined always undefined if beforeInvocation true
import { Cacheable, CacheEvict } from '@mwcp/cache'

const cacheName = 'UserService.getOne' 

@Cacheable() 
export class UserService {

  @Cacheable({ cacheName })
  async getOne(): Promise<UserDTO> {
    return { uid: 1 }
  }

  @CacheEvict({ cacheName }) // same cacheName with getOne()
  async updateOne(): Promise<UserDTO> {
    return { uid: 2 }
  }
}

CachePut Decorator

supports method

import { CachePut } from '@mwcp/cache'

const cacheName = 'FooRepo.getOne'

@Controller('/')
export class FooRepo {

  @Cacheable({ cacheName, ttl: 5 })
  async getOne(): Promise<string> {
    return 'world'
  }

  @CachePut({ cacheName })
  async update(): Promise<string> {
    return 'hello'
  }

}

Decorator Generics

Auto parameter type of keyGenerator from generics

import { Cacheable } from '@mwcp/cache'

@Cacheable() 
export class FooService {

  @Cacheable<FooService['world']>({  // pass generics and then input will get the type automatically
    key: ([input]) => input.uid.toString()
  }) // cacheKey will be `FooService.world:${uid}`
  async world(input: UserDTO): Promise<string> {
    return 'world'
  }

}
@Cacheable() 
export class FooService {
  @Cacheable<FooService['hello']>({  // <--- pass FooService['hello'] as method type
    key: (args) => args[0].uid.toString()   // <--- args 自动推导为类型 [UserDTO, string | undefined]
  }) 
  async hello(input: UserDTO, input2?: string): Promise<string> {
    return 'world'
  }
}

More examples

License

MIT

Languages


Package Sidebar

Install

npm i @mwcp/cache

Weekly Downloads

157

Version

25.2.3

License

MIT

Unpacked Size

134 kB

Total Files

111

Last publish

Collaborators

  • waiting