-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(api): Add user cache for optimization (#386)
- Loading branch information
Showing
10 changed files
with
150 additions
and
90 deletions.
There are no files selected for viewing
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
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
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 { Global, Module } from '@nestjs/common' | ||
import { CacheService } from './cache.service' | ||
|
||
@Global() | ||
@Module({ | ||
exports: [CacheService], | ||
providers: [CacheService] | ||
}) | ||
export class CacheModule {} |
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,34 @@ | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { CacheService } from './cache.service' | ||
import { REDIS_CLIENT } from '../provider/redis.provider' | ||
|
||
describe('CacheService', () => { | ||
let service: CacheService | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
CacheService, | ||
{ | ||
provide: REDIS_CLIENT, | ||
useValue: { | ||
publisher: { | ||
// Add minimal mock methods as needed | ||
setEx: jest.fn(), | ||
set: jest.fn(), | ||
get: jest.fn(), | ||
del: jest.fn(), | ||
keys: jest.fn() | ||
} | ||
} | ||
} | ||
] | ||
}).compile() | ||
|
||
service = module.get<CacheService>(CacheService) | ||
}) | ||
|
||
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,54 @@ | ||
import { Inject, Injectable, OnModuleDestroy } from '@nestjs/common' | ||
import { RedisClientType } from 'redis' | ||
import { User } from '@prisma/client' | ||
import { REDIS_CLIENT } from '../provider/redis.provider' | ||
|
||
@Injectable() | ||
export class CacheService implements OnModuleDestroy { | ||
private static readonly USER_PREFIX = 'user-' | ||
|
||
constructor( | ||
@Inject(REDIS_CLIENT) private redisClient: { publisher: RedisClientType } | ||
) {} | ||
|
||
private getUserKey(userId: string): string { | ||
return `${CacheService.USER_PREFIX}${userId}` | ||
} | ||
|
||
async setUser(user: User, expirationInSeconds?: number): Promise<void> { | ||
const key = this.getUserKey(user.id) | ||
const userJson = JSON.stringify(user) | ||
if (expirationInSeconds) { | ||
await this.redisClient.publisher.setEx(key, expirationInSeconds, userJson) | ||
} else { | ||
await this.redisClient.publisher.set(key, userJson) | ||
} | ||
} | ||
|
||
async getUser(userId: string): Promise<User | null> { | ||
const key = this.getUserKey(userId) | ||
const userData = await this.redisClient.publisher.get(key) | ||
if (userData) { | ||
return JSON.parse(userData) as User | ||
} | ||
return null | ||
} | ||
|
||
async deleteUser(userId: string): Promise<number> { | ||
const key = this.getUserKey(userId) | ||
return await this.redisClient.publisher.del(key) | ||
} | ||
|
||
async clearAllUserCache(): Promise<void> { | ||
const keys = await this.redisClient.publisher.keys( | ||
`${CacheService.USER_PREFIX}*` | ||
) | ||
if (keys.length > 0) { | ||
await this.redisClient.publisher.del(keys) | ||
} | ||
} | ||
|
||
async onModuleDestroy() { | ||
await this.redisClient.publisher.quit() | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.