Vert.x Equivalent to the Node.js Global object

痞子三分冷 提交于 2019-12-22 00:58:16

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/23481062/vert-x-equivalent-to-the-node-js-global-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!