kosyak v0.0.5
kosyak
Distributed in-memory cache store written in Node.js.
All the data is stored on different nodes which are connected between each other with simple TCP/IP JSON-messaging.
Attention! Data consistency is not guaranteed since if one node dies all the data stored in it is gone.
Installation
npm install kosyak --saveUsage
'use strict'
var assert = require('assert')
var Node = require('kosyak')
var firstNode = new Node({
host: '127.0.0.1',
port: 13390
}).init(/* callback(err) */)
var secondNode = new Node({
master: {
host: '127.0.0.1',
port: 13390
}
}).init(/* callback(err) */)
// after all initialized
firstNode.set('key', 123, function(err, value) {
assert.strictEqual(value, 123)
secondNode.get('key', function(err, value) {
assert.strictEqual(value, 123)
firstNode.stop(/* callback */)
secondNode.stop(/* callback */)
})
})API
Node
Base class which provides functionality to work with whole store.
Node(options)
Create Node instance.
Arguments
- options
object- Client options.- addr
string- Address or host to bind to. - port
number- Port to bind to. - master
object- Address/port of master node.- addr
string - port
number
- addr
- socket
object- Options passed tonet.Socketconstructor. - server
object- Options passed tonet.Serverconstructor. - stringify
boolean- If data should be stored as JSON string. Defaults tofalse. - fetch(key, callback)
function- Fetch function. Read more about it inNode#getmethod description. - connectDelay
number- Delay between attempts to connect remote node (ms). Grows in arithmetic progression. Defaults to2000. - connectAttempts
number- Max # of attempts to connect to remote node. Defaults to3.
- addr
void eachEntry(iterator)
Iterate through each local entry
Arguments
- iterator
function- Iterator. Should accept following arguments:(value, key, entry).
void get(key, callback)
Get or fetch value by key. Get operation works using following algorithm:
- Check if entry is stored locally. If local entry is found it retured. Otherwise go to step 2.
- Check if entry is stored in one of the remote nodes. If so
getrequest is sent to remote node. Otherwise go to step 3. - If
fetchfunction is specified in Node constructor optionsfetch(key, done)is called, then value is set locally and other nodes are notified. Otherwisenullis returned.
Arguments
- key
string - callback(err, value)
function- Callback function.
void init(callback)
Start server and connect to master node if it's specified in options.
Arguments
- callback(err)
function- Callback function.
void remove(key, callback)
Remove local entry.
Arguments
- key
string - callback(err)
function- Callback function.
void set(key, value, callback)
Set value locally and notify other nodes.
Arguments
- key
string - value
*- Value to be set. - callback(err, value) - Callback function.
object stats(callback)
If callback is specified node requests stats from all the nodes and returnes them as callback first argument. If not specified stats for node returned.
Arguments
- callback(nodesStats)
function- Callback function.
void stop(callback)
Stop node and disconnect from all the remote nodes.
Arguments
- callback()
function- Callback function.
FAQ
Why this cache store it's called so?
My friend has a cat called Kosyak. And this cat is crazy. Why not call module so?