Skip to content

Commit

Permalink
feat: fix caching, and adjust indexes mongo
Browse files Browse the repository at this point in the history
  • Loading branch information
wendryosales committed Aug 23, 2023
1 parent b5ced23 commit 0d370a8
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 16 deletions.
8 changes: 4 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ services:
resources:
limits:
cpus: '0.37'
memory: '0.7GB'
memory: '0.4GB'

api02:
build: ./
Expand All @@ -27,7 +27,7 @@ services:
resources:
limits:
cpus: '0.37'
memory: '0.7GB'
memory: '0.4GB'

nginx:
image: nginx:latest
Expand All @@ -48,7 +48,7 @@ services:
image: mongo:latest
hostname: db
environment:
- MONGO_INITDB_DATABASE=elysia
- MONGO_INITDB_DATABASE=nestjs
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=root
command: mongod --quiet --logpath /dev/null
Expand All @@ -70,7 +70,7 @@ services:
resources:
limits:
cpus: '0.05'
memory: '0.15GB'
memory: '0.75GB'

networks:
default:
Expand Down
99 changes: 99 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
"@nestjs/platform-express": "^10.0.0",
"bull": "^4.11.3",
"cache-manager": "^5.2.3",
"cache-manager-redis-yet": "^4.1.2",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"mongoose": "^7.4.4",
"redis": "^4.6.7",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1"
},
Expand Down
9 changes: 8 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Module } from '@nestjs/common';
import { PessoaModule } from './pessoa/pessoa.module';
import { BullModule } from '@nestjs/bull';
import { CacheModule } from '@nestjs/cache-manager';
import { redisStore } from 'cache-manager-redis-yet';
import { RedisClientOptions } from 'redis';

@Module({
imports: [
Expand All @@ -12,7 +14,12 @@ import { CacheModule } from '@nestjs/cache-manager';
port: 6379,
},
}),
CacheModule.register({
CacheModule.register<RedisClientOptions>({
store: redisStore,
socket: {
host: 'redis',
port: 6379,
},
isGlobal: true,
}),
],
Expand Down
7 changes: 7 additions & 0 deletions src/pessoa/pessoa.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ PessoaSchema.set('toJSON', {
delete ret._id;
},
});

PessoaSchema.index({ nome: 'text', apelido: 'text', stack: 'text' });
PessoaSchema.index({
nome: 'ascending',
apelido: 'ascending',
stack: 'ascending',
});
30 changes: 19 additions & 11 deletions src/pessoa/pessoa.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,43 @@ export class PessoaService {
throw new UnprocessableEntityException('Apelido já existe');
}

await this.cacheManager.set(body.apelido, '1');
await this.cacheManager.set(body.apelido, '1', 0);
await this.cacheManager.set(body.id, JSON.stringify(body), 0);
await this.pessoaQueue.add('createPerson', body);
return;
}

getPersons(t: string) {
return this.pessoaModel
async getPersons(t: string) {
const cached = await this.cacheManager.get(t);
if (cached) {
await this.cacheManager.del(t);
return JSON.parse(cached as string);
}
const persons = await this.pessoaModel
.find({
$or: [
{ nome: { $regex: t, $options: 'i' } },
{ apelido: { $regex: t, $options: 'i' } },
{ stack: { $regex: t, $options: 'i' } },
{ stack: t },
],
})
.limit(50)
.exec();
await this.cacheManager.set(t, JSON.stringify(persons), 0);
return persons;
}

async getPerson(id: string) {
// const cached = await this.cacheManager.get<string>(id);
// if (!cached) {
// throw new NotFoundException();
// }
// return JSON.parse(cached);
//
const person = await this.pessoaModel.findById(id).exec();
const cached = await this.cacheManager.get(id);
if (cached) {
await this.cacheManager.del(id);
return JSON.parse(cached as string);
}
const person = await this.pessoaModel.findOne({ _id: id }).exec();
if (!person) {
throw new NotFoundException();
}
await this.cacheManager.set(id, JSON.stringify(person));
return person;
}

Expand Down

0 comments on commit 0d370a8

Please sign in to comment.