Vert.x Equivalent to the global Node.js object

In Node.js, you can assign values ​​to keys from a global object. This gives you the ability to "remember" something between requests. Assuming Node.js process dies / hangs and is not restarted by a process like iisnode.

Does Vert.x have an equivalent? Basically, I'm looking for the simplest possible cache for a piece of data, so I don't need to fetch it on every request. I suppose the solution on Vert.x can work with streams?

+1


source to share


1 answer


code:

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

      

break because it is not valid json you can use

{companies: [{id: 1, name: "Yahoo"}, {id: 2, name: "Google"}]} // note that 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

      



This means that you may need

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

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

      

and then at another vertex

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

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

      

now..maybe a good option that would use redis as a cache system, although a vertexmap seems to solve your needs so far ...

+1


source







All Articles