-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #278 from feyishola/redis-fix
implemented redisdb closes #289
- Loading branch information
Showing
11 changed files
with
536 additions
and
21 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'; | ||
|
||
@Entity('leaderboard_entries') | ||
export class LeaderboardEntry { | ||
@PrimaryGeneratedColumn('uuid') | ||
id: string; | ||
|
||
@Column({ unique: true }) | ||
playerId: string; | ||
|
||
@Column() | ||
playerName: string; | ||
|
||
@Column({ type: 'int', default: 0 }) | ||
score: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,58 @@ | ||
// import { Injectable } from '@nestjs/common'; | ||
|
||
// // Service responsible for leaderboard operations. | ||
// @Injectable() | ||
// export class LeaderboardService { | ||
// // Retrieve the global leaderboard. | ||
// getLeaderboard() { | ||
// // Implement get leaderboard logic | ||
// } | ||
|
||
// // Retrieve the rank of a specific player. | ||
// getPlayerRank() { | ||
// // Implement get player rank logic | ||
// } | ||
// } | ||
|
||
|
||
import { Injectable } from '@nestjs/common'; | ||
import { RedisService } from 'src/redis/redis.service'; | ||
import { Repository } from 'typeorm'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { LeaderboardEntry } from '../leaderboard-entry.entity'; | ||
|
||
// Service responsible for leaderboard operations. | ||
@Injectable() | ||
export class LeaderboardService { | ||
// Retrieve the global leaderboard. | ||
getLeaderboard() { | ||
// Implement get leaderboard logic | ||
constructor( | ||
@InjectRepository(LeaderboardEntry) | ||
private leaderboardRepository: Repository<LeaderboardEntry>, | ||
private redisService: RedisService | ||
) {} | ||
|
||
// Helper method for caching | ||
private async getCachedData<T>(key: string, fetchFunction: () => Promise<T>, ttl = 3600): Promise<T> { | ||
const cachedData = await this.redisService.get(key); | ||
if (cachedData) return JSON.parse(cachedData); | ||
|
||
const freshData = await fetchFunction(); | ||
await this.redisService.set(key, JSON.stringify(freshData), ttl); | ||
|
||
return freshData; | ||
} | ||
|
||
// Retrieve the global leaderboard | ||
async getLeaderboard() { | ||
return this.getCachedData('leaderboard:global', () => | ||
this.leaderboardRepository.find({ order: { score: 'DESC' }, take: 100 }) | ||
); | ||
} | ||
|
||
// Retrieve the rank of a specific player. | ||
getPlayerRank() { | ||
// Implement get player rank logic | ||
// Retrieve the rank of a specific player | ||
async getPlayerRank(playerId: string) { | ||
return this.getCachedData(`leaderboard:rank:${playerId}`, async () => { | ||
const leaderboard = await this.getLeaderboard(); // Fetch cached leaderboard | ||
const rank = leaderboard.findIndex(entry => entry.playerId === playerId) + 1; | ||
return rank > 0 ? { playerId, rank } : null; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Module, Global } from '@nestjs/common'; | ||
import { RedisService } from './redis.service'; | ||
|
||
@Global() | ||
@Module({ | ||
providers: [RedisService], | ||
exports: [RedisService], | ||
}) | ||
export class RedisModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { RedisService } from './redis.service'; | ||
|
||
describe('RedisService', () => { | ||
let service: RedisService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [RedisService], | ||
}).compile(); | ||
|
||
service = module.get<RedisService>(RedisService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { Redis } from 'ioredis'; | ||
|
||
@Injectable() | ||
export class RedisService { | ||
private client: Redis; | ||
|
||
constructor() { | ||
this.client = new Redis({ | ||
host: '127.0.0.1', | ||
port: 6379, | ||
}); | ||
} | ||
|
||
async get(key: string): Promise<string | null> { | ||
return this.client.get(key); | ||
} | ||
|
||
async set(key: string, value: string, ttl?: number): Promise<void> { | ||
if (ttl) { | ||
await this.client.set(key, value, 'EX', ttl); | ||
} else { | ||
await this.client.set(key, value); | ||
} | ||
} | ||
|
||
async del(key: string): Promise<void> { | ||
await this.client.del(key); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters