Ejegg has uploaded a new change for review.
https://gerrit.wikimedia.org/r/177390
Change subject: Add memory-cache
......................................................................
Add memory-cache
Change-Id: Ib288a5aefc8949ccd7ae147c3bfad31497f5ce9f
---
A memory-cache/README.md
A memory-cache/index.js
A memory-cache/package.json
A memory-cache/test.js
4 files changed, 268 insertions(+), 0 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/dash/node_modules
refs/changes/90/177390/1
diff --git a/memory-cache/README.md b/memory-cache/README.md
new file mode 100644
index 0000000..7ea2ba2
--- /dev/null
+++ b/memory-cache/README.md
@@ -0,0 +1,83 @@
+# node-cache
+
+A simple in-memory cache for node.js
+
+## Installation
+
+ npm install memory-cache
+
+## 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', 'disapear', 100) // Time in ms
+ console.log('Houdini will now ' + cache.get('houdini'));
+
+ setTimeout(function() {
+ console.log('Houdini is ' + cache.get('houdini'));
+ }, 200);
+
+which should print
+
+ bar
+ Houdini will now disapear
+ Houdini is null
+
+## API
+
+### put = function(key, value, time)
+
+* Simply stores a value.
+* If time isn't passed in, it is stored forever.
+* Will actually remove the value in the specified time (via `setTimeout`)
+
+### get = function(key)
+
+* Retreives a value for a given key
+
+### del = function(key)
+
+* Deletes a key
+
+### 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
+
+### misses = function()
+
+* Returns the number of cache misses.
+
+## TODO
+
+* Namespaces
+* A way of walking the cache for diagnostic purposes
+
+## Note on Patches/Pull Requests
+
+* Fork the project.
+* Make your feature addition or bug fix.
+* Send me a pull request.
diff --git a/memory-cache/index.js b/memory-cache/index.js
new file mode 100644
index 0000000..3409f1c
--- /dev/null
+++ b/memory-cache/index.js
@@ -0,0 +1,82 @@
+var cache = {}
+function now() { return (new Date).getTime(); }
+var debug = false;
+var hitCount = 0;
+var missCount = 0;
+
+exports.put = function(key, value, time, timeoutCallback) {
+ if (debug) console.log('caching: '+key+' = '+value+' (@'+time+')');
+ var oldRecord = cache[key];
+ if (oldRecord) {
+ clearTimeout(oldRecord.timeout);
+ }
+
+ var expire = time + now();
+ var record = {value: value, expire: expire};
+
+ if (!isNaN(expire)) {
+ var timeout = setTimeout(function() {
+ exports.del(key);
+ if (typeof timeoutCallback === 'function') {
+ timeoutCallback(key);
+ }
+ }, time);
+ record.timeout = timeout;
+ }
+
+ cache[key] = record;
+}
+
+exports.del = function(key) {
+ delete cache[key];
+}
+
+exports.clear = function() {
+ cache = {};
+}
+
+exports.get = function(key) {
+ var data = cache[key];
+ if (typeof data != "undefined") {
+ if (isNaN(data.expire) || data.expire >= now()) {
+ if (debug) hitCount++;
+ return data.value;
+ } else {
+ // free some space
+ if (debug) missCount++;
+ exports.del(key);
+ }
+ }
+ return null;
+}
+
+exports.size = function() {
+ var size = 0, key;
+ for (key in cache) {
+ if (cache.hasOwnProperty(key))
+ if (exports.get(key) !== null)
+ size++;
+ }
+ return size;
+}
+
+exports.memsize = function() {
+ var size = 0, key;
+ for (key in cache) {
+ if (cache.hasOwnProperty(key))
+ size++;
+ }
+ return size;
+}
+
+exports.debug = function(bool) {
+ debug = bool;
+}
+
+exports.hits = function() {
+ return hitCount;
+}
+
+exports.misses = function() {
+ return missCount;
+}
diff --git a/memory-cache/package.json b/memory-cache/package.json
new file mode 100644
index 0000000..eecdc7f
--- /dev/null
+++ b/memory-cache/package.json
@@ -0,0 +1,46 @@
+{
+ "name": "memory-cache",
+ "description": "A simple in-memory cache. put() get() and delete()",
+ "author": {
+ "name": "Paul Tarjan",
+ "email": "[email protected]"
+ },
+ "url": "https://github.com/ptarjan/node-cache",
+ "keywords": [
+ "cache",
+ "ram",
+ "simple",
+ "storage"
+ ],
+ "main": "./index.js",
+ "version": "0.0.5",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/ptarjan/node-cache.git"
+ },
+ "readme": "# node-cache\n\nA simple in-memory cache for node.js\n\n##
Installation\n\n npm install memory-cache\n\n## Usage\n\n var cache =
require('memory-cache');\n\n // now just use the cache\n\n
cache.put('foo', 'bar');\n console.log(cache.get('foo'))\n\n // that
wasn't too interesting, here's the good part\n\n cache.put('houdini',
'disapear', 100) // Time in ms\n console.log('Houdini will now ' +
cache.get('houdini'));\n\n setTimeout(function() {\n
console.log('Houdini is ' + cache.get('houdini'));\n }, 200);\n\nwhich
should print\n\n bar\n Houdini will now disapear\n Houdini is
null\n\n## API\n\n### put = function(key, value, time)\n\n* Simply stores a
value. \n* If time isn't passed in, it is stored forever.\n* Will actually
remove the value in the specified time (via `setTimeout`)\n\n### get =
function(key)\n\n* Retreives a value for a given key\n\n### del =
function(key)\n\n* Deletes a key\n\n### clear = function()\n\n* Deletes all
keys\n\n### size = function()\n\n* Returns the current number of entries in the
cache\n\n### memsize = function()\n\n* Returns the number of entries taking up
space in the cache\n* Will usually `== size()` unless a `setTimeout` removal
went wrong\n\n### debug = function(bool)\n\n* Turns on or off debugging\n\n###
hits = function()\n\n* Returns the number of cache hits\n\n### misses =
function()\n\n* Returns the number of cache misses.\n\n## TODO\n\n*
Namespaces\n* A way of walking the cache for diagnostic purposes\n\n## Note on
Patches/Pull Requests\n \n* Fork the project.\n* Make your feature addition or
bug fix.\n* Send me a pull request.\n",
+ "readmeFilename": "README.md",
+ "_id": "[email protected]",
+ "dist": {
+ "shasum": "dbf99a56d7362c43eccaf39f0ba6f97f31a06786",
+ "tarball":
"http://registry.npmjs.org/memory-cache/-/memory-cache-0.0.5.tgz"
+ },
+ "_from": "memory-cache@",
+ "_npmVersion": "1.2.2",
+ "_npmUser": {
+ "name": "ptarjan",
+ "email": "[email protected]"
+ },
+ "maintainers": [
+ {
+ "name": "ptarjan",
+ "email": "[email protected]"
+ }
+ ],
+ "directories": {},
+ "_shasum": "dbf99a56d7362c43eccaf39f0ba6f97f31a06786",
+ "_resolved":
"https://registry.npmjs.org/memory-cache/-/memory-cache-0.0.5.tgz",
+ "bugs": {
+ "url": "https://github.com/ptarjan/node-cache/issues"
+ }
+}
diff --git a/memory-cache/test.js b/memory-cache/test.js
new file mode 100644
index 0000000..47ee817
--- /dev/null
+++ b/memory-cache/test.js
@@ -0,0 +1,57 @@
+var cache = require('./index')
+;
+
+cache.debug(false);
+
+cache.put('a', true);
+console.log('true == '+cache.get('a'));
+cache.clear();
+console.log('null == '+cache.get('a'));
+
+console.log('null == '+cache.get('a'));
+console.log('0 == '+cache.size());
+
+cache.put('a', 'b', 3000);
+console.log('1 == '+cache.size());
+
+console.log('b == '+cache.get('a'));
+
+var complicated = ['a',{'b':'c','d':['e',3]},'@'];
+cache.put(complicated, true);
+console.log('true == '+cache.get(complicated));
+cache.del(complicated);
+console.log('null == '+cache.get(complicated));
+
+console.log('1 == '+cache.size());
+cache.put(0, 0);
+console.log('2 == '+cache.size());
+cache.del(0);
+
+cache.put('c', 'd', 1000, function() {
+ console.log('callback was called');
+});
+
+setTimeout(function() {
+ console.log('b == '+cache.get('a'));
+}, 2000);
+
+setTimeout(function() {
+ console.log('null == '+cache.get('a'));
+ console.log('0 == '+cache.size());
+}, 4000);
+
+setTimeout(function() {
+ console.log('Cache hits: ' + cache.hits());
+ console.log('Cache misses: ' + cache.misses());
+}, 5000);
+
+cache.put('timeout', 'timeout', 2000);
+
+setTimeout(function() {
+ console.log('timeout == '+cache.get('timeout'));
+ cache.put('timeout', 'timeout-re', 2000); // Cancel timeout on NEW put
+}, 1000);
+
+setTimeout(function() {
+ console.log('timeout-re == '+cache.get('timeout'));
+}, 3000);
--
To view, visit https://gerrit.wikimedia.org/r/177390
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib288a5aefc8949ccd7ae147c3bfad31497f5ce9f
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/dash/node_modules
Gerrit-Branch: master
Gerrit-Owner: Ejegg <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits