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

js-cache

v1.0.3

Published

Caching library supporting timeouts, events and external data sources

Downloads

21,580

Readme

Caching library for JavaScript and Node.js

Caching library with support for timeouts, events and external data sources.

Overview

Usage in Node.js

var cache = require('js-cache');
cache.set('lorem', 'ipsum', 60000);
console.log(cache.get('lorem'));

var myCache = new cache();
myCache.set('lorem', 'dolor', 60000);
console.log(myCache.get('lorem'));

Usage in browsers

<script src="bundle/cache.js"></script> 
<script>
    cache.set('lorem', 'ipsum', 60000);
    console.log(cache.get('lorem'));
</script>

Instance

It is possible to call the cache object directly:

cache.set('lorem', 'ipsum');
console.log(cache.get('lorem'));

or use it as a constructor to create a separate storage:

var c2 = new cache();
c2.set('lorem', 'dolor');
console.log(c2.get('lorem'));

API

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

Cache data or update and existing record.

key Unique key identifying the cache
value Cached value
ttl Time to live in milliseconds (optional)

cache.get(key, [callback])

Get cached value. Returns cached value (or undefined) if no callback was provided. Always returns undefined if callback argument is present.

key Key identifying the cache
callback Return value in callback if record exists in memory or on external resource (optional)

cache.del(key)

Delete cached data. Returns true if the record existed, false if not.

key Key identifying the cache

cache.clear()

Clear all cached data. Returns number of cleared records.

cache.size()

Returns number of cached records.

cache.debug()

Returns internal object with cached records.

cache.keys()

Returns list of cached record keys.

External data source

It is possible to forward api requests to external handlers. They can be used for logging, storing in persistent database etc. The cache library will function as a temporary, in-memory layer.

var c = new cache({
    set: setHandler,
    get: getHandler,
    del: delHandler,
    clear: clearHandler
});

All handlers are optional.

setHandler is called with key, value, ttl on cache.set().

getHandler is called with key, callback on cache.get() when a callback is provided and the key is not present in the cache. The getHandler is required to execute callback.

delHandler is called with key on cache.del(). It is not called when a cached record times out.

clearHandler is called without arguments on cache.clear().

Events

The cache library uses the Backbone event framework. Please refer to their documentation for a detailed overview.

set , set:key

Emitted when setting a value to a new record.

cache.on('set', function(key, value, ttl){});
cache.on('set:lorem', function(value, ttl){});
cache.set('lorem', 'ipsum', 123);

update, update:key

Emitted when updating the value or ttl of a known record.

cache.set('lorem', 'ipsum');
cache.on('update', function(key, value, ttl){});
cache.on('update:lorem', function(value, ttl){});
cache.set('lorem', 'ipsum', 123);

del, del:key

Emitted when deleting a record or when a cached record times out.

cache.set('lorem', 'ipsum');
cache.on('del', function(key){});
cache.on('del:lorem', function(){});
cache.del('lorem');

clear

Emitted when the cache gets cleared.

cache.on('clear', function(size){});
cache.clear();

Developing

The library is published to NPM and can be installed with the following command:

$ npm install js-cache

Building

The scripts have to be bundled in order to use them in a browser. One of the tools you can use is browserify.

$ sudo npm install browserify -g
$ make browserify

This will build the script in bundle/cache.js

Testing

Navigate to this module's repository and make sure you have the development modules installed:

$ npm install

Run the tests:

$ npm test