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

@mateus-pires/file-cache

v1.1.1

Published

powerfull caching tools

Downloads

3

Readme

File Cache

File Cache uses Map, fs/promises and some "clever" tricks to handle fs <=> memory data sharing.

Benchmarks | API Documentation

It is being designed to be a very useful/convenient caching tool.

I will focus on performance later (maybe on v2), but remember: since it makes heavy use of the file system, a SSD is higly recommended at enterprise scale.

How to use

Cache

import { Cache, Store } from "@mateus-pires/file-cache";

const store = new Store

await store.setup('./cache-dir')

const cache = new Cache({
  max_size: 500,
  store,
  value_manager: { ... } as CacheValueManager,
});

// Cache exposes its internal map iterator, so it can be used like so:

const cache_entries = Array.from(cache);

for(const [key, value] of cache) {}

In the example above, we are creating a new Store instance, configuring it to place the cache files in ./cache-dir and then creating a Cache instance, with a maximum size of 500 items.

This can also be achieved using the static start function.

import { Cache } from "@mateus-pires/file-cache";

const cache = await Cache.start({
  max_size: 500,
  store: { dest: "./cache-dir" },
  value_manager: { ... } as CacheValueManager,
});

Cache Managers

The type of value_manager is intrinsically linked to the type of this Cache instance, since it is responsible for validating the insertion of new values, as well as the transformation to/from the file system.

// cache: Cache<string>
const cache = new Cache({ value_manager: { ... } as CacheValueManager<string> })

Cache only supports Key (check API docs) keys, but the value can be virtually anything, as long as it is possible to save it in a file.

File Cache ships with three built-in value managers:

String

As the name suggests, this value manager handles string values, saving them to the file system in UTF-8 format.

import { CacheManagers } from "@mateus-pires/file-cache";

// cache: Cache<string>
const cache = new Cache({ value_manager: CacheManagers.String });

Zod

Powered by node:v8 and zod (as a peer dependency, please install it if you use it), with this value manager it is possible to save any serializable object, as well as perform type checking at runtime.

Note:

  • Almost all objects are serializable (string, number, null, bigint, undefined, objects, arrays etc).
  • Functions are NOT serializable, nor anything that depends on or includes them (classes, objects that contain functions, etc.), except built-in classes such as Date.
import { CacheManagers } from "@mateus-pires/file-cache";
import z from "zod";

const ZodManager = CacheManagers.Zod(
  z.object({ a: z.string().optional(), b: z.number() })
);

// cache: Cache<{ a?: string; b: number }>
const cache = new Cache({ value_manager: ZodManager });

JSON

JSON is almost the same as String, with the exception that it uses JSON.parse and JSON.stringify.

import { CacheManagers } from "@mateus-pires/file-cache";

// cache: Cache<Record<string, any>>
const cache = new Cache({ value_manager: CacheManagers.JSON });

Note: you can create your own value manager, like so:

import type { CacheValueManager } from "@mateus-pires/file-cache";

const manager: CacheValueManager<AnythingYouWant> = { ... }

BidirectionalCache

This functionality was removed on v1.0.0-dev.3.

Store

The Cache was built to handle information I/O, with features to make everything easier. But it is possible to override the Cache and go directly to the "gate" that it uses to manage information stored on disk.

import { Store, Key } from "@mateus-pires/file-cache";

const store = new Store();

await store.setup("./dir");

// You can also use Store.start()

await store.insert([
  {
    key: new Key("hello"),
    value: Buffer.from("world"),
  },
]);

// Store has an asyncIterator built in:

for await (const metadata of store) {
}

Util

General utilities.

import { Util } from "@mateus-pires/file-cache";

await Util.awaitEOS();

await Util.waitUntil(() => x == 2);

// And more...

Roadmap

  • [x] Complete README.md
  • [ ] Add tests
  • [x] Make sure it is a stable v1
  • [ ] Add CONTRIBUTING.md
  • [ ] Add CODE_OF_CONDUCT.md