GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - dkstyle/jscache: Just a simple JavaScript LRU cache.

BranchesTags
Open more actions menu
 
 

Latest commit

 

History

26 Commits
26 Commits

Folders and files

NameName
Last commit message
Last commit date
README.md
README.md
 
 
cache.js
cache.js
 
 
sample.html
sample.html
 
 
test.js
test.js
 
 

Repository files navigation

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 CachePriority.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: CachePriority.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");

	// 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

  • 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.

About

Just a simple JavaScript LRU cache.

Resources

Readme
Activity

Stars

1 star

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.