summaryrefslogtreecommitdiff
path: root/src/scripts/controllers/connection/cache.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/scripts/controllers/connection/cache.ts')
-rw-r--r--src/scripts/controllers/connection/cache.ts85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/scripts/controllers/connection/cache.ts b/src/scripts/controllers/connection/cache.ts
new file mode 100644
index 00000000..15517519
--- /dev/null
+++ b/src/scripts/controllers/connection/cache.ts
@@ -0,0 +1,85 @@
+export enum CacheStatus {
+ MISS,
+ FETCHING,
+ HIT,
+ NOT_CACHABLE
+}
+
+
+interface ICachableObject {
+ status: CacheStatus;
+ object: any;
+ callbacks: any[];
+}
+
+
+export class CacheController {
+ private static CACHABLE_ROUTES = [
+ "/specifications/psus/{id}",
+ "/specifications/cooling-items/{id}",
+ "/specifications/cpus/{id}",
+ "/specifications/gpus/{id}",
+ "/specifications/memories/{id}",
+ "/specifications/storages/{id}",
+ "/specifications/failure-models/{id}",
+ ];
+
+ // Maps every route name to a map of IDs => objects
+ private routeCaches: { [keys: string]: { [keys: number]: ICachableObject } };
+
+
+ constructor() {
+ this.routeCaches = {};
+
+ CacheController.CACHABLE_ROUTES.forEach((routeName: string) => {
+ this.routeCaches[routeName] = {};
+ })
+ }
+
+ public checkCache(request: IRequest): CacheStatus {
+ if (request.method === "GET" && CacheController.CACHABLE_ROUTES.indexOf(request.path) !== -1) {
+ if (this.routeCaches[request.path][request.parameters.path["id"]] === undefined) {
+ this.routeCaches[request.path][request.parameters.path["id"]] = {
+ status: CacheStatus.MISS,
+ object: null,
+ callbacks: []
+ };
+ return CacheStatus.MISS;
+ } else {
+ return this.routeCaches[request.path][request.parameters.path["id"]].status;
+ }
+ } else {
+ return CacheStatus.NOT_CACHABLE;
+ }
+ }
+
+ public fetchFromCache(request: IRequest): any {
+ return this.routeCaches[request.path][request.parameters.path["id"]].object;
+ }
+
+ public setToFetching(request: IRequest): void {
+ this.routeCaches[request.path][request.parameters.path["id"]].status = CacheStatus.FETCHING;
+ }
+
+ public onFetch(request: IRequest, response: IResponse): any {
+ let pathWithoutVersion = request.path.replace(/\/v\d+/, "");
+ this.routeCaches[pathWithoutVersion][request.parameters.path["id"]].status = CacheStatus.HIT;
+ this.routeCaches[pathWithoutVersion][request.parameters.path["id"]].object = response.content;
+
+ this.routeCaches[pathWithoutVersion][request.parameters.path["id"]].callbacks.forEach((callback) => {
+ callback({
+ status: {
+ code: 200
+ },
+ content: response.content,
+ id: request.id
+ });
+ });
+
+ this.routeCaches[pathWithoutVersion][request.parameters.path["id"]].callbacks = [];
+ }
+
+ public registerCallback(request: IRequest, callback): any {
+ this.routeCaches[request.path][request.parameters.path["id"]].callbacks.push(callback);
+ }
+}