Skip to content

HTTP Caching for Deno - in memory and redis storage support. Inspired by the Service Worker Cache API.

License

Notifications You must be signed in to change notification settings

cloudydeno/deno_httpcache

 
 

Repository files navigation

deno_httpcache

HTTP Caching for Deno - in memory and redis storage support. Inspired by the Service Worker Cache API.

Usage

Setting, getting, and deleting items in the cache:

import { inMemoryCache } from "https://deno.land/x/[email protected]/in_memory.ts";

const cache = inMemoryCache(5);

const req = new Request("https://deno.land/[email protected]/version.ts");
const resp = await fetch(req);

await cache.set(req, resp);

const cachedResp = await cache.get(req); // or `cache.get(req.url)`
if (cachedResp === undefined) throw new Error("Response not found in cache");
console.log(cachedResp.status); // 200
console.log(cachedResp.headers.get("content-type")); // application/typescript; charset=utf-8

await cache.remove(req); // or `cache.remove(req.url)`
console.log(await cache.get(req)); // undefined

And with redis:

import { redisCache } from "https://deno.land/x/[email protected]/redis.ts";

const cache = await redisCache("redis://127.0.0.1:6379");

// you can also optionally specify a prefix to use for the cache key:
const cache = await redisCache("redis://127.0.0.1:6379", "v1-");

Contributing

Before submitting a PR, please run these three steps and check that they pass.

  1. deno fmt
  2. deno lint --unstable
  3. deno test --allow-net this requires you to have a redis server running at 127.0.0.1:6379

About

HTTP Caching for Deno - in memory and redis storage support. Inspired by the Service Worker Cache API.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 100.0%