Over a million developers have joined DZone.

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

· Java Zone

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

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.

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.


Published at DZone with permission of Sam Farmer, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}