简体   繁体   中英

Vert.x Equivalent to the Node.js Global object

In Node.js you can assign values to keys off of the global object. This gives you the ability to "remember" something between requests. Assuming the node.js process doesn't die/hang and restart by a process like iisnode.

Does Vert.x have an equivalent? Essentially, I'm looking for the simplest possible cache for a piece of data so I do not have to fetch it on every request. I assume the solution on Vert.x may be able to work across threads?

the code:

   {{id:1,name:"Yahoo"},{id:2,name:"Google"}} 

break cause it's not valid json,you can use

{companies : [{id:1,name:"Yahoo"},{id:2,name:"Google"}]} //notice than they are inside an array

now..the doc says

 To prevent issues due to mutable data, vert.x only allows simple immutable types such as number, boolean and string or Buffer to be used in shared data

that means, maybe you will need use

 var map = vertx.getMap('demo.mymap');

 map.put('data', JSON.stringify({companies : [{id:1,name:"Yahoo"},{id:2,name:"Google"}]}))

and then in other verticle

 var map = vertx.getMap('demo.mymap');

var yourJSON = JSON.parse(map.get('data');

now..maybe a good option which would be use redis like a cache system, although the vertex map seems solve your needs so far...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM