3

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

jzimmerman2011
  • 1,806
  • 2
  • 24
  • 37
  • Unclear whether you'd be wishing to maintain the information across multiple pageloads, however I'd look at the session scope for this kind of caching if it's a per-user, or Application if it's per-application. Have a read of this answer : http://stackoverflow.com/questions/25672/coldfusion-when-to-use-the-request-scope – Simon at The Access Group Apr 18 '12 at 14:08
  • storing in session, or other persistent scope will not work for us; i've already tried it. i was only considering REQUEST as i assume it gets completely "thrown away" after the request ends, yet even "caching" this data per request can probably save at least 1/2 of our page load times, as we don't have to load the same data multiple times per request. – jzimmerman2011 Apr 18 '12 at 14:12
  • Well if it's just for a single request, how about the variables scope within the template(s) being called? Have used similar previously. Does this variable need to be accessible within functions? – Simon at The Access Group Apr 18 '12 at 14:54
  • yes, it needs to be accessible within functions and CFCs (i know, not good practice, but right now am more worried about performance as that has been an issue recently). – jzimmerman2011 Apr 18 '12 at 15:16

2 Answers2

3

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.

Community
  • 1
  • 1
  • 1
    yes, i know this is not good practice, but the way things are currently structured, i don't think it would be a good idea to revise the signatures of these CFC methods... our intention is to mostly abandon our current code base and start from scratch, where these design considerations would be made before writing the code and using it. right now though, we are looking at little tweaks we can make to hold us over until we can develop this new product. – jzimmerman2011 Apr 18 '12 at 15:25
  • Then as stated, `variables` or `request` would be suitable and safe enough. If you want to make sure they are destroyed, just do `StructDelete(request,'yourVariableName');` at the end of the script – Simon at The Access Group Apr 18 '12 at 15:26
1

Yes. The only request that has access to the REQUEST scope is the current request.

Dan Roberts
  • 4,664
  • 3
  • 34
  • 43