Skip to content

Commit

Permalink
handle head in cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjwalguptaofficial committed Nov 24, 2024
1 parent 796c764 commit bf2a197
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
15 changes: 10 additions & 5 deletions src/providers/cache_guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import { IHttpResult } from "../interfaces";

export class CacheGuard extends Guard {
async check(): Promise<void | IHttpResult> {
if (this.request.method !== HTTP_METHOD.Get) {
// set cache true to allow walls to not cache data;
this.data[FROM_CACHE] = true;
return;
switch (this.request.method) {
case HTTP_METHOD.Get:
case HTTP_METHOD.Head:
// let the execution go to below
break;
default:
// set cache true to allow walls to not cache data;
this.data[FROM_CACHE] = true;
return;
}
// get cache condition from routes
const componentProp = this['componentProp_']
Expand All @@ -22,4 +27,4 @@ export class CacheGuard extends Guard {
return cacheData.data;
}
}
}
}
17 changes: 15 additions & 2 deletions tests/general/src/controllers/cache_controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Controller, http, cacheFor, HTTP_METHOD, textResult, jsonResult, route, HTTP_STATUS_CODE } from "fortjs";
import { Controller, http, cacheFor, HTTP_METHOD, textResult, jsonResult, route, HTTP_STATUS_CODE, worker } from "fortjs";

const hits = {

};
export class CacheController extends Controller {

@http.post("/add")
Expand Down Expand Up @@ -74,4 +77,14 @@ export class CacheController extends Controller {
data: obj
})
}
}

@worker()
@route("/hit")
@cacheFor(Infinity)
async cacheHit() {
let prevHitValue = hits[this.request.method];
prevHitValue = prevHitValue ? prevHitValue : 0;
hits[this.request.method] = ++prevHitValue;
return jsonResult(hits);
}
}
42 changes: 42 additions & 0 deletions tests/general/tests/e2e/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,46 @@ describe("/user", () => {
expect(storeKeys.length).toBe(1);
expect(storeKeys).toEqual(["CacheController_getFruits"]);
});

it("get cache hit", async () => {
const response = await request.get('/cache/hit');
expect(response.status).toBe(200);
const data = response.data;
expect(data).toEqual({ "GET": 1 });
});

it("get cache hit again", async () => {
const response = await request.get('/cache/hit');
expect(response.status).toBe(200);
const data = response.data;
expect(data).toEqual({ "GET": 1 });
});

it("head cache hit", async () => {
const response = await request.head('/cache/hit');
expect(response.status).toBe(200);
const data = response.data;
expect(data).toEqual("");
});

it("head cache hit again", async () => {
const response = await request.head('/cache/hit');
expect(response.status).toBe(200);
const data = response.data;
expect(data).toEqual("");
});

it("post cache hit", async () => {
const response = await request.post('/cache/hit');
expect(response.status).toBe(200);
const data = response.data;
expect(data).toEqual({ "GET": 1, "POST": 1 });
});

it("post cache hit again", async () => {
const response = await request.post('/cache/hit');
expect(response.status).toBe(200);
const data = response.data;
expect(data).toEqual({ "GET": 1, "POST": 2 });
});
});

0 comments on commit bf2a197

Please sign in to comment.