1

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?

guido
  • 18,864
  • 6
  • 70
  • 95
BuddyJoe
  • 69,735
  • 114
  • 291
  • 466
  • 1
    You mean like [this](http://vertx.io/core_manual_js.html#shared-data). I don't know what you mean by across threads. – user568109 May 06 '14 at 10:27
  • Yep That would work for simple data types. Found that it breaks down if you tried to add {{id:1,name:"Yahoo"},{id:2,name:"Google"}} via the JavaScript language. – BuddyJoe May 06 '14 at 15:09
  • 1
    From the doc: "vert.x only allows simple immutable types such as number, boolean and string or Buffer to be used in shared data". So, did you add this as JSON string (should be OK) or a JS object (should fail)? – David Dossot May 07 '14 at 02:13
  • 1
    I never used it as object, but I suppose it would fail. You see in JS the objects can be used by their references(not entirely). See [here](http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language). This poses a risk - what if some verticle modifies it. Since you cannot do anything to prevent mutation, better not allow them. – user568109 May 07 '14 at 05:02

1 Answers1

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

clagccs
  • 2,224
  • 2
  • 21
  • 33