Skip to content

Commit

Permalink
feat: Redis sorted set 자료구조를 활용하여 랭킹 갱신 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
coldrain-f committed Sep 21, 2022
1 parent 11806cb commit 2455d70
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"dependencies": {
"@anchan828/nest-redlock": "^0.1.3",
"@liaoliaots/nestjs-redis": "^9.0.2",
"@nestjs/axios": "^0.1.0",
"@nestjs/common": "^9.0.0",
"@nestjs/config": "^2.2.0",
Expand Down
18 changes: 18 additions & 0 deletions src/boss-raid-history/boss-raid-history.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { InjectRepository } from '@nestjs/typeorm';
import { Cache } from 'cache-manager';
import { BossRaidService } from 'src/boss-raid/boss-raid.service';
import { RankService } from 'src/rank/rank.service';
import { User } from 'src/users/entities/user.entity';
import { UsersService } from 'src/users/users.service';
import { Repository } from 'typeorm';
import { BossRaidHistory } from './entities/boss-raid-history.entity';
Expand Down Expand Up @@ -135,6 +136,8 @@ export class BossRaidHistoryService {
);

// Todo: Redis에서 랭킹 갱신
// Todo: 해당 사용자의 totalScore를 계산해서 redis에 집어넣는다.
await this.fetchRanking(userId);
}

async findOne(raidRecordId: number): Promise<BossRaidHistory> {
Expand All @@ -148,6 +151,21 @@ export class BossRaidHistoryService {
return bossRaidHistory;
}

/**
* 특정 사용자의 랭킹을 갱신한다.
*/
private async fetchRanking(userId: number) {
const user: User = await this.usersService.findOne(userId);
let totalScore = 0;

// 보스 레이드가 종료된 히스토리만 totalScore에 누적한다.
user.bossRaidHistories
.filter((history) => history.endTime)
.forEach((history) => (totalScore += history.score));

await this.rankService.fetchRanking(totalScore, userId);
}

/**
* 입장 가능여부 Redis 데이터 초기화
* Redis에 canEnter 값이 설정되어 있지 않다면 값 설정 및 초기화
Expand Down
15 changes: 15 additions & 0 deletions src/rank/rank.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
import { RedisModule } from '@liaoliaots/nestjs-redis';
import { Module } from '@nestjs/common';
import { BossRaidHistoryModule } from 'src/boss-raid-history/boss-raid-history.module';
import { UsersModule } from 'src/users/users.module';
import { RankService } from './rank.service';

// Redis sorted set을 위해서
// npm install @liaoliaots/nestjs-redis ioredis 설치
@Module({
imports: [
RedisModule.forRoot({
config: {
host: '127.0.0.1',
port: 6379,
},
}),
BossRaidHistoryModule,
UsersModule,
],
providers: [RankService],
exports: [RankService],
})
Expand Down
37 changes: 36 additions & 1 deletion src/rank/rank.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,39 @@
import { InjectRedis } from '@liaoliaots/nestjs-redis';
import { Injectable } from '@nestjs/common';
import Redis from 'ioredis';
import { BossRaidHistoryService } from 'src/boss-raid-history/boss-raid-history.service';
import { BossRaidHistory } from 'src/boss-raid-history/entities/boss-raid-history.entity';
import { UsersService } from 'src/users/users.service';

export interface RankingInfo {
ranking: number;
userId: number;
totalScore: number;
}

@Injectable()
export class RankService {}
export class RankService {
constructor(
@InjectRedis() private readonly redis: Redis,
private readonly bossRaidHistoryService: BossRaidHistoryService,
private readonly usersService: UsersService,
) {}

/**
* 랭킹 패치
* 순서대로 Sorted Set의 이름, 점수(score), 키(key)
*/
async fetchRanking(totalScore: number, userId: number) {
await this.redis.zadd('rank', totalScore, userId);
}

/**
* 모든 랭킹 조회
*/
getAllRanking() {}

/**
* 나의 랭킹 조회
*/
getMyRanking(userId: number) {}
}
9 changes: 9 additions & 0 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,13 @@ export class UsersService {

return userEntity;
}

/**
* 모든 유저 조회
*/
async findAll(): Promise<User[]> {
return await this.usersRepository.find({
relations: ['bossRaidHistories'],
});
}
}

0 comments on commit 2455d70

Please sign in to comment.