2

In my application I have a sidebar (on every page) that displays a list of job openings for a smallish company. Is it wise to run the query in Application.cfc and store the results there somehow or is there a smarter way to run the query and store the results somewhere else in a different scope?

What is the best use of server resources?

Thanks in advance.

Edit: I'm using CF8. The updates will likely happen monthly.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Ofeargall
  • 5,340
  • 5
  • 28
  • 33

4 Answers4

3

Do you have CF9? I would use the application scope, either caching the query or the resultant HTML in a cache object using cachePut() and calling the cache object where needed with cacheGet(). Then wherever you would manage the updating of that information (either manual CRUD or a web service consuming this information, etc) just make sure to refresh that cache object when you sense a change. Populate the cache object initially in onApplicationStart() and you should be good to go.

Rob Barthle
  • 376
  • 2
  • 9
  • I'm using CF8. Although, we're looking at switching to CF9 very soon. – Ofeargall Jun 07 '11 at 18:22
  • I agree this approach would be best once you've upgraded. Generally you should try and cache as close as possible to the output, which in your case means caching the HTML for the jobs list. The query will only run once, CF will only generate the HTML once - thereafter you just keep returning the HTML fragment from the cache until the next update. You save DB calls AND CF processing without having to worry about whether your query is cached or not. – CfSimplicity Jun 09 '11 at 07:49
2

Used cachedwithin on the cfquery that the sidebar calls.

Bradley Moore
  • 748
  • 3
  • 11
  • This is, by far, the most straight forward approach. The question does not leave the impression anything more sophisticated is called for. – orangepips Jun 07 '11 at 17:06
  • So, if I have `SELECT * FROM jobs ` in my CFC that will cache the query result for the timespan and not call the DB until the timespan is over? – Ofeargall Jun 07 '11 at 18:46
  • For the record, I always use the `` tag in my queries. Just thought I would head that one off at the pass... – Ofeargall Jun 07 '11 at 18:51
  • That depends if the CFC is persistent and if the name of the query is persistent. If you store the object in the application scope and qJobs is not locally scoped, then yes it won't call the DB until the timespan is over. – Bradley Moore Jun 07 '11 at 19:10
  • Thanks, this looks like an elegant solution for my application. Appreciate the help. – Ofeargall Jun 07 '11 at 19:53
0

If the result depends on a user session, session scope comes to mind. Application scope will only be refreshed when the server (re)-starts, right? As far as resources are concerned, it's obviously nicer for the server to only load the results once when they are first requested (application scope). But it's also obvious that those results would quickly (or soon) become stale.

If you fear that too many queries are sent to the database, you could look into caching the results.

Since you talk about a smallish company, maybe you should just start by reloading the content per session and see if the load on the database server gets out of hand.

mkro
  • 1,902
  • 14
  • 15
  • I had initially considered the session scope but wasn't sure if it was the most elegant way to store the data. – Ofeargall Jun 07 '11 at 18:26
0

How often do the job openings change? You could store them in application, and have a scheduled task to periodically re-run the query.

duncan
  • 31,401
  • 13
  • 78
  • 99