Skip to content

Commit

Permalink
Adding cache
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosNunezMX committed Jun 6, 2024
1 parent 757164e commit 883db5c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
32 changes: 32 additions & 0 deletions source/auth/Cache.ts
Original file line number Diff line number Diff line change
@@ -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<Val> = Map<typeof AuthMethod | typeof Method, Val>;
export class Cache{
private _Cache: Map<string, ClassCache<any>> = new Map();
lastUpdate: Date = new Date();

getCache<R>(c: typeof AuthMethod | typeof Method, studentCode: string){
return this._Cache.get(studentCode)?.get(c) as R;
}

setCache<I>(student: string, c: typeof AuthMethod | typeof Method, value: I){
const prev = this._Cache.get(student);
if(!prev){
const _Cache = new Map<typeof AuthMethod | typeof Method, I>();
_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);
}
}
5 changes: 5 additions & 0 deletions source/auth/Login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Options>;
Cache?: Cache;
constructor(credentials: credentials, options?: Partial<Options>){
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.
Expand Down
16 changes: 16 additions & 0 deletions source/utils/Method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class Method{
*/
export abstract class AuthMethod<Return = void> extends Method {
declare protected Auth: Login;
ShouldUpCache: boolean = true;
constructor(Auth: Login){
super();
this.Auth = Auth;
Expand All @@ -47,4 +48,19 @@ export abstract class AuthMethod<Return = void> extends Method {
await this.Auth.exec.bind(this.Auth)();
}
}

UpdateCache<T>(data: T, c: typeof AuthMethod): void{
if(this.ShouldUpCache)
return;
if(this.Auth.Cache)
this.Auth.Cache.setCache(this.Auth.StudentCode!, c, data);
}


getCache<R>(c: typeof AuthMethod): R | undefined{
if(this.ShouldUpCache)
return;
if(this.Auth.Cache)
return this.Auth.Cache?.getCache<R>(c, this.Auth.StudentCode!);
}
}

0 comments on commit 883db5c

Please sign in to comment.