I found that I could not share this hashmap
// start
pm2 start lib/index.js -i 2 --name cache
// I want the effect
4|cache | start
4|cache |
4|cache | undefined
4|cache | hello world
4|cache | end
5|cache | start
5|cache | hello world // !!!!!!!!!!! right
5|cache | undefined
5|cache | hello world
5|cache | end
// But actually
4|cache | start
4|cache |
4|cache | undefined
4|cache | hello world
4|cache | end
5|cache | start
5|cache | // !!!!!!!!!!! error
5|cache | undefined
5|cache | hello world
5|cache | end
`
#[macro_use]
extern crate lazy_static;
extern crate neon;
use neon::prelude::*;
use neon::register_module;
use std::collections::HashMap;
use std::sync::Mutex;
lazy_static! {
static ref HASHMAP: Mutex<HashMap<String, String>> = Mutex::new(HashMap::new());
}
fn hashmap_put(key: String, value: String) {
HASHMAP.lock().unwrap().insert(key, value);
}
fn hashmap_remove(key: String) {
HASHMAP.lock().unwrap().remove(&key);
}
fn hashmap_get(key: String) -> String {
match HASHMAP.lock().unwrap().get(&key) {
Some(value) => value.to_owned(),
None => "".to_owned(),
}
}
fn put(mut cx: FunctionContext) -> JsResult<JsUndefined> {
let key = cx.argument::<JsString>(0)?.value();
let value = cx.argument::<JsString>(1)?.value();
hashmap_put(key, value);
Ok(cx.undefined())
}
fn remove(mut cx: FunctionContext) -> JsResult<JsUndefined> {
let key = cx.argument::<JsString>(0)?.value();
hashmap_remove(key);
Ok(cx.undefined())
}
fn get(mut cx: FunctionContext) -> JsResult<JsString> {
let key = cx.argument::<JsString>(0)?.value();
Ok(cx.string(hashmap_get(key)))
}
register_module!(mut m, {
m.export_function("put", put)?;
m.export_function("remove", remove)?;
m.export_function("get", get)?;
Ok(())
});
nodeJS
var addon = require('../native');
console.log('start')
console.log(addon.get('1')); // exist?
console.log(addon.put('1',"hello world"));
console.log(addon.get('1'));
console.log('end')
you start multiple nodejs processes, which means you have multiple HASHMAP instance
you start multiple nodejs processes, which means you have multiple
HASHMAPinstance
What do I need to do to complete multiple node processes one HASHMAP instance,
Thank you for your advice
What do I need to do to complete multiple node processes one
HASHMAPinstance
Perhaps you can manage node process in the Rust side, so that you can share memory to which node processes you forked.
But this solution too complex to implement. Maybe you should consider share cache with Redis or something similar.
I don't really want to use redis, because what I want is very simple. Let me think again
The difficult factor here is that you build on top of PM2, as @Brooooooklyn already mentioned you could use wrapper code to manage the memory for you, use IPC or a different alternative to this is to let node manage the memory for you by utilizing worker threads. By doing this they share the same Process and Isolate.
https://github.com/elast0ny/shared_memory-rs/tree/master/examples
I will try
correct !
Most helpful comment
The difficult factor here is that you build on top of PM2, as @Brooooooklyn already mentioned you could use wrapper code to manage the memory for you, use IPC or a different alternative to this is to let node manage the memory for you by utilizing worker threads. By doing this they share the same Process and Isolate.