mirror of
https://gitlab.com/mobicoop/v3/service/user.git
synced 2026-01-11 22:02:39 +00:00
add redis cache
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
import { Mapper } from '@automapper/core';
|
||||
import { InjectMapper } from '@automapper/nestjs';
|
||||
import { Controller, UsePipes } from '@nestjs/common';
|
||||
import {
|
||||
CacheInterceptor,
|
||||
CacheKey,
|
||||
Controller,
|
||||
UseInterceptors,
|
||||
UsePipes,
|
||||
} from '@nestjs/common';
|
||||
import { CommandBus, QueryBus } from '@nestjs/cqrs';
|
||||
import { GrpcMethod, RpcException } from '@nestjs/microservices';
|
||||
import { DatabaseException } from '../../../database/src/exceptions/database.exception';
|
||||
@@ -33,6 +39,8 @@ export class UsersController {
|
||||
) {}
|
||||
|
||||
@GrpcMethod('UsersService', 'FindAll')
|
||||
@UseInterceptors(CacheInterceptor)
|
||||
@CacheKey('UsersServiceFindAll')
|
||||
async findAll(data: FindAllUsersRequest): Promise<ICollection<User>> {
|
||||
const userCollection = await this._queryBus.execute(
|
||||
new FindAllUsersQuery(data),
|
||||
@@ -46,6 +54,8 @@ export class UsersController {
|
||||
}
|
||||
|
||||
@GrpcMethod('UsersService', 'FindOneByUuid')
|
||||
@UseInterceptors(CacheInterceptor)
|
||||
@CacheKey('UsersServiceFindOneByUuid')
|
||||
async findOneByUuid(data: FindUserByUuidRequest): Promise<UserPresenter> {
|
||||
try {
|
||||
const user = await this._queryBus.execute(new FindUserByUuidQuery(data));
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { RabbitMQModule } from '@golevelup/nestjs-rabbitmq';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { RedisClientOptions } from '@liaoliaots/nestjs-redis';
|
||||
import { CacheModule, Module } from '@nestjs/common';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { CqrsModule } from '@nestjs/cqrs';
|
||||
import { redisStore } from 'cache-manager-ioredis-yet';
|
||||
import { DatabaseModule } from '../database/database.module';
|
||||
import { UsersController } from './adapters/primaries/users.controller';
|
||||
import { LoggingMessager } from './adapters/secondaries/logging.messager';
|
||||
@@ -36,6 +38,17 @@ import { UserProfile } from './mappers/user.profile';
|
||||
}),
|
||||
inject: [ConfigService],
|
||||
}),
|
||||
CacheModule.registerAsync<RedisClientOptions>({
|
||||
imports: [ConfigModule],
|
||||
useFactory: async (configService: ConfigService) => ({
|
||||
store: await redisStore({
|
||||
host: configService.get<string>('REDIS_HOST'),
|
||||
port: configService.get<number>('REDIS_PORT'),
|
||||
ttl: configService.get('CACHE_TTL'),
|
||||
}),
|
||||
}),
|
||||
inject: [ConfigService],
|
||||
}),
|
||||
],
|
||||
controllers: [UsersController],
|
||||
providers: [
|
||||
|
||||
Reference in New Issue
Block a user