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

@yorozu/resource-cache

v1.0.151

Published

blob TTL / bytes / count engine (Collection in)

Readme

@yorozu/resource-cache

Blob TTL / bytes / count engine. Persistence is an injected @yorozu/db Collection. This package does not open IndexedDB or SQLite; the host supplies a driver.

Setup

import { openMemoryDb } from "@yorozu/db" // tests; prod: host driver.open
import type { Logger } from "@yorozu/log"
import { BytesLruMap, createResourceCache, dropDelete, dropStripBlob, resourceSchema } from "@yorozu/resource-cache"

let db = await openMemoryDb(resourceSchema("media", ["blobs", "thumbs"]))
let log: Logger | undefined
let blob: Blob
let caps = {
    maxBytes: 1024 ** 3,
    maxAgeMs: 7 * 24 * 60 * 60 * 1000,
    maxEntries: 2000,
}

let strip = dropStripBlob()
let blobs = createResourceCache({
    collection: db.collection("blobs"),
    drop: {
        apply(col, plan) {
            if (plan.reason === "bytes") return strip.apply(col, plan)
            return dropDelete.apply(col, plan)
        },
    },
    caps,
    evictMetaEveryNPuts: 50,
    log, // optional; silent default
    l1: new BytesLruMap({
        maxBytes: 64 * 1024 * 1024,
        maxEntries: 512,
        sizeOf: (r) => r.bytes,
        onEvict: (key) => {
            /* revoke object URLs */
        },
    }),
    onDropped: (keys, reason) => {
        if (reason === "bytes" || reason === "ttl") {
            /* revoke object URLs */
        }
    },
})

await blobs.put({ key: "a", storedAt: Date.now(), blob, meta: { name: "a.png" } })
await blobs.setCaps({ ...caps, maxBytes: 512 * 1024 ** 2 })
await blobs.evict("full")

let thumbs = createResourceCache({
    collection: db.collection("thumbs"),
    drop: dropDelete,
    caps: { maxBytes: 512 * 1024 * 1024, maxAgeMs: 7 * 24 * 60 * 60 * 1000 },
})

Logger is optional. Internally: opts.log ?? init("yorozu-resource-cache", "evict"). Bytes trim logs skip / start / done as info messages.

Omitted caps turn that policy off. dropDelete removes rows. dropStripBlob() keeps the row, bytes === 0, no blob. A host that wants strip on bytes and delete on TTL/count composes plan.reason in its own DropHandler.

Classed bytes trim

Optional class on put ("thumb" | "original" | any string). Pass preferDrop: ["thumb"] so bytes trim drops matching classes first (in array order, oldest within each class), then the rest by age. Two collections (blobs + thumbs) remain valid and are not required. Classed trim stays keysOnly: it scans covering by-evict then joins class from side index by-evict-class (["storedAt","bytes","class"]) — never values. Rows without a string class are absent from by-evict-class but still appear on by-evict. Omit / empty preferDrop stays a single by-evict keysOnly walk.

Default resourceSchema version is 2 so hosts that call it pick up by-evict-class on upgrade. Callers who pass an explicit version keep that number. Hosts must bump their DbSchema.version to create the new index (IDB skips an index name that already exists, so by-evict keyPath stays ["storedAt","bytes"]).

Musts

  • Port is Collection. Host opens memory / IDB / SQLite via a DbDriver and passes db.collection(...).
  • Covering index by-evict is ["storedAt", "bytes"]. Side index by-evict-class is ["storedAt", "bytes", "class"]. TTL/count eviction walks scan("by-evict", { keysOnly: true, limit?: n }). Count trim passes limit: extra; seed / ledger rebuild omit limit. Bytes trim is always keysOnly; with preferDrop it also keysOnly-scans by-evict-class and joins class by primary key. Never getAll() on blob collections.
  • Bytes: getBytesTotal then ordered pick. Under cap: do not scan for the pick.
  • put bytes come from blob.size. If maxBytes is set and bytes > maxBytes, put is a no-op.
  • Tests in this package use openMemoryDb only. Do not import @yorozu/db-idb or @yorozu/db-sqlite here.