summaryrefslogtreecommitdiff
path: root/src/scripts/controllers/connection/cache.ts
blob: c1c47c2d787e582087be5b62f2107523f1db50e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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 {
        const 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);
    }
}