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

@haxtra/memory-cache

v0.9.0

Published

Memory based cache library, with two-way expiration, tags and events

Downloads

2

Readme

memory-cache

Memory based cache library, with two-way expiration, tags and events.

Usage

Note: Any function that accepts an array of key/tag names, will also accept a space delimited string. This means that key/tag names should not use spaces. If that's a requirement, then use arrays only.

Install

npm i @haxtra/memory-cache

Create

// import
const Cache = require('@haxtra/memory-cache')

// create instance
const cache = new Cache({ // showing defaults, all optional
    state: {},      // initial state, an object from .export function
    gc: 600,        // garbage collection interval in seconds
    logger: null,   // logging function, ie. `console.log`
})

Set / Update

// basic set
cache.set(key, item)

// set with options, all optional
cache.set(key, item, {
    expireIn: false,  // (int) time to live in seconds
    expireAt: false,  // (int) unix timestamp when item becomes stale
    tags: false       // (arr) item's tags, string or array
})

Get

// basic get
cache.get(key) // returns null on miss

// get with options
cache.get(key, {
    default: null,    // value to return on miss
    maxAge: false,    // max data age in seconds
                      // - maxAge overrides `expire*` option from .set
    force: false,     // return cache item whether expired or not
})

// get internal container for given key, without expiry check
// includes tags, creation and expiration times, if any
cache.getMeta(key)

Delete

// delete given keys, returns number of removed items
cache.delete(str|arr)

// delete everything from the cache
cache.clear()

Tags

Note: .getTagged does not check for expiration. If you require fresh items only, run .deleteExpired first.

// get items tagged with...
cache.getTagged(str|arr)

// get tagged with options
cache.getTagged(str|arr, {
    all: false,       // item must have all specified tags, otherwise any
    meta: false,      // return internal container, inc tags, etc
})

Delete tagged

// remove items tagged with ANY tag specified
cache.deleteTagged(str|arr)

// remove items having ALL specified tags
cache.deleteTagged(str|arr, {all:true})

Events

Events are conceptual, they are just tags. Tags should describe the data, while events should describe action, upon which the item is invalidated. Tags and events can be set using tags option on .set, and the convention is to prefix event names with on. ie. on.update.

A semantic function is provided to be used with events, and it's equal to .deleteTagged.

// trigger an event that removes items with ANY specified events and tags
cache.trigger(str|arr)

// trigger an event that removes items with ALL specified events and tags
cache.trigger(str|arr, {all:true})

Info

// get array of keys of currently cached items
cache.keys(validOnly?) // bool

// get count of cached items
cache.count(validOnly?)

// get tag breakdown of cached items
// returns key-value object of tags and their counts
cache.tags(validOnly?)

Garbage Collection

Manual

// remove all expired items, returns the number of items removed
cache.deleteExpired()

Auto

Runs periodically to clear expired items. Must be started manually.

// start gc
cache.gcStart()

// stop gc
cache.gcStop()

// get status
cache.gcStatus()

Instances

// create new cache instance, accepts the same set of options as the main function
const newCache = cache.spawn(opts?)

// destroy the cache
cache.dispose()

Export / Import

Cache state can be exported and restored later on.

// export as json, including items that will expire at some point (but not expired already)
cache.export()

// export as json, but only persistent items - without expiration time
cache.export({persistent:true})

// export as json, including already expired (straight copy)
cache.export({expired:true})

// export as object, above also applies
cache.export({object:true})

Import

// either json or object
cache.import(json|object)

Logger

memory-cache will log its events if logger is attached.

// at initialization
const cache = new Cache({logger:console.log})

// at runtime
cache.logger = console.log

Haxxor

// internal state is available at:
cache.$

License

MIT