# 使用 Redis

组件包默认绑定了 Redis 实例在容器中(使用了 ioredis (opens new window)

Redis 连接所需的配置,可以在配置文件 src/config/redis.ts 中声明:

import { RedisConfigInterface } from '@tiger/base-provider'

export default {
  // ...
} as RedisConfigInterface

配置文件可以使用 RedisConfigInterface 来约束和提示

组件包也支持配置声明到 src/config/app.tsredis 属性中

示例配置

单机:

import { RedisConfigInterface } from '@tiger/base-provider'

export default {
  host: '127.0.0.1',
  port:  6379
} as RedisConfigInterface

分布式:

import { RedisConfigInterface } from '@tiger/base-provider'

export default {
  cluster: true, // 表示连接的分布式集群
  nodes: [{
    host: '127.0.0.1',
    port:  6379
  }, {
    host: '127.0.0.1',
    port:  6378
  }, {
    host: '127.0.0.1',
    port:  6377
  }]
} as RedisConfigInterface

在业务中使用

import { Controller, Get, Autowired } from '@tiger/common'
import { Redis } from '@tiger/base-provider'

@Controller()
export class ExampleController {

  @Autowired
  redis: Redis;

  @Get()
  index() {
    return this.redis.get('key1')
  }
}