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 🙏

© 2025 – Pkg Stats / Ryan Hefner

key-value-lru-file-cache

v1.4.1

Published

A key-value LRU file cache

Downloads

157

Readme

key-value-lru-file-cache

npm package Build Status Downloads Issues Code Coverage Commitizen Friendly Semantic Release

Install

npm install key-value-lru-file-cache

or

yarn add key-value-lru-file-cache

Example

import { getCache } from "key-value-lru-file-cache";
import MMKV from "react-native-mmkv";
import ReactNativeBlobUtil from "react-native-blob-util";

// Example key parameters type (or just use a primitive)
interface ImageCacheKey {
  remoteImgPath: string;
  width: number;
  height: number;
}

// Create the cache instance
const imageCache = getCache({
  prefix: "IMG_RESIZED",
  evictionMillis: 7 * 24 * 60 * 60 * 1000, // 1 week
  maxEntries: 1000,
  maxCacheSize: 300 * 1024 * 1024, // 300MB

  // Key/value storage using MMKV
  getValue: async key => MMKV.getString(key) ?? null,
  setValue: async (key, value) => {
    try {
      MMKV.set(key, value);
      return true;
    } catch {
      return false;
    }
  },
  delete: async key => {
    try {
      MMKV.delete(key);
      return true;
    } catch {
      return false;
    }
  },
  getAllKeys: async () => MMKV.getAllKeys(),
  getKeyFor: async params => {
    const encodedUri = encodeURIComponent(params.remoteImgPath);
    return `IMG_RESIZED:${encodedUri}-${params.width}-${params.height}`;
  },

  // File system operations using react-native-blob-util
  fileExists: path => ReactNativeBlobUtil.fs.exists(path),
  fileUnlink: async path => {
    try {
      await ReactNativeBlobUtil.fs.unlink(path);
      return true;
    } catch {
      return false;
    }
  },
  fileSize: async path => {
    const stat = await ReactNativeBlobUtil.fs.stat(path);
    return stat.size;
  },
});

// Using the cache
async function exampleUsage() {
  // Put a file in cache
  await imageCache.put(
    {
      remoteImgPath: "https://example.com/image.jpg",
      width: 200,
      height: 200,
    },
    "/local/path/to/resized-image.jpg"
  );

  // Get a file from cache
  const filePath = await imageCache.get({
    remoteImgPath: "https://example.com/image.jpg",
    width: 200,
    height: 200,
  });

  if (filePath) {
    console.log("Found cached file:", filePath);
  } else {
    console.log("File not in cache or expired");
  }

  // Clean expired entries
  await imageCache.cleanExpiredEntries();
}

API

KeyValueCache<TKeyParams>

A generic, adapter‑based key‑value + file LRU cache with eviction based on:

  • Max age (evictionMillis)
  • Max entries (maxEntries)
  • Max total disk size (maxCacheSize)

Constructor

new KeyValueCache<TKeyParams>(adapter: KeyValueCacheAdapter<TKeyParams>)

Parameters:

| Parameter | Type | Description | | --------- | ---------------------------------- | -------------------------------------------------------------------------------------- | | adapter | KeyValueCacheAdapter<TKeyParams> | Implementation of the key‑value and file system adapter methods required by the cache. |

Adapter Interface: KeyValueCacheAdapter<TKeyParams>

| Property / Method | Type | Description | | ---------------------------- | -------------------------------------------------- | -------------------------------------------------- | | prefix | string | Prefix for all keys in the cache. | | evictionMillis | number | Max age (in milliseconds) before an entry expires. | | maxEntries | number | Maximum number of entries allowed in the cache. | | maxCacheSize | number | Maximum total cache size in bytes. | | getValueForKey(key) | (key: string) => Promise<string \| null> | Get value for a given key. | | setValueForKey(key, value) | (key: string, value: string) => Promise<boolean> | Store value for a given key. | | deleteKeyValue(key) | (key: string) => Promise<boolean> | Delete a key/value pair. | | getAllKeys() | () => Promise<string[]> | Get all keys from the store. | | getKeyFor(params) | (params: TKeyParams) => Promise<string \| null> | Build a unique key string from parameters. | | fileExists(path) | (path: string) => Promise<boolean> | Check if a file exists at the given path. | | fileUnlink(path) | (path: string) => Promise<boolean> | Delete a file at the given path. | | fileSize(path) | (path: string) => Promise<number> | Get file size in bytes. |

Instance Methods

| Method | Returns | Description | | -------------------------- | ------------------------- | ------------------------------------------------------------------------------------------- | | get(params) | Promise<string \| null> | Get file path for given params if entry exists and is valid. Updates lastAccessed on hit. | | put(params, filePath) | Promise<boolean> | Add/update an entry in the cache. Triggers eviction if limits exceeded. | | delete(params) | Promise<boolean> | Remove an entry and delete its file if present. | | cleanExpiredEntries() | Promise<boolean> | Remove all entries older than evictionMillis. Returns true if any were removed. | | getCurrentEntriesCount() | Promise<number> | Get current number of cached entries. | | getCurrentDiskSize() | Promise<number> | Get current total cache size in bytes. |