kosyak

0.0.5 • Public • Published

kosyak

NPM version

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 --save

Usage

'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
    • socket object - Options passed to net.Socket constructor.
    • server object - Options passed to net.Server constructor.
    • stringify boolean - If data should be stored as JSON string. Defaults to false.
    • fetch(key, callback) function - Fetch function. Read more about it in Node#get method description.
    • connectDelay number - Delay between attempts to connect remote node (ms). Grows in arithmetic progression. Defaults to 2000.
    • connectAttempts number - Max # of attempts to connect to remote node. Defaults to 3.

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:

  1. Check if entry is stored locally. If local entry is found it retured. Otherwise go to step 2.
  2. Check if entry is stored in one of the remote nodes. If so get request is sent to remote node. Otherwise go to step 3.
  3. If fetch function is specified in Node constructor options fetch(key, done) is called, then value is set locally and other nodes are notified. Otherwise null is 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?

Package Sidebar

Install

npm i kosyak

Weekly Downloads

2

Version

0.0.5

License

MIT

Last publish

Collaborators

  • estliberitas