简体   繁体   English

Vert.x等效于Node.js全局对象

[英]Vert.x Equivalent to the Node.js Global object

In Node.js you can assign values to keys off of the global object. 在Node.js中,您可以为全局对象之外的键分配值。 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. 假设node.js进程不会死掉/挂起,而是通过iisnode之类的进程重新启动。

Does Vert.x have an equivalent? Vert.x是否具有等效功能? 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? 我认为Vert.x上的解决方案可能能够跨线程工作?

the code: 编码:

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

break cause it's not valid json,you can use 打破原因,它是无效的JSON,您可以使用

{companies : [{id:1,name:"Yahoo"},{id:2,name:"Google"}]} //notice than they are inside an array {companies:[{id:1,name:“ Yahoo”},{id:2,name:“ Google”}]} //注意它们在数组中

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... 现在..也许是一个不错的选择,可以像缓存系统一样使用redis,尽管到目前为止顶点映射似乎可以解决您的需求...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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