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

localstash

v0.2.0

Published

A fast, minimalistic in-memory key/value cache

Downloads

19

Readme

LocalStash

LocalStash is a fast, minimalistic in-memory key/value cache, with support for expiration. In essence, it's quite similar to other key/value stores like memcached, except that LocalStash only keeps things in the current process's memory, and not in some external service. LocalStash has no external dependencies.

Why would I use this?

Sometimes you want to store some calculated, or externally retrieved values, because you know you may need them again very soon. Nonetheless, you don't want to keep them around for too long. That's where the ability to expire information comes in handy. LocalStash has its own garbage collection mechanism for expiring old data. The main design considerations are a small performance overhead, and a fast garbage collection mechanism.

API

LocalStash exposes the following APIs:

new LocalStash([cycleInterval], [options])

cycleInterval is the garbage collection cycle interval, in seconds. If you tend to store a lot of data, with short TTL values, it is recommended to use a relatively low interval. If data generally has a high TTL, you may want to raise the interval.

options is an object for passing options to the cache. The following values are supported:

maxKeys (integer, default: null). Can be set to limit the total amount of values that may be stored in the cache. If this value gets exceeded, the values that will expire soonest will be made to expire prematurely.

aggressiveExpiration (boolean, default: false). If set to true, even if a get() call yields a value, and that value is expired (just not yet garbage collected), it will not be returned.

cache.flush()

Resets the entire cache to zero.

cache.diagnostics()

Returns an object containing information about the state of the cache.

cache.add(key, value, [ttl], [touchIfExists])

Adds a value to the cache, optionally expiring after ttl seconds. If a value with that key did not yet exist, it will be created and the new value will be returned. If a value did exist, it will not be overwritten, and instead that existing value will be returned. Also, if touchIfExists is true, the existing value will get ttl as a new time-to-live.

cache.set(key, value, [ttl])

Adds a value to the cache, optionally expiring after ttl seconds. If a value with that key already exists, it will be overwritten. For chainability, set() will return the value.

cache.touch(key, ttl)

Resets the value's expiration time to now + ttl seconds. The value that key holds will be returned.

cache.del(key)

Deletes the value with key key from the cache. If the value existed, this deleted value will be returned. If it did not exist, undefined will be returned.

cache.get(key, newTTL)

Returns the value stored by key key. If it does not exist, undefined will be returned. If newTTL is defined, the value will be "touched" with the new TTL.

cache.getExpirationTime(key)

Yields the time (unix timestamp in seconds) at which the value for key key will expire. if the value will never expire, null is returned. If the value does not exist, undefined is returned.

Example

var LocalStash = require('localstash').LocalStash;

// instantiate

var options = {};

var cache = new LocalStash(60, options);

// set key "hello" to value "world" and expire after 30 seconds.

cache.set('hello', 'world', 30);

// adding a key called "hello" will now fail and return the previous value "world".

cache.add('hello', 'foo');

// print "hello world"

console.log('hello', cache.get('hello'));

// extend expiration to 120 seconds after now

cache.touch('hello', 120);

// cleanup prematurely, because we can

cache.del('hello');

License

LocalStash uses the MIT License.