diff --git a/project/src/di/Container.ts b/project/src/di/Container.ts index 224ce30b..2058c89b 100644 --- a/project/src/di/Container.ts +++ b/project/src/di/Container.ts @@ -209,6 +209,7 @@ import { CustomLocationWaveService } from "@spt/services/CustomLocationWaveServi import { DatabaseService } from "@spt/services/DatabaseService"; import { FenceService } from "@spt/services/FenceService"; import { GiftService } from "@spt/services/GiftService"; +import { InMemoryCacheService } from "@spt/services/InMemoryCacheService"; import { InsuranceService } from "@spt/services/InsuranceService"; import { ItemBaseClassService } from "@spt/services/ItemBaseClassService"; import { ItemFilterService } from "@spt/services/ItemFilterService"; @@ -227,7 +228,6 @@ import { PlayerService } from "@spt/services/PlayerService"; import { PmcChatResponseService } from "@spt/services/PmcChatResponseService"; import { ProfileActivityService } from "@spt/services/ProfileActivityService"; import { ProfileFixerService } from "@spt/services/ProfileFixerService"; -import { ProfileSnapshotService } from "@spt/services/ProfileSnapshotService"; import { RagfairCategoriesService } from "@spt/services/RagfairCategoriesService"; import { RagfairLinkedItemService } from "@spt/services/RagfairLinkedItemService"; import { RagfairOfferService } from "@spt/services/RagfairOfferService"; @@ -744,7 +744,7 @@ export class Container { }); depContainer.register("CustomItemService", CustomItemService); depContainer.register("BotEquipmentFilterService", BotEquipmentFilterService); - depContainer.register("ProfileSnapshotService", ProfileSnapshotService, { + depContainer.register("InMemoryCacheService", InMemoryCacheService, { lifecycle: Lifecycle.Singleton, }); depContainer.register("ItemFilterService", ItemFilterService, { diff --git a/project/src/services/InMemoryCacheService.ts b/project/src/services/InMemoryCacheService.ts new file mode 100644 index 00000000..2ff340e6 --- /dev/null +++ b/project/src/services/InMemoryCacheService.ts @@ -0,0 +1,52 @@ +import { ICloner } from "@spt/utils/cloners/ICloner"; +import { inject, injectable } from "tsyringe"; + +@injectable() +export class InMemoryCacheService { + protected cacheData: Record = {}; + + constructor(@inject("PrimaryCloner") protected cloner: ICloner) {} + + /** + * Store data into an in-memory object + * @param key key to store data against + * @param dataToCache - Data to store in cache + */ + public storeByKey(key: string, dataToCache: any): void { + this.cacheData[key] = this.cloner.clone(dataToCache); + } + + /** + * Retreve data stored by a key + * @param key key + * @returns Stored data + */ + public getDataByKey(key: string): any | undefined { + if (this.cacheData[key]) { + return this.cacheData[key]; + } + + return undefined; + } + + /** + * Does data exists against the provided key + * @param key Key to check for data against + * @returns true if exists + */ + public hasStoredDataByKey(key: string): boolean { + if (this.cacheData[key]) { + return true; + } + + return false; + } + + /** + * Remove data stored against key + * @param key Key to remove data against + */ + public clearDataStoredByKey(key: string): void { + delete this.cacheData[key]; + } +} diff --git a/project/src/services/ProfileSnapshotService.ts b/project/src/services/ProfileSnapshotService.ts deleted file mode 100644 index db360ff6..00000000 --- a/project/src/services/ProfileSnapshotService.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { ISptProfile } from "@spt/models/eft/profile/ISptProfile"; -import { ICloner } from "@spt/utils/cloners/ICloner"; -import { inject, injectable } from "tsyringe"; - -@injectable() -export class ProfileSnapshotService { - protected storedProfileSnapshots: Record = {}; - - constructor(@inject("PrimaryCloner") protected cloner: ICloner) {} - - /** - * Store a profile into an in-memory object - * @param sessionID session id - acts as the key - * @param profile - profile to save - */ - public storeProfileSnapshot(sessionID: string, profile: ISptProfile): void { - this.storedProfileSnapshots[sessionID] = this.cloner.clone(profile); - } - - /** - * Retreve a stored profile - * @param sessionID key - * @returns A player profile object - */ - public getProfileSnapshot(sessionID: string): ISptProfile | undefined { - if (this.storedProfileSnapshots[sessionID]) { - return this.storedProfileSnapshots[sessionID]; - } - - return undefined; - } - - /** - * Does a profile exists against the provided key - * @param sessionID key - * @returns true if exists - */ - public hasProfileSnapshot(sessionID: string): boolean { - if (this.storedProfileSnapshots[sessionID]) { - return true; - } - - return false; - } - - /** - * Remove a stored profile by key - * @param sessionID key - */ - public clearProfileSnapshot(sessionID: string): void { - delete this.storedProfileSnapshots[sessionID]; - } -}