refactor source code

This commit is contained in:
2024-10-29 17:42:54 +07:00
parent 3b23d9e0b7
commit 152bb50da8
83 changed files with 8473 additions and 7577 deletions

View File

@@ -1,6 +1,6 @@
import { Global, Module } from '@nestjs/common';
import { Global, Module } from '@nestjs/common'
import { RedisService } from './redis.service';
import { RedisService } from './redis.service'
@Global()
@Module({

View File

@@ -1,41 +1,41 @@
import { Injectable } from '@nestjs/common';
import { Redis } from 'ioredis';
import { User } from '@prisma/client';
import { Injectable } from '@nestjs/common'
import { Redis } from 'ioredis'
import { User } from '@prisma/client'
@Injectable()
export class RedisService {
private readonly redis: Redis;
private readonly redis: Redis
constructor() {
this.redis = new Redis(process.env.REDIS_URL as string);
this.redis = new Redis(process.env.REDIS_URL as string)
}
async get(key: string) {
return await this.redis.get(key);
return await this.redis.get(key)
}
async set(key: string, value: string, expireAt: number) {
return await this.redis.set(key, value, 'EXAT', expireAt);
return await this.redis.set(key, value, 'EXAT', expireAt)
}
async del(key: string) {
return await this.redis.del(key);
return await this.redis.del(key)
}
async close() {
return await this.redis.quit();
return await this.redis.quit()
}
async getUser(sessionId: string) {
const userData = await this.get(sessionId);
const userData = await this.get(sessionId)
if (!userData) {
return null;
return null
}
const retrievedUser: User = JSON.parse(userData);
return retrievedUser;
const retrievedUser: User = JSON.parse(userData)
return retrievedUser
}
async setUser(sessionId: string, user: User, expireAt: number) {
return await this.set(sessionId, JSON.stringify(user), expireAt);
return await this.set(sessionId, JSON.stringify(user), expireAt)
}
}