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

fs-cache-fast

v1.0.3

Published

Cache stored on the file system

Downloads

437

Readme

fs-cache-fast

fs-cache-fast is a small, no dependancy, both promise and sync based file system cache storage. This package is designed to be a smaller, lighter, drop-in replacement for file-system-cache.

Installation

Install with npm:

$ npm install --save fs-cache-fast

Getting started

The api is extremely simple:

import Cache from 'fs-cache-fast'

let cache = new Cache()

cache.setSync('my-key', { value: 'here' })
let item = cache.getSync('my-key', { fallback: 'here' })

await cache.set('another-key', 'Hi there')
let result = await cache.get('another-key')

Api

new Cache(options)

Create a new cache with the specified directory (if directory is skipped, it randomly generates one in fs.tmp on each run).

Possible values in options:

{
  prefix: 'myprefix', // Add a prefix to every cached filename that is generated
  ns: 'myprefix', // Alternative name for prefix, for API compatibility with file-system-cache
  hash_alg: 'sha256', // Use the specified hashing algorithm that is used to generate the filename
  cache_dir: '/tmp/MY_CACHE', // The directory where all the cache gets stored, gets auto-created if not exist.
  ttl: 60, // Expiration in seconds for each cache item.
}

The default options are as follow:

{
  prefix: '-',
  hash_alg: 'md5',
  cache_dir: path.join(os.tmpdir(), /* random id */),
  ttl: 0,
}

cache.get(key, fallback = null)

Promise get the cache value that exists with item key and if it doesn't exist or has expired, returns the fallback value instead.

let myCache = await cache.get('mykey', null)

cache.getSync(key, fallback = null)

Immediately get the cache value that exists with item key and if it doesn't exist or has expired, returns the fallback value instead.

let myCache = cache.getSync('mykey', null)

cache.set(key, content, ttl | { ttl: number } = null)

Promise store the content as cache with the specified key (with optional overwriting default ttl set on the cache constructor).

await cache.set('mykey', { hello: 'world' })

cache.setSync(key, content, ttl | { ttl: number } = null)

Immediately store the content as cache with the specified key (with optional overwriting default ttl set on the cache constructor).

cache.setSync('mykey', { hello: 'world' }, 5 * 60) // Expire this after 5 minutes

cache.setMany(items, ttl | { ttl: number } = null)

cache.save(items, ttl | { ttl: number } = null)

Promise store multiple items all at once while optionally overwriting the ttl for these entries. Items take form of an array of json objects with the following signature: { key, content }

Note, for backwards compatibility with file-system-cache you can also use the property value instead of content.

await cache.setMany([
  { key: 'one', content: 'Store this' },
  { key: 'two', content: { a: 'and also this' } },
])

await cache.save([
  { key: 'one', value: 'Store this' },
  { key: 'two', value: { a: 'and also this' } },
])

cache.setManySync(items, ttl | { ttl: number } = null)

cache.saveSync(items, ttl | { ttl: number } = null)

Immediately store multiple items all at once while optionally overwriting the ttl for these entries. Items take form of an array of json objects with the following signature: { key, content }

Note, for backwards compatibility with file-system-cache you can also use the property value instead of content.

Note, there's an alternative name for it called .saveSync(...) for retaining similar naming schema as file-system-cache (it does not provide this functionality).

cache.setManySync([
  { key: 'one', content: 'Store this' },
  { key: 'two', content: { a: 'and also this' } },
], 10 * 60) // Expire all of these after 10 minutes.

cache.saveSync([
  { key: 'one', value: 'Store this' },
  { key: 'two', value: { a: 'and also this' } },
], 10 * 60) // Expire all of these after 10 minutes.

cache.remove(key)

Promise remove a cache with the specified key.

await cache.remove('mykey')

cache.removeSync(key)

Immediately remove a cache with the specified key.

cache.removeSync('mykey')

cache.clear()

Promise remove all items in the cache directory that match the specified prefix or ns if you will.

await cache.clear()

cache.clearSync()

Immediately remove all items in the cache directory that match the specified prefix or ns if you will.

cache.clearSync()

cache.getAll()

Promise return all items currently residing in the cache that have valid ttl. This returns an array of objects, each one with with following signature: { key, content, ttl }

let items = await cache.getAll()
// items[0] = { key: 'one', content: 'Store this' }
// items[1] = { key: 'two', content: { a: 'and also this' } }

cache.load()

Promise return all items currently residing in the cache that have valid ttl. This is an API compatible version with file-system-cache and returns the results slightly different to maintain compatibility. Returns an object with key files that has an array of items with this signature: { path, value, key }

let items = await cache.load()
// items.files[0] = { path: '...', value: 'Store this', key: 'one' }
// items.files[1] = { path: '...', value: { a: 'and also this' }, key: 'two' }