Is the storage of the CFC Coldfusion instance in the REQUEST scope safe?

advertisements

Is it safe to store a CFC object in the REQUEST scope to be accessed later? Right now, our sites load up navigation data at least twice, possibly three times if they use our breadcrumbs feature. Some times, this data can vary, however, most of the time, three separate calls end up being made to grab the same exact navigation data...

So, I was thinking after the first load, save the navigation data in the REQUEST scope in some sort of struct, and in subsequent calls, just check to see if that data is already there, and if so, just use what is stored rather than re-creating it again. I know this would be accessing a shared scope outside of a contained object, which is probably not good practice, but in the end could shave off half of our page load times...

I know it can be done, however, we have had problems with the server recently, some of it possibly being memory leaks from how we use/store certain things, so was wondering if this was safe to do...


Either the variables or request scope would be suitable for your purpose, however more advisable would be to modify the functions that require access to this variable to accept your cached variable as an argument. With regard to CFCs it could be passed in the init() method and stored for use by the methods within that CFC (assuming you initialise it)

By relying on a global variable (even one restricted to current request) you are potentially just causing difficulties for yourself down the line, which would be solved by ensuring the methods are more encapsulated.

As mentioned in my comments earlier, ColdFusion - When to use the "request" scope? is worth a quick read as it has relevant information in the answers.