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

@visallo/jscache-lru

v1.0.0

Published

JavaScript LRU Cache ====================

Downloads

4

Readme

JavaScript LRU Cache

Just a simple LRU cache written in javascript. It is loosely based on ASP.NET's Cache, and includes many caching options such as absolute expiration, sliding expiration, cache priority, and a callback function. It can be used to cache data locally in the user's browser, saving a server roundtrip in AJAX heavy applications.

How It Works

	// Create a new cache item
	// The constructor accepts an optional integer
	// parameter which places a limit on how many
	// items the cache holds
	var cache = new Cache();

	// add an item to the cache
	// parameters: key - the key to refer to the object
	//             value - the object to cache
	//             options - an optional parameter described below
	// the last parameter accepts an object which controls various caching options:
	//      expirationAbsolute: the datetime when the item should expire
	//      expirationSliding: an integer representing the seconds since
	//                         the last cache access after which the item
	//                         should expire
	//      priority: How important it is to leave this item in the cache.
	//                You can use the values Cache.Priority.LOW, .NORMAL, or
	//                .HIGH, or you can just use an integer.  Note that
	//                placing a priority on an item does not guarantee
	//                it will remain in cache.  It can still be purged if
	//                an expiration is hit, or if the cache is full.
	//      callback: A function that gets called when the item is purged
	//                from cache.  The key and value of the removed item
	//                are passed as parameters to the callback function.
	cache.setItem("A", "1", {expirationAbsolute: null,
	                         expirationSliding: 60,
	                         priority: Cache.Priority.HIGH,
	                         callback: function(k, v) { alert('removed ' + k); }
	                        });

	// retrieve an item from the cache
	// takes one parameter, the key to retreive
	// returns the cached item
	cache.getItem("A");

	// Remove and return an item from the cache.
	// If the item doesn't exist it returns null.
	cache.removeItem("A");
	
	// Removes items from the cache which pass the provided test.
	// If the test function returns true, the item will be removed.
	// E.g., Remove keys which start with 'RemoveMe'
	cache.removeWhere(function(k, v) { return /^RemoveMe/.test(k); });

	// Returns the number of items in the cache.
	cache.size();

	// Return stats about the cache, like {"hits": 1, "misses": 4}
	cache.stats();

	// clears all items from the cache
	cache.clear();

LocalStorage Persistance

You can have the cache persist its values to localStorage on browsers that support it. To do this simply create the cache with a different storage backend like:

var cache = new Cache(-1, false, new Cache.LocalStorageCacheStorage());

All values have to be JSON stringifiable, which means the callback option to setItem won't work.

If you want to have multiple independent caches, pass in a namespace argument, like:

var cache = new Cache(-1, false, new Cache.LocalStorageCacheStorage('myNameSpace'));

If -1 is used for the cache size, the cache will be limited to the size of localStorage, which is currently 5MB on Chrome/Safari.

History

  • 4/16/2013: Thanks to Nick Young for AMD module support and the removeWhere() method.
  • 11/29/2011: Thanks to Andrew Carman for tests, pluggable backends, localStorage persistance, and bug fixes.
  • 1/8/2011: Migrated project to GitHub.
  • 1/20/2010: Thanks to Andrej Arn for some syntax updates.
  • 5/30/2008: First version.