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

@mwcp/cache

v26.5.1

Published

midway component for declarative cache

Downloads

1,662

Readme

@mwcp/cache

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

安装依赖

npm i @mwcp/cache

更新配置

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 {
}

更改默认设置,通过文件 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!
      },
    }
  }
}

使用

Normal Cache-Docs

判断返回对象值是否来自于缓存

assert(retrieveCacheMetaFrom(data))

缓存入口生成规则

  • 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

Cacheable 装饰器

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 装饰器

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 装饰器

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'
  }

}

装饰器泛型参数

从泛型参数自动获取方法调用参数类型

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