Skip to content

Commit

Permalink
Review
Browse files Browse the repository at this point in the history
  • Loading branch information
azisaka committed Oct 9, 2024
1 parent ec9e84c commit 8c97643
Showing 1 changed file with 38 additions and 20 deletions.
58 changes: 38 additions & 20 deletions runtime/caches/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@ import {
type RedisModules,
type RedisScripts,
} from "npm:@redis/client@^1.6.0";
import { logger } from "../../observability/mod.ts";

const REDIS_URL = Deno.env.get("LOADER_CACHE_REDIS_URL") ||
"redis://localhost:6379/0";

let redis:
| null
| RedisClientType<RedisModules, RedisFunctions, RedisScripts> = null;
const RECONNECTION_TIMEOUT = 5000;

async function serialize(response: Response): Promise<string> {
const body = await response.text();
Expand All @@ -34,6 +30,10 @@ function deserialize(raw: string): Response {
return new Response(body, { headers, status });
}

let redis:
| null
| RedisClientType<RedisModules, RedisFunctions, RedisScripts> = null;

export const caches: CacheStorage = {
delete: (_cacheName: string): Promise<boolean> => {
throw new Error("Not Implemented");
Expand All @@ -53,15 +53,35 @@ export const caches: CacheStorage = {
open: async (namespace: string): Promise<Cache> => {
const client = createClient({
url: REDIS_URL,
}).on("error", () => {
if (client.isOpen) {
client.disconnect();
}

setTimeout(async () => redis = await client.connect(), 5000);
});
const wait = (ms: number) => new Promise((r) => setTimeout(r, ms));

const connect = () => {
const { promise, resolve } = Promise.withResolvers();

client.connect().then((
connection: RedisClientType<
RedisModules,
RedisFunctions,
RedisScripts
>,
) => {
redis ??= connection;
resolve(true);
});

client.on("error", () => {
if (client.isOpen) {
client.disconnect();
}

wait(RECONNECTION_TIMEOUT).then(connect);
});

return promise;
};

redis ??= await client.connect();
connect().then(() => console.log("connected!"));

return Promise.resolve({
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Cache/add) */
Expand Down Expand Up @@ -98,9 +118,7 @@ export const caches: CacheStorage = {

return true;
})
.catch((err: Error) => {
logger.error(err);

.catch(() => {
return false;
});
},
Expand All @@ -121,9 +139,7 @@ export const caches: CacheStorage = {

return deserialize(result);
})
.catch((err: Error) => {
logger.error(err);

.catch(() => {
return undefined;
});
},
Expand All @@ -141,7 +157,7 @@ export const caches: CacheStorage = {
const generateKey = withCacheNamespace(namespace);
const cacheKey = await generateKey(request);

return serialize(response)
serialize(response)
.then((data) => {
const expirationTimestamp = Date.parse(
response.headers.get("expires") ?? "",
Expand All @@ -151,7 +167,9 @@ export const caches: CacheStorage = {

return redis?.set(cacheKey, data, { PX: ttl });
})
.catch((err) => logger.error(err));
.catch(() => {
return;
});
},
});
},
Expand Down

0 comments on commit 8c97643

Please sign in to comment.