Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated version that works with npm/browserify, and fixes unnecessary purging #17

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
coverage.html
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: node_js
node_js:
- "0.12"
- "0.11"
- "0.10"
- "iojs"

script:
- npm run test
- npm run test-coveralls
173 changes: 99 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,81 +1,106 @@
JavaScript LRU Cache
====================
cachai
======

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.
> LRU Cache for Node.js

How It Works
------------
[![Build Status](https://secure.travis-ci.org/rstuven/node-cachai.png?branch=master)](http://travis-ci.org/rstuven/node-cachai)
[![Coverage Status](https://coveralls.io/repos/rstuven/node-cachai/badge.svg)](https://coveralls.io/r/rstuven/node-cachai)
[![dependencies Status](https://david-dm.org/rstuven/node-cachai.svg)](https://david-dm.org/rstuven/node-cachai#info=dependencies)
[![devDependencies Status](https://david-dm.org/rstuven/node-cachai/dev-status.svg)](https://david-dm.org/rstuven/node-cachai#info=devDependencies)

// 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); }
});
This is a fork of Monsur Hossain's jscache for browsers, which 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 on purge.

// 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
Install
-------
* 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.

npm install --save cachai


Usage
-----

First, create a new cache object.
The constructor accepts an optional integer
parameter which places a limit on how many
items the cache holds.
Example:
``` javascript
var Cache = require('cachai');

var cache = new Cache();
```

### Methods

`setItem` adds an item to the cache. Arguments:
- `key`: key to refer to the object
- `value`: object to cache
- `options`: optional parameters described below

Options available are:

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

- `onPurge`:
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.

Example:
``` javascript
cache.setItem("A", "1", {
expirationAbsolute: null,
expirationSliding: 60,
priority: Cache.Priority.HIGH,
onPurge: function(k, v) { console.log('removed', k, v); }
});
```

`getItem` retrieves an item from the cache
takes one parameter, the key to retrieve
returns the cached item.
Example:
``` javascript
var item = cache.getItem("A");
```

`removeItem` removes and returns an item from the cache.
If the item doesn't exist it returns null.
Example:
``` javascript
var removed = cache.removeItem("A");
```

`size` returns the number of items in the cache.
Example:
``` javascript
var size = cache.size();
```

`stats` returns stats about the cache, like `{"hits": 1, "misses": 4}`.
Example:
``` javascript
console.dir(cache.stats());
```

`clear` removes all items from the cache.
Example:
``` javascript
cache.clear();
```
Loading