Socket
Socket
Sign inDemoInstall

@ttou/midway-cache

Package Overview
Dependencies
0
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @ttou/midway-cache

封装 cache-manager


Version published
Maintainers
1
Created

Readme

Source

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

Keywords

FAQs

Last updated on 28 May 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc