Growing up I never imagined I would play bass guitar for the Dave Matthews Band. And indeed it never happened. But I have become a passionate and pretty good web developer. I code mostly in ColdFusion and jQuery. Sam is a DZone MVB and is not an employee of DZone and has posted 16 posts at DZone. You can read more from them at their website. View Full User Profile

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

06.14.2011
| 4865 views |
  • submit to reddit

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.

References
Published at DZone with permission of Sam Farmer, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Ray Varner replied on Tue, 2011/06/14 - 9:36am

Hi Sam. Thanks for the post about objectSave(). I've not had the chance to play with this much but ran into issues when I did.

There is a distinct difference in what I want the function to do and what it actually does. This is probably the root of my problem. :)

In your example, you are essentially creating a struct and saving data using objectSave(). I was looking to:
  • Create and populate an ORM object, serialize and have it available at a later time with objectLoad.
  • and/or Do the same with a non-ORM object; with methods.
I understand the nature of persistence with regards to ORM, but in this case I'm looking to create a new object and objectSave() without persisting to the database with entitySave(obj).

What's your take?

Mitch Pronschinske replied on Thu, 2011/06/16 - 8:32am in response to: Ray Varner

I forwarded your comment to Sam.  He had a good response for you:

"A couple things that might work:

1. Try using entityReload() after using objectLoad()
2. Duplicate the object, use objectSave() then after objectLoad(), get the entity fresh and then populate the values from the object you got from objectLoad().
3. This entry from Mark Mandel also might work: http://www.compoundtheory.com/?action=displayPost&ID=464

Let me know if either of those work and if you have any other questions."

Passion Lab replied on Tue, 2012/08/14 - 2:09pm

I understand the nature of persistence with regards to ORM, but in this case I'm looking to create a new object and objectSave() without persisting to the database with entitySave(obj). Forex Programming

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.