1.1.1 • Published 2 years ago

@ttou/midway-cache v1.1.1

Weekly downloads
-
License
MulanPSL2
Repository
-
Last release
2 years ago

Midway Cache

基于 cache-manager 封装的组件。

相关信息:

描述
可用于标准项目
可用于 Serverless
可用于一体化

安装依赖

npm i @ttou/midway-cache cache-manager

或者在 package.json 中增加如下依赖后,重新安装。

{
  "dependencies": {
    "@ttou/midway-cache": "^1.0.0",
    "cache-manager": "^5.0.0"
    // ...
  }
}

开启组件

在 configuration.ts 中增加组件。

import { Configuration } from '@midwayjs/core';
import * as cache from '@ttou/midway-cache';

@Configuration({
  imports: [
    // ...
    cache
  ]
})
export class MainConfiguration {}

基础配置

// src/config/config.default.ts
export default {
  // ...
  cacheNext: {
    store: 'memory',
    opts: {
      max: 100,
      ttl: 10 * 1000,
    }
  },
};

使用示例

import { Inject, Provide } from '@midwayjs/core';
import { CacheManager } from '@ttou/midway-cache';

@Provide()
export class UserService {
  @Inject()
  cacheManager: CacheManager;

  /**
   * 设置缓存
   */
  async setUser(user: any) {
    await this.cacheManager.set('user', user);
  }

  /**
   * 获取缓存
   */
  async getUser() {
    const user = await this.cacheManager.get<any>('user');

    return user;
  }

  /**
   * 删除缓存
   */
  async delUser() {
    await this.cacheManager.del('user');
  }

  /**
   * 清空缓存
   */
  async reset() {
    await this.cacheManager.reset();
  }
}

其他Cache

用户也可以修改 store 方式,在 config.default.ts 中进行组件的配置:

import { redisStore } from 'cache-manager-ioredis-yet'

export default {
  // ...
  cacheNext: {
    store: redisStore,
    opts: {
      host: 'localhost',
      port: 6379
    },
  }
}
1.1.1

2 years ago

1.1.0

2 years ago

1.0.2

2 years ago

1.0.3

2 years ago

1.0.1

3 years ago

1.0.0

3 years ago