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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@bklarjs/cache

v1.0.0

Published

Response caching middleware for Bklar with ETag support and pluggable stores.

Readme

@bklarjs/cache ⚡

NPM Version License: MIT

High-performance server-side caching middleware for the bklar framework.

It caches responses in memory (or a custom store like Redis), generates ETag headers automatically using Bun's native hashing, and handles 304 Not Modified responses to save bandwidth.


✨ Features

  • Bun Native Hashing: Uses Bun.hash (Wyhash) for extremely fast ETag generation.
  • 💾 Pluggable Storage: Comes with an in-memory store but supports any backend (Redis, Memcached, etc.).
  • 🧠 Smart Caching: Automatically handles query parameters sorting for consistent cache keys.
  • 📉 Bandwidth Saving: Supports If-None-Match headers to return 304 Not Modified.
  • 🧩 Configurable: Customize TTL, allowed methods, and key generation strategies.

📦 Installation

This package is designed to work with bklar.

bun add bklar @bklarjs/cache

🚀 Usage

Basic In-Memory Caching

import { Bklar } from "bklar";
import { cache } from "@bklarjs/cache";

const app = Bklar();

// Cache all GET requests for 1 minute (60,000ms)
app.use(cache({ ttl: 60000 }));

app.get("/expensive-data", async (ctx) => {
  // Simulate DB work
  await Bun.sleep(500);
  return ctx.json({ data: "Expensive Result", timestamp: Date.now() });
});

app.listen(3000);

First Request:

  • Latency: ~500ms
  • Header: X-Cache: MISS

Second Request:

  • Latency: < 1ms
  • Header: X-Cache: HIT

Conditional Revalidation (ETags)

The middleware automatically adds ETag headers.

If a client sends If-None-Match: "W/..." and the content hasn't changed, the server responds with 304 Not Modified and an empty body, saving bandwidth.

⚙️ Configuration

| Option | Type | Default | Description | | :------------- | :---------------- | :---------------- | :------------------------------------------- | | ttl | number | 60000 | Time to live in milliseconds. | | methods | string[] | ['GET', 'HEAD'] | HTTP methods to cache. | | store | CacheStore | MemoryStore | Storage implementation. | | keyGenerator | (ctx) => string | (URL + Query) | Function to generate unique cache keys. | | addHeaders | boolean | true | Whether to add X-Cache and ETag headers. |

Using Redis (Example)

You can easily implement the CacheStore interface to use Redis.

import { createClient } from "redis";
import type { CacheStore, CacheEntry } from "@bklarjs/cache";

class RedisStore implements CacheStore {
  private client = createClient();

  constructor() {
    this.client.connect();
  }

  async get(key: string) {
    const data = await this.client.get(key);
    return data ? JSON.parse(data) : undefined;
  }

  async set(key: string, value: CacheEntry, ttl: number) {
    // Store as JSON string with TTL (converted to seconds)
    await this.client.set(key, JSON.stringify(value), {
      EX: Math.ceil(ttl / 1000),
    });
  }

  async delete(key: string) {
    await this.client.del(key);
  }
}

app.use(cache({ store: new RedisStore() }));

🤝 Contributing

Contributions are welcome! Please open an issue or submit a Pull Request to the main bklar repository.

📄 License

This project is licensed under the MIT License.