Vert.x等效于Node.js Global对象 [英] Vert.x Equivalent to the Node.js Global object

查看:77
本文介绍了Vert.x等效于Node.js Global对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Node.js中,您可以为全局对象的键指定值。这使您能够记住请求之间的某些内容。假设node.js进程没有死/挂起并通过像iisnode这样的进程重新启动。

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.

Vert.x是否有等效的?本质上,我正在为一段数据寻找最简单的缓存,所以我不必在每次请求时获取它。我假设Vert.x上的解决方案可以跨线程工作吗?

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?

推荐答案

代码:

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

中断因为它无效json,你可以使用

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

{公司: [{id:1,name:Yahoo},{id:2,name:Google}}} //注意,而不是在数组中

{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"}]}))

然后在其他Verticle

and then in other verticle

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

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

now ..可能是一个很好的选择,可以像使用缓存系统一样使用redis,虽然顶点地图似乎可以解决你的需求到目前为止......

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

这篇关于Vert.x等效于Node.js Global对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆