[Nestjs] 使用redis的基础配置,以及基本使用的介绍

本文涉及的产品
云数据库 Redis 版,标准版 2GB
推荐场景:
搭建游戏排行榜
云原生内存数据库 Tair,内存型 2GB
简介: 要在 NestJS 中使用 Redis,需要安装依赖npm install --save redis fastify-redis

要在 NestJS 中使用 Redis,需要安装依赖npm install --save redis fastify-redis


app.module.ts 中导入 RedisModuleCacheModule

import { Module } from '@nestjs/common';
import { RedisModule } from 'nestjs-redis';
import { CacheModule } from '@nestjs/common';
@Module({
  imports: [
    RedisModule.register({
      host: 'localhost', // Redis 服务器地址
      port: 6379, // Redis 服务器端口号
      password: 'your_password', // Redis 密码(如果需要)
    }),
    CacheModule.register(),
  ],
})
export class AppModule {}

创建一个使用 Redis 的服务示例:

import { Injectable, Inject } from '@nestjs/common';
import { Redis } from 'ioredis';
import { CACHE_MANAGER } from '@nestjs/common';
@Injectable()
export class RedisService {
  constructor(
    @Inject(CACHE_MANAGER) private cacheManager: CacheManager,
    @Inject('REDIS') private readonly redis: Redis,
  ) {}
  async setValue(key: string, value: any) {
    await this.cacheManager.set(key, value);
  }
  async getValue(key: string): Promise<any> {
    return await this.cacheManager.get(key);
  }
  async removeFromCache(key: string) {
    await this.cacheManager.del(key);
  }
}

使用 Redis 服务:

import { Controller, Get, Param } from '@nestjs/common';
import { RedisService } from './redis.service';
@Controller('example')
export class ExampleController {
  constructor(private readonly redisService: RedisService) {}
  @Get('set/:key/:value')
  async setKeyValue(
    @Param('key') key: string,
    @Param('value') value: string,
  ): Promise<void> {
    await this.redisService.setValue(key, value);
  }
  @Get('get/:key')
  async getValueByKey(@Param('key') key: string): Promise<any> {
    return await this.redisService.getValue(key);
  }
  @Get('remove/:key')
  async removeFromCache(@Param('key') key: string): Promise<void> {
    await this.redisService.removeFromCache(key);
  }
}

以上步骤中,我们使用 nestjs-redis 模块提供的 RedisModule 来连接和配置 Redis。在 RedisService 中,我们使用 CACHE_MANAGER 标记注入缓存管理器,并通过 REDIS 标记注入 Redis 实例。


在 ExampleController 中,我们使用 RedisService 来展示如何设置、获取和删除缓存数据。需要了解更多 Redis 操作,请参考 Redis 官方文档:https://redis.io/documentation

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
26天前
|
NoSQL Redis 容器
【Azure Cache for Redis】Redis的导出页面无法配置Storage SAS时通过az cli来完成
【Azure Cache for Redis】Redis的导出页面无法配置Storage SAS时通过az cli来完成
|
26天前
|
缓存 NoSQL 测试技术
【Azure Redis 缓存】Azure Redis 功能性讨论三: 调优参数配置
【Azure Redis 缓存】Azure Redis 功能性讨论三: 调优参数配置
|
27天前
|
存储 缓存 NoSQL
【Azure Redis 缓存】由Azure Redis是否可以自定义密码而引申出Azure PaaS的Redis服务是否可以和自建的Redis进行主从配置呢?
【Azure Redis 缓存】由Azure Redis是否可以自定义密码而引申出Azure PaaS的Redis服务是否可以和自建的Redis进行主从配置呢?
|
27天前
|
缓存 NoSQL 网络协议
【Azure Redis 缓存】如何使得Azure Redis可以仅从内网访问? Config 及 Timeout参数配置
【Azure Redis 缓存】如何使得Azure Redis可以仅从内网访问? Config 及 Timeout参数配置
|
2月前
|
NoSQL Redis 数据安全/隐私保护
Redis主从配置
Redis主从配置
47 5
|
2月前
|
NoSQL Redis
Redis 主从复制架构配置及原理
Redis 主从复制架构配置及原理
45 5
|
2月前
|
NoSQL Redis 数据库
redis 持久化机制及配置
redis 持久化机制及配置
63 4
|
29天前
|
缓存 NoSQL 网络协议
【Azure Redis 缓存 Azure Cache For Redis】在创建高级层Redis(P1)集成虚拟网络(VNET)后,如何测试VNET中资源如何成功访问及配置白名单的效果
【Azure Redis 缓存 Azure Cache For Redis】在创建高级层Redis(P1)集成虚拟网络(VNET)后,如何测试VNET中资源如何成功访问及配置白名单的效果
|
2月前
|
NoSQL Redis
Redis性能优化问题之根据 Redis 的 AOF 配置级别优化性能,如何解决
Redis性能优化问题之根据 Redis 的 AOF 配置级别优化性能,如何解决
|
2月前
|
NoSQL Redis
Redis性能优化问题之为什么配置为 appendfsync everysec 的 AOF 也可能导致 Redis 延迟变大
Redis性能优化问题之为什么配置为 appendfsync everysec 的 AOF 也可能导致 Redis 延迟变大