node-ipc-store

1.0.8 • Public • Published

node-ipc-store

node-ipc-based freely configurable shared storage. Memory, file and MySQL accessors included.

Installation

npm i node-ipc-store

Server

Configuration

{
	ipc: { // node-ipc config
		id: "node-ipc-store",
		silent: true
	},
	accessor: new Accessor, // accessor used to restore values in the cache
	cachedValueTtl: 3600 * 1000, // values are removed from the cache if the time has expired (use -1 as infinity)
	cachedValueTtlTestInterval: 60 * 1000,
	startupTimeout: 5 * 1000 // server shuts down if the time after startup has expired and the clients have not connected (use -1 as infinity)
}

Example

const {Server} = require("node-ipc-store");

const server = new Server;

Simple accessor

This accessor is used for in-memory caching - it doesn't actually store values anywhere.

Configuration

{
	decorate: undefined, // f(server, key, value, onResult) - use to decorate the value before return
	actions: {} // name -> f(server, value, onResult)
}

FileAccessor

This accessor stores values in files.

Configuration

{
	decorate: undefined, // f(server, key, value, onResult) - use to decorate the value before return (do not modify the value if an error occurred)
	actions: {}, // name -> f(server, value, onResult)
	path: "./dat/" // the values are stored in the path/key files
}

MysqlAccessor

This accessor stores values in a MySQL DB. For tables without specified rule, this accessor uses default rules with keys like: some_table/some_key_field_name/some_key_field_value/some_field_name (for one field) or some_table/some_key_field_name/some_key_field_value (for all fields).

Configuration

{
	decorate: undefined, // f(server, key, value, onResult) - use to decorate the value before return (do not modify the value if an error occurred)
	actions: {}, // name -> f(server, value, onResult)
	connection: undefined,
	rules: {} // table -> sql, values(key), parse(results), where parse return [success, value]
}

Router

This accessor is used for routing between multiple accessors by key/name prefix.

Configuration

{
	accessors: {} // prefix -> accessor
}

Client

Configuration

{
	ipc: { // node-ipc config
		id: "node-ipc-store",
		retry: 1000,
		silent: true
	},
	serverPath: "./server.js", // clients automatically start the server (use undefined to prevent autostart)
	defaultRequestTtl: 5 * 1000, // requests are considered failed if the time has expired (use -1 as infinity)
	requestTtlTestInterval: 1 * 1000
}

Methods

get

get(key, onResult, ttl = this.#config.defaultRequestTtl, hash)

Used to get the value from the cache or via the accessor (if the value is not in the cache). The value is not actually passed if the hashes are the same. The value is always passed if the hash is undefined or the accessor does not support hashing.

set

set(key, value, onResult, ttl = this.#config.defaultRequestTtl)

Used to set the value.

remove

remove(key, onResult, ttl = this.#config.defaultRequestTtl)

Used to remove the value.

resetCache

resetCache(key, onResult, ttl = this.#config.defaultRequestTtl)

Used to remove the value from the cache.

action

action(name, value, onResult, ttl = this.#config.defaultRequestTtl)

Used to perform some actions on the server.

setServerPath

setServerPath(serverPath)

Used to set the path to the server.

shutdownServer

shutdownServer()

Used to shutdown the server.

Example

const {Client} = require("node-ipc-store");

const client = new Client;

const actions = [
	() => client.set("x", 42, (success, details) => {
		console.log("set x:", success, details);
	}),

	() => client.get("x", (success, details) => {
		console.log("get x:", success, details);
	}),

	() => client.remove("x", (success, details) => {
		console.log("remove x:", success, details);
	})
];

const exec = () => {
	const action = actions.shift();
	if (action) {
		action();
		setTimeout(exec, 2000); // sending requests with pauses
	} else {
		console.log("done");
		process.exit();
	}
};

exec();

Output

c:\npm\node-ipc-store\examples>cache\node client.js
Client: disconnected
Client: fork
Client: connected
set x: true undefined
get x: true 42
remove x: true undefined

For a complex example, visit examples/mysql-ext/server.js.

Dependencies (3)

Dev Dependencies (6)

Package Sidebar

Install

npm i node-ipc-store

Weekly Downloads

2

Version

1.0.8

License

MIT

Unpacked Size

41 kB

Total Files

28

Last publish

Collaborators

  • a.gusev