Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
taoyuan
committed
Aug 27, 2013
1 parent
de435bb
commit ef6d3c0
Showing
19 changed files
with
1,227 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
lib-cov | ||
*.seed | ||
*.log | ||
*.csv | ||
*.dat | ||
*.out | ||
*.pid | ||
*.gz | ||
|
||
pids | ||
logs | ||
results | ||
|
||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ignore these files | ||
*.swp | ||
*~ | ||
*.lock | ||
*.DS_Store | ||
node_modules | ||
npm-debug.log | ||
*.out | ||
*.o | ||
*.tmp | ||
.git | ||
.idea | ||
jasmine*.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2013 Tao Yuan | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,85 @@ | ||
kvs | ||
=== | ||
============ | ||
|
||
Simple key-value store facade for node. | ||
|
||
## Installation | ||
npm install kvs | ||
|
||
## Methods | ||
set(key, val, cb) | ||
get(key, cb) | ||
del(key, cb) | ||
getset(key, value, cb) | ||
getdel(key, cb) | ||
keys(cb) | ||
clear(cb) | ||
|
||
## Usage Examples | ||
|
||
### Single Cache | ||
```js | ||
var kvs = require('kvs'); | ||
|
||
var redisStore = kvs.store('redis', { db: 1 }); | ||
var redisBucket = redisStore.crateBucket({ ttl: 100/*seconds*/ }); | ||
|
||
var memoryStore = kvs.store('memory'); | ||
var memoryBucket = memoryStore.crateBucket({ max: 100, ttl: 10/*seconds*/ }); | ||
|
||
|
||
redisBucket.set('foo', 'bar', function(err) { | ||
if (err) { throw err; } | ||
|
||
redisBucket.get('foo', function(err, result) { | ||
console.log(result); | ||
// >> 'bar' | ||
redisBucket.del('foo', function(err) {}); | ||
}); | ||
}); | ||
|
||
function getUser(id, cb) { | ||
setTimeout(function () { | ||
console.log("Returning user from slow database."); | ||
cb(null, {id: id, name: 'Bob'}); | ||
}, 100); | ||
} | ||
|
||
var user_id = 123; | ||
var key = '#' + user_id; // for test if key is not the data (user_id) to load. | ||
|
||
// Using namespace "user" | ||
var redisLoadBucket = redisStore.crateBucket('user', { | ||
ttl: 100, /*seconds*/ | ||
|
||
// method to load a thing if it's not in the bucket. | ||
load: function (user_id, cb) { | ||
// this method will only be called if it's not already in bucket, and will | ||
// bucket the result in the bucket store. | ||
getUser(user_id, cb); | ||
} | ||
}); | ||
|
||
// `user_id` is used to load, or use `key` without `user_id` parameter. | ||
redisLoadBucket.get(key, user_id, function (err, user) { | ||
console.log(user); | ||
|
||
// Second time fetches user from redisLoadBucket | ||
redisLoadBucket.get(key, user_id, function (err, user) { | ||
console.log(user); | ||
}); | ||
}); | ||
|
||
// Outputs: | ||
// Returning user from slow database. | ||
// { id: 123, name: 'Bob' } | ||
// { id: 123, name: 'Bob' } | ||
``` | ||
|
||
|
||
## Tests | ||
npm test | ||
|
||
## License | ||
|
||
`kvs` is licensed under the MIT license. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
var kvs = require('kvs'); | ||
|
||
var redisStore = kvs.store('redis', { db: 1 }); | ||
var redisBucket = redisStore.crateBucket({ ttl: 100/*seconds*/ }); | ||
|
||
var memoryStore = kvs.store('memory'); | ||
var memoryBucket = memoryStore.crateBucket({ max: 100, ttl: 10/*seconds*/ }); | ||
|
||
|
||
redisBucket.set('foo', 'bar', function(err) { | ||
if (err) { throw err; } | ||
|
||
redisBucket.get('foo', function(err, result) { | ||
console.log(result); | ||
// >> 'bar' | ||
redisBucket.del('foo', function(err) {}); | ||
}); | ||
}); | ||
|
||
function getUser(id, cb) { | ||
setTimeout(function () { | ||
console.log("Returning user from slow database."); | ||
cb(null, {id: id, name: 'Bob'}); | ||
}, 100); | ||
} | ||
|
||
var user_id = 123; | ||
var key = '#' + user_id; // for test if key is not the data (user_id) to load. | ||
|
||
// Using namespace "user" | ||
var redisLoadBucket = redisStore.crateBucket('user', { | ||
ttl: 100, /*seconds*/ | ||
|
||
// method to load a thing if it's not in the bucket. | ||
load: function (user_id, cb) { | ||
// this method will only be called if it's not already in bucket, and will | ||
// bucket the result in the bucket store. | ||
getUser(user_id, cb); | ||
} | ||
}); | ||
|
||
// `user_id` is used to load, or use `key` without `user_id` parameter. | ||
redisLoadBucket.get(key, user_id, function (err, user) { | ||
console.log(user); | ||
|
||
// Second time fetches user from redisLoadBucket | ||
redisLoadBucket.get(key, user_id, function (err, user) { | ||
console.log(user); | ||
}); | ||
}); | ||
|
||
// Outputs: | ||
// Returning user from slow database. | ||
// { id: 123, name: 'Bob' } | ||
// { id: 123, name: 'Bob' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
var fs = require('fs'); | ||
|
||
exports.store = | ||
exports.Store = require('./lib/store'); | ||
|
||
exports.__defineGetter__('version', function () { | ||
return JSON.parse(fs.readFileSync(__dirname + '/package.json')).version; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
var LRU = require("lru-cache"); | ||
|
||
exports.initialize = function initializeMemory(store, callback) { | ||
store.adapter = { | ||
crateBucket: function (options) { | ||
return new Memory(options); | ||
} | ||
}; | ||
process.nextTick(callback); | ||
}; | ||
|
||
function Memory(options) { | ||
options = options || {}; | ||
this.name = 'memory'; | ||
this.bucket = LRU({ | ||
max: options.max || 500, | ||
maxAge: options.ttl ? options.ttl * 1000 : null | ||
}); | ||
} | ||
|
||
Memory.prototype.has = function (key, cb) { | ||
cb(null, this.bucket.has(key)); | ||
return this; | ||
}; | ||
|
||
Memory.prototype.get = function (key, cb) { | ||
cb(null, this.bucket.get(key)); | ||
return this; | ||
}; | ||
|
||
Memory.prototype.set = function (key, value, cb) { | ||
this.bucket.set(key, value); | ||
if (cb) cb(null, value); | ||
return this; | ||
}; | ||
|
||
Memory.prototype.getset = function (key, value, cb) { | ||
if (typeof value === 'function') { | ||
cb = value; | ||
value = null; | ||
} | ||
var result = this.bucket.get(key); | ||
this.bucket.set(key, value); | ||
cb(null, result); | ||
return this; | ||
}; | ||
|
||
Memory.prototype.getdel = function (key, cb) { | ||
var result = this.bucket.get(key); | ||
this.bucket.del(key); | ||
cb(null, result); | ||
return this; | ||
}; | ||
|
||
Memory.prototype.del = function (key, cb) { | ||
this.bucket.del(key); | ||
if (cb) cb(); | ||
return this; | ||
}; | ||
|
||
Memory.prototype.keys = function (pattern, cb) { | ||
if (typeof pattern === 'function') { | ||
cb = pattern; | ||
} | ||
cb(null, this.bucket.keys()); | ||
return this; | ||
}; | ||
|
||
Memory.prototype.clear = function (pattern, cb) { | ||
if (typeof pattern === 'function') { | ||
cb = pattern; | ||
} | ||
var del_count = this.bucket.itemCount; | ||
this.bucket.reset(); | ||
cb(null, del_count); | ||
}; |
Oops, something went wrong.