Homepage Redesign
This commit is contained in:
119
backend/src/features/platform/domain/platform-cache.service.ts
Normal file
119
backend/src/features/platform/domain/platform-cache.service.ts
Normal file
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* @ai-summary Platform-specific Redis caching service
|
||||
* @ai-context Caching layer for vehicle data and VIN decoding
|
||||
*/
|
||||
import { CacheService } from '../../../core/config/redis';
|
||||
import { logger } from '../../../core/logging/logger';
|
||||
|
||||
export class PlatformCacheService {
|
||||
private cacheService: CacheService;
|
||||
private readonly prefix = 'platform:';
|
||||
|
||||
constructor(cacheService: CacheService) {
|
||||
this.cacheService = cacheService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cached years
|
||||
*/
|
||||
async getYears(): Promise<number[] | null> {
|
||||
const key = this.prefix + 'years';
|
||||
return await this.cacheService.get<number[]>(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set cached years
|
||||
*/
|
||||
async setYears(years: number[], ttl: number = 6 * 3600): Promise<void> {
|
||||
const key = this.prefix + 'years';
|
||||
await this.cacheService.set(key, years, ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cached makes for year
|
||||
*/
|
||||
async getMakes(year: number): Promise<any[] | null> {
|
||||
const key = this.prefix + 'vehicle-data:makes:' + year;
|
||||
return await this.cacheService.get<any[]>(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set cached makes for year
|
||||
*/
|
||||
async setMakes(year: number, makes: any[], ttl: number = 6 * 3600): Promise<void> {
|
||||
const key = this.prefix + 'vehicle-data:makes:' + year;
|
||||
await this.cacheService.set(key, makes, ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cached models for year and make
|
||||
*/
|
||||
async getModels(year: number, makeId: number): Promise<any[] | null> {
|
||||
const key = this.prefix + 'vehicle-data:models:' + year + ':' + makeId;
|
||||
return await this.cacheService.get<any[]>(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set cached models for year and make
|
||||
*/
|
||||
async setModels(year: number, makeId: number, models: any[], ttl: number = 6 * 3600): Promise<void> {
|
||||
const key = this.prefix + 'vehicle-data:models:' + year + ':' + makeId;
|
||||
await this.cacheService.set(key, models, ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cached trims for year and model
|
||||
*/
|
||||
async getTrims(year: number, modelId: number): Promise<any[] | null> {
|
||||
const key = this.prefix + 'vehicle-data:trims:' + year + ':' + modelId;
|
||||
return await this.cacheService.get<any[]>(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set cached trims for year and model
|
||||
*/
|
||||
async setTrims(year: number, modelId: number, trims: any[], ttl: number = 6 * 3600): Promise<void> {
|
||||
const key = this.prefix + 'vehicle-data:trims:' + year + ':' + modelId;
|
||||
await this.cacheService.set(key, trims, ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cached engines for year, model, and trim
|
||||
*/
|
||||
async getEngines(year: number, modelId: number, trimId: number): Promise<any[] | null> {
|
||||
const key = this.prefix + 'vehicle-data:engines:' + year + ':' + modelId + ':' + trimId;
|
||||
return await this.cacheService.get<any[]>(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set cached engines for year, model, and trim
|
||||
*/
|
||||
async setEngines(year: number, modelId: number, trimId: number, engines: any[], ttl: number = 6 * 3600): Promise<void> {
|
||||
const key = this.prefix + 'vehicle-data:engines:' + year + ':' + modelId + ':' + trimId;
|
||||
await this.cacheService.set(key, engines, ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cached VIN decode result
|
||||
*/
|
||||
async getVINDecode(vin: string): Promise<any | null> {
|
||||
const key = this.prefix + 'vin-decode:' + vin.toUpperCase();
|
||||
return await this.cacheService.get<any>(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set cached VIN decode result (7 days for successful decodes, 1 hour for failures)
|
||||
*/
|
||||
async setVINDecode(vin: string, result: any, success: boolean = true): Promise<void> {
|
||||
const key = this.prefix + 'vin-decode:' + vin.toUpperCase();
|
||||
const ttl = success ? 7 * 24 * 3600 : 3600;
|
||||
await this.cacheService.set(key, result, ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidate all vehicle data cache (for admin operations)
|
||||
*/
|
||||
async invalidateVehicleData(): Promise<void> {
|
||||
logger.warn('Vehicle data cache invalidation not implemented (requires pattern deletion)');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user