从零开始搭建nestjs项目-配置redis服务

服务器需要先安装redis的相关服务,推荐使用Another Redis Desktop Manager可视化客户端去测试一下redis配置是否正常。

相关包

pnpm i ioredis

封装redis服务

使用nest g s redis创建redis.service.ts

import { Injectable } from '@nestjs/common';
import { Redis } from 'ioredis';

@Injectable()
export class RedisService {
    private readonly redisClient: Redis;
    private defaultTime = 60 * 10;//默认有效时间,单位秒

    constructor() {
        this.redisClient = new Redis({
            host: 'host',
            port: 6379,
        });
        
        //使用db0
        this.redisClient.select(0)
    }


    setValue(key: string, value: string, seconds?: number) {
        return this.redisClient.set(key, value, 'EX', seconds || this.defaultTime);
    }

    getValue(key: string) {
        return this.redisClient.get(key);
    }

    deleteValue(key: string) {
        return this.redisClient.del(key);
    }

    deleteAllValue() {
        return this.redisClient.flushall();
    }

    // 根据正则表达式获取keys
    getKeysByPattern(pattern:string){
        return this.redisClient.keys(pattern)
    }
}

结束

版本
  1. nodejs:20.9.0
  2. npm:10.1.0
  3. nestjs:10.0.0
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
NestJS Cache Manager is a module for caching data in NestJS applications. It provides a simple interface for caching data using various caching strategies such as memory, Redis, and others. Redis is one of the caching strategies that NestJS Cache Manager supports. Redis is an in-memory data structure store that can be used as a database, cache, and message broker. It is often used as a cache because of its high performance and scalability. Redis stores data in memory, which makes it faster than traditional disk-based databases. Using Redis as a caching strategy in NestJS Cache Manager is easy. First, you need to install the Redis module: ``` npm install cache-manager-redis-store ``` Next, you need to configure the Redis cache in your NestJS application: ``` import { CacheModule, Module } from '@nestjs/common'; import * as redisStore from 'cache-manager-redis-store'; @Module({ imports: [ CacheModule.register({ store: redisStore, host: 'localhost', port: 6379, }), ], }) export class AppModule {} ``` In this example, we import the CacheModule and configure it to use the Redis store. We set the host and port to connect to the Redis instance. Now, you can use the cache in your NestJS application: ``` import { CacheInterceptor, CacheTTL, Controller, Get, UseInterceptors } from '@nestjs/common'; @Controller('cats') export class CatsController { @Get() @UseInterceptors(CacheInterceptor) @CacheTTL(60) findAll(): string[] { return ['Cat 1', 'Cat 2', 'Cat 3']; } } ``` In this example, we use the CacheInterceptor to cache the response of the findAll() method for 60 seconds. This means that subsequent requests for the same resource will be served from the cache, which improves performance and reduces the load on the server. Overall, using Redis as a caching strategy in NestJS Cache Manager is a great way to improve the performance and scalability of your NestJS application.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值