add redis cache
This commit is contained in:
parent
c11a316057
commit
d8e6ed7222
|
@ -13,6 +13,9 @@ RMQ_URI=amqp://v3-broker:5672
|
||||||
POSTGRES_IMAGE=postgres:15.0
|
POSTGRES_IMAGE=postgres:15.0
|
||||||
|
|
||||||
# REDIS
|
# REDIS
|
||||||
REDIS_IMAGE=redis/redis-stack:latest
|
REDIS_IMAGE=redis:7.0-alpine
|
||||||
REDIS_HOST=v3-user-redis
|
REDIS_HOST=v3-user-redis
|
||||||
REDIS_PORT=6379
|
REDIS_PORT=6379
|
||||||
|
|
||||||
|
# CACHE
|
||||||
|
CACHE_TTL=5000
|
||||||
|
|
|
@ -6,13 +6,5 @@ SERVICE_CONFIGURATION_DOMAIN=USER
|
||||||
# PRISMA
|
# PRISMA
|
||||||
DATABASE_URL="postgresql://user:user@localhost:5601/user?schema=public"
|
DATABASE_URL="postgresql://user:user@localhost:5601/user?schema=public"
|
||||||
|
|
||||||
# RABBIT MQ
|
|
||||||
RMQ_URI=amqp://v3-broker:5672
|
|
||||||
|
|
||||||
# POSTGRES
|
# POSTGRES
|
||||||
POSTGRES_IMAGE=postgres:15.0
|
POSTGRES_IMAGE=postgres:15.0
|
||||||
|
|
||||||
# REDIS
|
|
||||||
REDIS_IMAGE=redis/redis-stack:latest
|
|
||||||
REDIS_HOST=v3-user-redis
|
|
||||||
REDIS_PORT=6379
|
|
||||||
|
|
|
@ -25,8 +25,7 @@ services:
|
||||||
container_name: v3-user-redis
|
container_name: v3-user-redis
|
||||||
image: ${REDIS_IMAGE}
|
image: ${REDIS_IMAGE}
|
||||||
ports:
|
ports:
|
||||||
- 6379:6379
|
- 6501:6379
|
||||||
- 8101:8001
|
|
||||||
env_file:
|
env_file:
|
||||||
- .env
|
- .env
|
||||||
networks:
|
networks:
|
||||||
|
|
|
@ -26,6 +26,8 @@
|
||||||
"@types/supertest": "^2.0.11",
|
"@types/supertest": "^2.0.11",
|
||||||
"@typescript-eslint/eslint-plugin": "^5.0.0",
|
"@typescript-eslint/eslint-plugin": "^5.0.0",
|
||||||
"@typescript-eslint/parser": "^5.0.0",
|
"@typescript-eslint/parser": "^5.0.0",
|
||||||
|
"cache-manager": "^5.1.4",
|
||||||
|
"cache-manager-ioredis-yet": "^1.1.0",
|
||||||
"class-transformer": "^0.5.1",
|
"class-transformer": "^0.5.1",
|
||||||
"dotenv-cli": "^6.0.0",
|
"dotenv-cli": "^6.0.0",
|
||||||
"ioredis": "^5.3.0"
|
"ioredis": "^5.3.0"
|
||||||
|
@ -3530,6 +3532,35 @@
|
||||||
"node": ">= 0.8"
|
"node": ">= 0.8"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/cache-manager": {
|
||||||
|
"version": "5.1.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/cache-manager/-/cache-manager-5.1.4.tgz",
|
||||||
|
"integrity": "sha512-beXzzuboV6I0AkU25udrd8tRxFbU6c5m7+3eOdmznSVNkyKe0+uTK8EtcuhTwqc/wAYrAGl62w3s58rjKnrO6g==",
|
||||||
|
"dependencies": {
|
||||||
|
"lodash.clonedeep": "^4.5.0",
|
||||||
|
"lru-cache": "^7.14.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/cache-manager-ioredis-yet": {
|
||||||
|
"version": "1.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/cache-manager-ioredis-yet/-/cache-manager-ioredis-yet-1.1.0.tgz",
|
||||||
|
"integrity": "sha512-bGBAq8oNzzNkO2dwlYGWBxNXrz4w8FUTpe3nfUydJ6bm1ixKEcSUKYksGokQMaRgqkQjMbIHWFkvb8p+V9ZKqw==",
|
||||||
|
"dependencies": {
|
||||||
|
"cache-manager": "^5.1.0",
|
||||||
|
"ioredis": "^5.2.3"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 16.17.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/cache-manager/node_modules/lru-cache": {
|
||||||
|
"version": "7.14.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-7.14.1.tgz",
|
||||||
|
"integrity": "sha512-ysxwsnTKdAx96aTRdhDOCQfDgbHnt8SK0KY8SEjO0wHinhWOFTESbjVCMPbU1uGXg/ch4lifqx0wfjOawU2+WA==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=12"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/call-bind": {
|
"node_modules/call-bind": {
|
||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.2.tgz",
|
||||||
|
@ -6489,6 +6520,11 @@
|
||||||
"resolved": "https://registry.npmjs.org/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz",
|
"resolved": "https://registry.npmjs.org/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz",
|
||||||
"integrity": "sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA=="
|
"integrity": "sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA=="
|
||||||
},
|
},
|
||||||
|
"node_modules/lodash.clonedeep": {
|
||||||
|
"version": "4.5.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz",
|
||||||
|
"integrity": "sha512-H5ZhCF25riFd9uB5UCkVKo61m3S/xZk1x4wA6yp/L3RFP6Z/eHH1ymQcGLo7J3GMPfm0V/7m1tryHuGVxpqEBQ=="
|
||||||
|
},
|
||||||
"node_modules/lodash.defaults": {
|
"node_modules/lodash.defaults": {
|
||||||
"version": "4.2.0",
|
"version": "4.2.0",
|
||||||
"resolved": "https://registry.npmjs.org/lodash.defaults/-/lodash.defaults-4.2.0.tgz",
|
"resolved": "https://registry.npmjs.org/lodash.defaults/-/lodash.defaults-4.2.0.tgz",
|
||||||
|
@ -11702,6 +11738,31 @@
|
||||||
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz",
|
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz",
|
||||||
"integrity": "sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg=="
|
"integrity": "sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg=="
|
||||||
},
|
},
|
||||||
|
"cache-manager": {
|
||||||
|
"version": "5.1.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/cache-manager/-/cache-manager-5.1.4.tgz",
|
||||||
|
"integrity": "sha512-beXzzuboV6I0AkU25udrd8tRxFbU6c5m7+3eOdmznSVNkyKe0+uTK8EtcuhTwqc/wAYrAGl62w3s58rjKnrO6g==",
|
||||||
|
"requires": {
|
||||||
|
"lodash.clonedeep": "^4.5.0",
|
||||||
|
"lru-cache": "^7.14.0"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"lru-cache": {
|
||||||
|
"version": "7.14.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-7.14.1.tgz",
|
||||||
|
"integrity": "sha512-ysxwsnTKdAx96aTRdhDOCQfDgbHnt8SK0KY8SEjO0wHinhWOFTESbjVCMPbU1uGXg/ch4lifqx0wfjOawU2+WA=="
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"cache-manager-ioredis-yet": {
|
||||||
|
"version": "1.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/cache-manager-ioredis-yet/-/cache-manager-ioredis-yet-1.1.0.tgz",
|
||||||
|
"integrity": "sha512-bGBAq8oNzzNkO2dwlYGWBxNXrz4w8FUTpe3nfUydJ6bm1ixKEcSUKYksGokQMaRgqkQjMbIHWFkvb8p+V9ZKqw==",
|
||||||
|
"requires": {
|
||||||
|
"cache-manager": "^5.1.0",
|
||||||
|
"ioredis": "^5.2.3"
|
||||||
|
}
|
||||||
|
},
|
||||||
"call-bind": {
|
"call-bind": {
|
||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.2.tgz",
|
||||||
|
@ -13905,6 +13966,11 @@
|
||||||
"resolved": "https://registry.npmjs.org/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz",
|
"resolved": "https://registry.npmjs.org/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz",
|
||||||
"integrity": "sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA=="
|
"integrity": "sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA=="
|
||||||
},
|
},
|
||||||
|
"lodash.clonedeep": {
|
||||||
|
"version": "4.5.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz",
|
||||||
|
"integrity": "sha512-H5ZhCF25riFd9uB5UCkVKo61m3S/xZk1x4wA6yp/L3RFP6Z/eHH1ymQcGLo7J3GMPfm0V/7m1tryHuGVxpqEBQ=="
|
||||||
|
},
|
||||||
"lodash.defaults": {
|
"lodash.defaults": {
|
||||||
"version": "4.2.0",
|
"version": "4.2.0",
|
||||||
"resolved": "https://registry.npmjs.org/lodash.defaults/-/lodash.defaults-4.2.0.tgz",
|
"resolved": "https://registry.npmjs.org/lodash.defaults/-/lodash.defaults-4.2.0.tgz",
|
||||||
|
|
|
@ -44,6 +44,8 @@
|
||||||
"@types/supertest": "^2.0.11",
|
"@types/supertest": "^2.0.11",
|
||||||
"@typescript-eslint/eslint-plugin": "^5.0.0",
|
"@typescript-eslint/eslint-plugin": "^5.0.0",
|
||||||
"@typescript-eslint/parser": "^5.0.0",
|
"@typescript-eslint/parser": "^5.0.0",
|
||||||
|
"cache-manager": "^5.1.4",
|
||||||
|
"cache-manager-ioredis-yet": "^1.1.0",
|
||||||
"class-transformer": "^0.5.1",
|
"class-transformer": "^0.5.1",
|
||||||
"dotenv-cli": "^6.0.0",
|
"dotenv-cli": "^6.0.0",
|
||||||
"ioredis": "^5.3.0"
|
"ioredis": "^5.3.0"
|
||||||
|
|
|
@ -1,6 +1,12 @@
|
||||||
import { Mapper } from '@automapper/core';
|
import { Mapper } from '@automapper/core';
|
||||||
import { InjectMapper } from '@automapper/nestjs';
|
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 { CommandBus, QueryBus } from '@nestjs/cqrs';
|
||||||
import { GrpcMethod, RpcException } from '@nestjs/microservices';
|
import { GrpcMethod, RpcException } from '@nestjs/microservices';
|
||||||
import { DatabaseException } from '../../../database/src/exceptions/database.exception';
|
import { DatabaseException } from '../../../database/src/exceptions/database.exception';
|
||||||
|
@ -33,6 +39,8 @@ export class UsersController {
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@GrpcMethod('UsersService', 'FindAll')
|
@GrpcMethod('UsersService', 'FindAll')
|
||||||
|
@UseInterceptors(CacheInterceptor)
|
||||||
|
@CacheKey('UsersServiceFindAll')
|
||||||
async findAll(data: FindAllUsersRequest): Promise<ICollection<User>> {
|
async findAll(data: FindAllUsersRequest): Promise<ICollection<User>> {
|
||||||
const userCollection = await this._queryBus.execute(
|
const userCollection = await this._queryBus.execute(
|
||||||
new FindAllUsersQuery(data),
|
new FindAllUsersQuery(data),
|
||||||
|
@ -46,6 +54,8 @@ export class UsersController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@GrpcMethod('UsersService', 'FindOneByUuid')
|
@GrpcMethod('UsersService', 'FindOneByUuid')
|
||||||
|
@UseInterceptors(CacheInterceptor)
|
||||||
|
@CacheKey('UsersServiceFindOneByUuid')
|
||||||
async findOneByUuid(data: FindUserByUuidRequest): Promise<UserPresenter> {
|
async findOneByUuid(data: FindUserByUuidRequest): Promise<UserPresenter> {
|
||||||
try {
|
try {
|
||||||
const user = await this._queryBus.execute(new FindUserByUuidQuery(data));
|
const user = await this._queryBus.execute(new FindUserByUuidQuery(data));
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
import { RabbitMQModule } from '@golevelup/nestjs-rabbitmq';
|
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 { ConfigModule, ConfigService } from '@nestjs/config';
|
||||||
import { CqrsModule } from '@nestjs/cqrs';
|
import { CqrsModule } from '@nestjs/cqrs';
|
||||||
|
import { redisStore } from 'cache-manager-ioredis-yet';
|
||||||
import { DatabaseModule } from '../database/database.module';
|
import { DatabaseModule } from '../database/database.module';
|
||||||
import { UsersController } from './adapters/primaries/users.controller';
|
import { UsersController } from './adapters/primaries/users.controller';
|
||||||
import { LoggingMessager } from './adapters/secondaries/logging.messager';
|
import { LoggingMessager } from './adapters/secondaries/logging.messager';
|
||||||
|
@ -36,6 +38,17 @@ import { UserProfile } from './mappers/user.profile';
|
||||||
}),
|
}),
|
||||||
inject: [ConfigService],
|
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],
|
controllers: [UsersController],
|
||||||
providers: [
|
providers: [
|
||||||
|
|
Loading…
Reference in New Issue