diff --git a/packages/storage-memory/src/index.ts b/packages/storage-memory/src/index.ts index 00a0b1d..4a1567f 100644 --- a/packages/storage-memory/src/index.ts +++ b/packages/storage-memory/src/index.ts @@ -1,12 +1,26 @@ import { ICacheItem, IStorage } from "node-ts-cache" +import { IMemoryCacheOptions } from "./memory-types" +export * from "./memory-types" export class MemoryStorage implements IStorage { private memCache: any = {} + private shouldClone: boolean = false; // by default no cloning (default behaviour) - constructor() {} + constructor(options?:IMemoryCacheOptions) { + if(options){ + this.shouldClone = options.clone + } + } public async getItem(key: string): Promise { - return this.memCache[key] + let response; + if(this.shouldClone){ + const cacheData = this.memCache[key]; + response = cacheData?JSON.parse(JSON.stringify(this.memCache[key])):cacheData; + }else{ + response = this.memCache[key] + } + return response; } public async setItem(key: string, content: any): Promise { diff --git a/packages/storage-memory/src/memory-types.ts b/packages/storage-memory/src/memory-types.ts new file mode 100644 index 0000000..45552e4 --- /dev/null +++ b/packages/storage-memory/src/memory-types.ts @@ -0,0 +1,3 @@ +export interface IMemoryCacheOptions { + clone: boolean // when set to true data returned from cache will be deep copied (deep copy will be returned) +} \ No newline at end of file