Over a million developers have joined DZone.

Sharing Data Across Instances with objectSave() and objectLoad()

· Java Zone

Learn more about Kotlin, a new programming language designed to solve problems that software developers face every day brought to you in partnership with JetBrains.

Running multiple ColdFusion instances is pretty awesome from failover to performance. Sharing data among them throws up a couple possibilities:

  • Use a database (save from one, retrieve from another)
  • Use ColdFusion 9.0.1 with Distributed Ehcache and then use the cachePut() and cacheGet() functions.
  • Use objectSave() and objectLoad() while saving the object to disk.


Recently I needed to do just this with a dataset of structures and arrays that could have changed at anytime. This made option 1 a little tricky. Option 2 was my favorite but required some extra installs with Ehcache (which is a simple 7 step process, but would have required testing, yada, yada). So, option 3 was what we picked and it worked well.

Here is a simple example of how to do it. First create and save your object:

myObject = {
    funRun = [1,3,5],
    hardcore = [10,26,1000]
};
objectSave( myObject, "myObject.cfo");

The objectSave() function takes two arguments; first the object, and second the filename. By default it saves it relatively to the running file. The documentation use a ".out" extension but I think a ".cfo" is more fun (ColdFusion Object or if you need to please your CFO tell them you named it after them!).

Retrieval is just as easy:

<cfset myObject = objectLoad("myObject.cfo")> 

And that will work across any instance.

If you have large objects you may run into issues with writes and saves coming at the same time. In that case wrap the objectLoad() in a try/catch.

The Java Zone is brought to you in partnership with JetBrains.  Discover how powerful static code analysis and ergonomic design make development not only productive but also an enjoyable experience.

Topics:

Published at DZone with permission of Sam Farmer , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}