Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make the cache plugin use MRU instead of LRU.

  • Loading branch information...
commit e02af6b190e28e8b9cfb3fc9d685d1bb644cc128 1 parent 2a4c71a
@creationix authored
Showing with 41 additions and 22 deletions.
  1. +34 −20 lib/nstore/cache.js
  2. +7 −2 test/findTest.js
View
54 lib/nstore/cache.js
@@ -1,35 +1,49 @@
-// This plugin adds an auto-expiring ramCache to nStore
-module.exports = function CachePlugin() {
- var values = [],
+// This plugin implements an MRU cache to speed up reads
+module.exports = function CachePlugin(maxSize) {
+ var values = {},
items = 0;
-
+
+ function push(key, doc) {
+ if (!values.hasOwnProperty(key)) {
+ if (items >= maxSize) return;
+ items++;
+ }
+ values[key] = doc;
+ }
+
var self = {
- save: function save(key, value, callback) {
- if (!values.hasOwnProperty(key)) {
- items++;
- if (items > maxSize) {
- delete values[Object.keys(values)[0]];
- items--;
- }
- }
- values[key] = value;
- return dataSource.save(key, value, callback);
+ save: function save(key, doc, callback) {
+ // Go ahead and generate the auto key so we know what to cache
+ if (!key) key = this.getKey();
+ push(key, doc);
+ // Call super
+ self.__proto__.save.call(this, key, doc, callback);
},
get: function get(key, callback) {
if (values.hasOwnProperty(key)) {
+ var value = values[key];
process.nextTick(function () {
- callback(null, values[key], key);
+ callback(null, value, key);
});
return;
}
- dataSource.get(key, callback);
+
+ // Call super
+ self.__proto__.get.call(this, key, function (err, doc, key) {
+ if (err) return callback(err);
+ push(key, doc);
+ callback(err, doc, key);
+ });
},
remove: function remove(key, callback) {
- delete values[key];
- items--;
- return dataSource.remove(key, callback);
+ if (values.hasOwnProperty(key)) {
+ delete values[key];
+ items--;
+ }
+ // Call super
+ self.__proto__.remove.call(this, key, callback);
}
};
return self;
-}
+};
View
9 test/findTest.js
@@ -1,7 +1,8 @@
require('./helper');
// Enable the cache plugin
-nStore = nStore.extend(require('nstore/cache')(2000));
+// (that only caches some of the values to test the cache miss)
+nStore = nStore.extend(require('nstore/cache')(150));
// Enable the query plugin
nStore = nStore.extend(require('nstore/query')());
@@ -24,7 +25,7 @@ var queries = [
[undefined, 200],
[[], 200]
];
-
+var start;
expect("load");
var store = nStore.new('fixtures/new.db', function () {
@@ -35,6 +36,7 @@ var store = nStore.new('fixtures/new.db', function () {
Step(
function () {
fulfill("one");
+ start = Date.now();
var group1 = this.group();
var group2 = this.group();
for (var i = 0; i < 100; i++) {
@@ -44,6 +46,8 @@ var store = nStore.new('fixtures/new.db', function () {
},
function (err, keys1, keys2) {
fulfill("two");
+ console.log("Insert %s ms", Date.now() - start);
+ start = Date.now();
expect("all");
store.all(function (err, result) {
@@ -61,6 +65,7 @@ var store = nStore.new('fixtures/new.db', function () {
},
function (err, results) {
fulfill("three");
+ console.log("Query %s ms", Date.now() - start);
if (err) throw err;
assert.equal(results.length, queries.length, "All queries should come back");
expect("result");
Please sign in to comment.
Something went wrong with that request. Please try again.