• Stars
    star
    1,585
  • Rank 29,515 (Top 0.6 %)
  • Language
    JavaScript
  • License
    BSD 2-Clause "Sim...
  • Created over 14 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple in-memory cache for nodejs

memory-cache Build Status

A simple in-memory cache for node.js

Installation

npm install memory-cache --save

Usage

var cache = require('memory-cache');

// now just use the cache

cache.put('foo', 'bar');
console.log(cache.get('foo'));

// that wasn't too interesting, here's the good part

cache.put('houdini', 'disappear', 100, function(key, value) {
    console.log(key + ' did ' + value);
}); // Time in ms

console.log('Houdini will now ' + cache.get('houdini'));

setTimeout(function() {
    console.log('Houdini is ' + cache.get('houdini'));
}, 200);


// create new cache instance
var newCache = new cache.Cache();

newCache.put('foo', 'newbaz');

setTimeout(function() {
  console.log('foo in old cache is ' + cache.get('foo'));
  console.log('foo in new cache is ' + newCache.get('foo'));
}, 200);

which should print

bar
Houdini will now disappear
houdini did disappear
Houdini is null
foo in old cache is baz
foo in new cache is newbaz

API

put = function(key, value, time, timeoutCallback)

  • Simply stores a value
  • If time isn't passed in, it is stored forever
  • Will actually remove the value in the specified time in ms (via setTimeout)
  • timeoutCallback is optional function fired after entry has expired with key and value passed (function(key, value) {})
  • Returns the cached value

get = function(key)

  • Retrieves a value for a given key
  • If value isn't cached, returns null

del = function(key)

  • Deletes a key, returns a boolean specifying whether or not the key was deleted

clear = function()

  • Deletes all keys

size = function()

  • Returns the current number of entries in the cache

memsize = function()

  • Returns the number of entries taking up space in the cache
  • Will usually == size() unless a setTimeout removal went wrong

debug = function(bool)

  • Turns on or off debugging

hits = function()

  • Returns the number of cache hits (only monitored in debug mode)

misses = function()

  • Returns the number of cache misses (only monitored in debug mode)

keys = function()

  • Returns all the cache keys

exportJson = function()

  • Returns a JSON string representing all the cache data
  • Any timeoutCallbacks will be ignored

importJson = function(json: string, options: { skipDuplicates: boolean })

  • Merges all the data from a previous call to export into the cache
  • Any existing entries before an import will remain in the cache
  • Any duplicate keys will be overwritten, unless skipDuplicates is true
  • Any entries that would have expired since being exported will expire upon being imported (but their callbacks will not be invoked)
  • Available options:
    • skipDuplicates: If true, any duplicate keys will be ignored when importing them. Defaults to false.
  • Returns the new size of the cache

Cache = function()

  • Cache constructor
  • note that require('cache') would return the default instance of Cache
  • while require('cache').Cache is the actual class

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Send me a pull request.

More Repositories

1

online-yaml-parser

A simple online yaml parser
Python
86
star
2

viencrypt

A simple script for keeping a file encrypted but still editing it. A common use is for managing a file containing passwords.
Shell
20
star
3

nodeselector

A small bookmarklet to help you find Xpaths. You simply hover over any of the dom nodes, and click on the one that you want to see the xpath for. This bookmarklet can also pass the xpath back to a calling webpage.
JavaScript
18
star
4

relmeauth

A sample implementation of relmeauth
Python
15
star
5

stdicon

A standard icon set for hotlinking
Python
13
star
6

signed-request

Examples of parsing Facebook's signed_request
PHP
12
star
7

base64url

Various language implementations of base64_url_decode
JavaScript
8
star
8

fb-hackathon

A tool for our Camp Hackathon submissions
Python
7
star
9

libfreenect

Open Source driver for Microsoft Kinect
C
7
star
10

dotfiles

My . files
Shell
6
star
11

crypto-request-examples

A collection of samples for validating and decrypting signed and encrypted requests from Facebook.
PHP
6
star
12

hadoop_record

A record reader for hadoop CSV files
Python
5
star
13

slidemonkey

An automatic slideshow generator
PHP
4
star
14

webnumbr

Track numbers from anywhere on the web
PHP
3
star
15

hack-hosting

A simple setup to host many users
PHP
3
star
16

blog

ptarjan's webspace on github
2
star
17

hands-on-hadoop-tutorial

My hadoop tutorial for Yahoo! hacku.
Shell
2
star
18

homepage

The source of my homepage
Python
2
star
19

wowduel

A simple app to take two World of Warcraft characters and guess which one would win in a duel. Used as an example for Yahoo! HackU
PHP
1
star
20

og-importer-example

PHP
1
star
21

paulisageek.github.io

1
star