Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/cache optin #269

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
simple test
lane711 committed Dec 19, 2024
commit 19d63ecb85cbf0fd7f667202eaa0e0f871a8c9fe
4 changes: 2 additions & 2 deletions src/pages/api/v1/[table].test.ts
Original file line number Diff line number Diff line change
@@ -44,7 +44,7 @@ describe('GET /api/v1/:table', () => {
};

const response = await GET(context as any);
const result = await response.json();
const result = await response.json() as { error: string };

expect(response.status).toBe(500);
expect(result.error).toBe('Table "undefined-table" not defined in your schema');
@@ -60,7 +60,7 @@ describe('GET /api/v1/:table', () => {
(getApiAccessControlResult as any).mockResolvedValue(false);

const response = await GET(context as any);
const result = await response.json();
const result = await response.json() as { error: string };

expect(response.status).toBe(401);
expect(result.error).toBe('Unauthorized');
34 changes: 22 additions & 12 deletions src/pages/api/v1/cache-test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import { return200 } from "@services/return-types";
import type { APIRoute } from "astro";
export const GET = async (context) => {
const { request } = context;
const { url } = request;

export const GET: APIRoute = async (context) => {
const request = context.request;
const cacheUrl = new URL(context.url);

// Construct the cache key from the cache URL
const cacheKey = new Request(cacheUrl.toString(), request);
const cache = context.locals.runtime.caches.default;

console.log('before', cache)
let cacheFound = false;
const cacheResponse = await cache.match(url);
if (cacheResponse) {
console.log("Cache hit");
cacheFound = true;
return cacheResponse;
} else{
console.log("Cache miss");

cache.put('cacheKey', { message: "Cache test" });
}

console.log('after', cache)
var response = new Response(
JSON.stringify({
"message": "Cache test: " + cacheFound
}),
{ status: 200 }
);
response.headers.set("cache-control", "public, max-age=600000");
response.headers.set("content-type", "application/json");

await cache.put(url, new Response(response.body, response));

return return200({ message: "Cache test" });
return response;
};
64 changes: 32 additions & 32 deletions src/pages/api/v1/cache.ts
Original file line number Diff line number Diff line change
@@ -2,48 +2,48 @@ import { return200 } from "@services/return-types";
import type { APIRoute } from "astro";

export const GET: APIRoute = async (context) => {
const start = Date.now();
let params = context.params;
console.log("params", params);
console.log("context.url", context.url);
// const start = Date.now();
// let params = context.params;
// console.log("params", params);
// console.log("context.url", context.url);

const urlToCache = "https://demo.sonicjs.com/api/v1/posts?limit=10";
// const urlToCache = "https://demo.sonicjs.com/api/v1/posts?limit=10";

console.log("processing url start", urlToCache);
// console.log("processing url start", urlToCache);

// Construct the cache key from the cache URL
const cacheKey = new Request(urlToCache, context.request);
const cache = context.locals.runtime.caches.default;
// // Construct the cache key from the cache URL
// const cacheKey = new Request(urlToCache, context.request);
// const cache = context.locals.runtime.caches.default;

// Check whether the value is already available in the cache
// if not, fetch it from origin, and store it in the cache
let response = await cache.match(urlToCache);
// // Check whether the value is already available in the cache
// // if not, fetch it from origin, and store it in the cache
// let response = await cache.match(urlToCache);

if (!response) {
console.log(`--> MISS: ${urlToCache}`);
// If not in cache, get it from origin
response = await fetch(urlToCache);
// if (!response) {
// console.log(`--> MISS: ${urlToCache}`);
// // If not in cache, get it from origin
// response = await fetch(urlToCache);

// Must use Response constructor to inherit all of response's fields
response = new Response(response.body, response);
// // Must use Response constructor to inherit all of response's fields
// response = new Response(response.body, response);

// Cache API respects Cache-Control headers. Setting s-max-age to 10
// will limit the response to be in cache for 10 seconds max
// // Cache API respects Cache-Control headers. Setting s-max-age to 10
// // will limit the response to be in cache for 10 seconds max

// Any changes made to the response here will be reflected in the cached value
response.headers.append("Cache-Control", "s-maxage=2592000");
response.headers.append("SonicJs-Source", "fetch");
// // Any changes made to the response here will be reflected in the cached value
// response.headers.append("Cache-Control", "s-maxage=2592000");
// response.headers.append("SonicJs-Source", "fetch");

postProcessRequest(context, cache, cacheKey, urlToCache, response, start);
return response;
} else {
console.log( `--> HIT: ${urlToCache}`);
const responeCloned = new Response(response.body, response);
responeCloned.headers.delete("SonicJs-Source");
responeCloned.headers.append("SonicJs-Source", "cache");
// postProcessRequest(context, cache, cacheKey, urlToCache, response, start);
// return response;
// } else {
// console.log( `--> HIT: ${urlToCache}`);
// const responeCloned = new Response(response.body, response);
// responeCloned.headers.delete("SonicJs-Source");
// responeCloned.headers.append("SonicJs-Source", "cache");

return responeCloned;
}
// return responeCloned;
// }

return new Response(JSON.stringify({ url: "check" }), {
headers: { "Content-Type": "application/json" },