Skip to content

Optimize Redis Storage with Zlib Compression #888

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 4 additions & 3 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@deco/deco",
"version": "1.110.0",
"name": "@vitor/deco",
"version": "1.110.7",
"lock": false,
"exports": {
".": "./mod.ts",
Expand Down Expand Up @@ -95,7 +95,8 @@
"preact": "npm:[email protected]",
"preact-render-to-string": "npm:[email protected]",
"simple-git": "npm:simple-git@^3.25.0",
"std/": "https://deno.land/[email protected]/"
"std/": "https://deno.land/[email protected]/",
"zlib": "node:zlib"
},
"compilerOptions": {
"jsx": "react-jsx",
Expand Down
15 changes: 11 additions & 4 deletions runtime/caches/redis.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { assertEquals } from "@std/assert";
import { create, type RedisConnection } from "./redis.ts";
import type { SetOptions } from "npm:@redis/client@^1.6.0";
import { deflateSync } from "zlib";

Deno.test({
name: ".match",
Expand All @@ -9,13 +10,19 @@ Deno.test({
}, async (t) => {
const namespace = "test";

const encoder = new TextEncoder();
const store: RedisConnection = {
get: (cacheKey: string): string => {
const data: { [key: string]: string } = {
a94a8fe5ccb19ba61c4c0873d391e987982fbbd3test: JSON.stringify({
body: "body",
status: 200,
}),
"a94a8fe5ccb19ba61c4c0873d391e987982fbbd3test": btoa(
String.fromCharCode(
...deflateSync(encoder.encode(JSON.stringify({
body: "body",
headers: {},
status: 200,
}))),
),
),
};

return data[cacheKey];
Expand Down
70 changes: 44 additions & 26 deletions runtime/caches/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
type RedisModules,
type RedisScripts,
} from "npm:@redis/client@^1.6.0";
import { promisify } from "node:util";
import { deflate, unzip } from "node:zlib";

const CONNECTION_TIMEOUT = 500;
const COMMAND_TIMEOUT = 500;
Expand All @@ -31,16 +33,34 @@ export const isAvailable = Deno.env.has("LOADER_CACHE_REDIS_URL");

async function serialize(response: Response): Promise<string> {
const body = await response.text();

return JSON.stringify({
const data = JSON.stringify({
body: body,
headers: response.headers,
headers: Object.fromEntries(response.headers.entries()),
status: response.status,
});

const encoder = new TextEncoder();
const do_deflate = promisify(deflate);
const compressed = await do_deflate(encoder.encode(data));

// Process bytes in chunks to avoid call stack issues
let binary = "";
const bytes = new Uint8Array(compressed);
const len = bytes.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary);
}

function deserialize(raw: string): Response {
const { body, headers, status } = JSON.parse(raw);
async function deserialize(raw: string): Promise<Response> {
const compressed = Uint8Array.from(atob(raw), (c) => c.charCodeAt(0));
const do_unzip = promisify(unzip);

const decompressed = await do_unzip(compressed);
const { body, headers, status } = JSON.parse(
new TextDecoder().decode(decompressed),
);
return new Response(body, { headers, status });
}

Expand Down Expand Up @@ -89,26 +109,22 @@ export function create(redis: RedisConnection | null, namespace: string) {
options?: CacheQueryOptions,
): Promise<Response | undefined> => {
assertNoOptions(options);

const generateKey = withCacheNamespace(namespace);

const result = await generateKey(request)
.then((cacheKey: string) =>
waitOrReject<string | null>(
() => redis?.get(cacheKey) ?? Promise.resolve(null),
COMMAND_TIMEOUT,
)
)
.then((result: string | null) => {
if (!result) {
return undefined;
}
try {
const cacheKey = await generateKey(request);
const result = await waitOrReject<string | null>(
() => redis?.get(cacheKey) ?? Promise.resolve(null),
COMMAND_TIMEOUT,
);

return deserialize(result);
})
.catch(() => undefined);

return result;
if (!result) {
return undefined;
}
return await deserialize(result);
} catch {
return undefined;
}
},
put: async (
request: RequestInfo | URL,
Expand All @@ -117,13 +133,13 @@ export function create(redis: RedisConnection | null, namespace: string) {
const req = new Request(request);
assertCanBeCached(req, response);

const generateKey = withCacheNamespace(namespace);
const cacheKey = await generateKey(request);

if (!response.body) {
return;
}

const generateKey = withCacheNamespace(namespace);
const cacheKey = await generateKey(request);

serialize(response)
.then((data) =>
waitOrReject<string | null>(
Expand All @@ -132,7 +148,9 @@ export function create(redis: RedisConnection | null, namespace: string) {
COMMAND_TIMEOUT,
)
)
.catch(() => {});
.catch((error) => {
console.log(`${cacheKey} - Error`, error);
});
},
};
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// deno-lint-ignore-file no-explicit-any
import { HTTPException } from "@hono/hono/http-exception";
import { DECO_MATCHER_HEADER_QS } from "../blocks/matcher.ts";
import { context, Context } from "../deco.ts";
import { Context, context } from "../deco.ts";
import { Exception, getCookies, SpanStatusCode } from "../deps.ts";
import { startObserve } from "../observability/http.ts";
import { logger } from "../observability/mod.ts";
Expand Down
Loading