diff --git a/source/auth/Cache.ts b/source/auth/Cache.ts new file mode 100644 index 0000000..c4ca31e --- /dev/null +++ b/source/auth/Cache.ts @@ -0,0 +1,32 @@ +import { StudentPlans } from "../info/Plans.js"; +import type { Plans } from "../info/PlansType.js"; +import type { AuthMethod, Method } from "../utils/Method.js"; + +type ClassCache = Map; +export class Cache{ + private _Cache: Map> = new Map(); + lastUpdate: Date = new Date(); + + getCache(c: typeof AuthMethod | typeof Method, studentCode: string){ + return this._Cache.get(studentCode)?.get(c) as R; + } + + setCache(student: string, c: typeof AuthMethod | typeof Method, value: I){ + const prev = this._Cache.get(student); + if(!prev){ + const _Cache = new Map(); + _Cache.set(c, value); + this._Cache.set(student, _Cache); + return; + } + prev.set(c, value); + } + + clearCache(student: string){ + this._Cache.delete(student); + } + + getPlanfromCache(student: string): Plans | undefined{ + return this.getCache(StudentPlans as typeof AuthMethod, student); + } +} \ No newline at end of file diff --git a/source/auth/Login.ts b/source/auth/Login.ts index ddd5f05..c3fd22d 100644 --- a/source/auth/Login.ts +++ b/source/auth/Login.ts @@ -5,6 +5,7 @@ import { HeaderPreset } from "../utils/CommonHeaders.js"; import { ErrorHandling } from "../error/Request.js"; import { encryptPassword } from "../utils/crypto/Password.js"; +import { Cache } from "./Cache.js"; type TokenType = { token: string | null; @@ -44,11 +45,15 @@ export class Login extends Method { protected Route: string = "https://micro-leo.udg.mx/login/v1/validar"; public StudentCode?: string; private options?: Partial; + Cache?: Cache; constructor(credentials: credentials, options?: Partial){ super() this.User = credentials.User; this.Password = credentials.Password; this.options = options; + + if(this.options?.createCache) + this.Cache = new Cache(); } /** * Execute the login method, it's required for most of the methods. diff --git a/source/utils/Method.ts b/source/utils/Method.ts index 22fc5f1..2981dbd 100644 --- a/source/utils/Method.ts +++ b/source/utils/Method.ts @@ -35,6 +35,7 @@ export class Method{ */ export abstract class AuthMethod extends Method { declare protected Auth: Login; + ShouldUpCache: boolean = true; constructor(Auth: Login){ super(); this.Auth = Auth; @@ -47,4 +48,19 @@ export abstract class AuthMethod extends Method { await this.Auth.exec.bind(this.Auth)(); } } + + UpdateCache(data: T, c: typeof AuthMethod): void{ + if(this.ShouldUpCache) + return; + if(this.Auth.Cache) + this.Auth.Cache.setCache(this.Auth.StudentCode!, c, data); + } + + + getCache(c: typeof AuthMethod): R | undefined{ + if(this.ShouldUpCache) + return; + if(this.Auth.Cache) + return this.Auth.Cache?.getCache(c, this.Auth.StudentCode!); + } } \ No newline at end of file