Stability level

Current stability level for Memory Store:

Alpha
Beta 1
Beta 2
Stable
Visit here to report an issue or request a feature enhancement.

Back To Top

Introduction

JXcore provides you with a fast and efficient in-memory storing capabilities. We call it Memory Store.

It’s implementation is based on Google’s cpp-btree library, hence benefits from it’s advantages.

The major performance benefit from Memory Store is very high access speed (even for large containers) and low memory foot-print. Indeed, the best news is, that Memory Store is kept outside V8′s heap memory space. As a result it doesn’t fall under it’s limitations (similar to Multithreaded isolates). Keeping V8 away from high memory pressure also results in more responsive application since the unexpected garbage collection may keep the application blocked several seconds.

The Memory Store works like a standard key/value store. It is available for both main and sub threads and also as a separate or shared object.

jxcore.store

When used in multithreading, every subthread has it’s own jxcore.store object. It can be considered as a static global per store context, which means, that threads cannot share the same jxcore.store among themselves. But all of the tasks running inside a particular thread have shared access to it.

jxcore.store.shared

In some scenarios you may need a single store, which you could access from any thread. Use jxcore.store.shared object for this purpose. You should be aware of the following principles, even if all operations on this store are thread-safe. First of all, access time may take little longer comparing to the single threaded jxcore.store especially when using operations, which modify content of the data store (set(), remove() and get() – the last one also removes the item). Also, when different threads are simultaneously writing/modifying value of the same key, the final stored value is the one that is updated last.

Thread-safe jxcore.store.shared has exactly the same methods as single threaded jxcore.store.

Back To Top

How it works?

The Memory Store internally keeps all values as strings. You can also save other primitive value types (numbers, booleans), but they will be automatically converted to strings. For any other types, like objects or arrays – the conversion has to be done by you.

Storing the value:

// string
jxcore.store.set("myKey", "my value");

// number, will be stored as string "3.14"
jxcore.store.set("myKey", 3.14);

// boolean, will be stored as string "true"
jxcore.store.set("myKey", true);

// object
var obj = { val1: "one", val2 : 2, arr : [ 3, 4, 5] };
jxcore.store.set("myKey", JSON.stringify(obj));

When the object is written to the Memory Store, the value is copied into a memory space completely separated from what is used by V8. Thus, you can even delete the object on the javascript side, so it can be handled by the garbage collection:

obj = null;

Don’t worry, you still have your copy in a safe place.
Whenever you want to take it back, simply call one of the:

var copy = jxcore.store.read("myKey");
// or
var item = jxcore.store.get("myKey");

The difference between read() and get() is that get() also removes the key and value from the store. For more information, please refer to the api documentation.

Please keep in mind that at the moment when you read() the object, you have 2 instances of it in the memory: one is still kept in Memory Store, and the second one (result from the read() method) resides in V8′s memory block.

If you use large strings, you may not want to keep the duplicates. For this purpose you can use directly get(), which will remove the value from the store.

Back To Top