npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@napolab/kv-response-cache

v0.2.2

Published

Hono middleware for response caching using Cloudflare's KV storage

Downloads

106

Readme

@napolab/kv-response-cache

This package is a middleware for Hono, It provides a response cache mechanism using Cloudflare's KV (Key Value) storage.

One of the key features of this middleware is that it can be used even when a custom domain is not available.

Features

  • Response Cache: The middleware provides a caching mechanism for responses, reducing latency and network load.
  • Cloudflare KV: The caching mechanism leverages Cloudflare's fast and distributed KV storage system.
  • No Custom Domain Required: Unlike some caching solutions, this middleware works even when you don't have a custom domain.

Provided Functionality

Here is an overview of the functions this middleware provides:

kvCaches

The kvCaches function is the primary way to use this middleware. Here's its TypeScript definition:

import type { Context, Env, MiddlewareHandler } from "hono"

type Filter<T extends Record<string, unknown>, V> = {
  [K in keyof T as T[K] extends V ? K : never]: T[K];
};

type Namespace<E extends Env> = string | ((c: Context<E>) => string)
type KVCacheOption<E extends Env> = {
  key: keyof Filter<Exclude<E["Bindings"], undefined>, KVNamespace>
  namespace: Namespace<E>
  options?: KVNamespacePutOptions
}

declare const kvCaches = <E extends Env>({ namespace, key: bindingKey, options }: KVCacheOption<E>): MiddlewareHandler<E>

kvResponseCache

kvResponseCache is a function similar to Cache from @cloudflare/workers-types. It provides an interface for storing responses in KV and is used internally by the kvCaches middleware.

interface KVResponseCache {
  match(key: string): Promise<Response | null>;
  put(key: string, res: Response, options?: KVNamespacePutOptions): Promise<void>;
  delete(key: string): Promise<void>;
}

declare const kvResponseCache = (kv: KVNamespace) => (cacheName: string): KVResponseCache

Installation

Here are the steps to install this project. We'll assume you are using npm for this example:

npm install @napolab/kv-response-cache

Usage

To use this middleware in your Hono application, you need to import the kvCaches function and use it as per your caching needs.

Example

Here is a simple example of how to use kvCaches in a Hono application:

import { kvCaches, kvResponseCache } from "@napolab/kv-response-cache";

const cacheOptions = {
  key: "myKVNamespace",
  namespace: "api-cache",
};

const middleware = kvCaches(cacheOptions);

// Use the middleware in your application
// app.use(middleware);

// delete cache example
app.on(["POST", "PUT", "DELETE"], "*", async (c) => {
  const reqURL = new URL(c.req.url);
  const url = new URL(`${c.env.API_URL}${reqURL.pathname}`);
  url.search = reqURL.search;

  const res = await fetch(url.href, c.req);
  const caches = kvResponseCache(c.env.API_CACHE);

  const cache = caches("api-cache");
  const key = c.req.url;

  c.executionCtx.waitUntil(cache.delete(key));

  return res;
});

In this example, myKVNamespace is the key for the KV Namespace binding and myNamespace is the namespace for the cache. Adjust these values to match your Cloudflare KV settings.