Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6846323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:36:27+00:00 2026-05-27T00:36:27+00:00

I have set caching on a query that is getting run a lot of

  • 0

I have set caching on a query that is getting run a lot of times.. the query itself is not all that slow, but it get’s run many times for each request, so figured caching may help. I have enabled caching, but doesn’t really seem to be making a difference.. how do I tell if my query is being cached or not?

I’m setting caching with : q.setCachedWithin("#createTimespan(0, 1, 0, 0)#");

Here is my full query preperation:

            q = New Query();
            q.setSQL("SELECT * FROM guest_booking WHERE room_id = :roomID and check_in <= :iDate and check_out > :iDate and status != 0");
            q.setName("checkAvailability");
            q.setCachedWithin("#createTimespan(0, 1, 0, 0)#");
            q.addParam(name="iDate", value="#createODBCDate(arguments.date)#", cfsqltype="cf_sql_date");
            q.addParam(name="roomID", value="#createODBCDate(arguments.room_id)#", cfsqltype="cf_sql_integer");
            qResult = q.execute().getresult();

Debug output is showing:

checkAvailability (Datasource=accom_crm, Time=16ms, Records=1) in C:\ColdFusion9\CustomTags\com\adobe\coldfusion\base.cfc @ 16:15:56.056


                        SELECT * FROM guest_booking WHERE room_id = 

                                    ?

                                 and check_in <= 

                                    ?

                                 and check_out > 

                                    ?

                                 and status != 0

Query Parameter Value(s) -
Parameter #1(cf_sql_integer) = 56
Parameter #2(cf_sql_date) = {ts '2011-11-14 00:00:00'}
Parameter #3(cf_sql_date) = {ts '2011-11-14 00:00:00'}

Many thanks in advance..

Jason

EDIT AFTER SHAWN’S ANSWER BELOW

Have changed the following two lines of query preperation:
query name is now different for differ query.. created dynamically from paramaters passed in

q.setName("check#arguments.room_id##DateFormat(arguments.date,'ddmmyy')#");

createTimeSpan removed from quotes, so not passed in as a string.

q.setCachedWithin(createTimespan(0, 1, 0, 0));

I have also tried sending through an unprepared query (not using addparam(), but just rendering the variables straight in the query string), but made no difference..

EDIT 2 AFTER SHAWN’S 3rd EDITANWSER BELOW
Shawn.. nice pickup on edit 3!!! you have isolated where the problem is. (anyone reading this, quick, up vote Shawn’s answer, he found a needle in a hay stack)

Passing the dates in as params does not cache..e.g..

q.setSQL("SELECT booking_id FROM guest_booking WHERE room_id = :roomID and check_in <= :iDate and check_out > :iDate and status != 0");
q.addParam(name="iDate", value="#createODBCDate(arguments.date)#", cfsqltype="cf_sql_date");

Just passing it in as a variable does not cache..e.g..

q.setSQL("SELECT booking_id FROM guest_booking WHERE room_id = :roomID and check_in <= #createODBCDate(arguments.date)# and check_out > #createODBCDate(arguments.date)# and status != 0");

BUT hard coding the dates DOES cache..e.g..

q.setSQL("SELECT booking_id FROM guest_booking WHERE room_id = :roomID and check_in <= {ts '2011-12-16 00:00:00'} and check_out > {ts '2011-12-16 00:00:00'} and status != 0");

This is all good, but clearly I can’t hard code the dates… The dates will change for each day obviously, but even where I run the same query with the same dates being passed in dynamically (query syntax being exactly the same), the query won’t cache if the dates are passed in as variables.. only if they are hard coded into the query.. wierd.. will keep playing and see what I can find.

Thank you Shawn for pinpointing the problem !!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:36:27+00:00Added an answer on May 27, 2026 at 12:36 am

    If it was correctly cached, your debug output would include just a tiny bit of additional info, to the tune of:

    checkAvailability (Datasource=accom_crm, Time=0ms, Records=1, Cached Query) 
    

    Something is preventing your query from being cached.

    I notice your call to .setCachedWithin() has a string being passed to it-or rather, you’re making it a string by qualifying it with quotes, and using # signs.

    Try passing the actual value returned from CreateTimeSpan(), without converting it to a string, like so:

    q.setCachedWithin(createTimeSpan(0, 1, 0, 0));
    

    — edit —

    Some other tidbits to note about query caching:

    1. The name of the query must be the same.
    2. The SQL statement (in all its parameterized forms) must be the same.
    3. The datasource must be the same.
    4. If used, the username & password must be the same.
    5. The DBTYPE must be the same.

    All these attributes must remain the same from call to call in order for ColdFusion to consider it a cache-able query. You mentioned above that you tried taking the addParam() calls out, but still had no luck…

    …try using a static query name, rather than one with variables–see if you get any further

    q.setName("checkTestQuery");
    

    — 2nd edit —

    Another often overlooked issue is the ColdFusion server’s clock. Make sure the CFServer’s date/time is set correctly. This may sound silly, but I’ve seen many a “production” server whose clock was completely off, and not set to the correct timezone, let alone time…and time, of course, is of great significance within the context of caching.

    — 3rd edit —

    After re-reading and reviewing everything, I’m going to recommend you take one more look at the 2nd point I made above regarding the SQL statement needing to be the same, and your WHERE clause being dependent upon a variable that is influenced by date/time, which implicitly could change on every request.

    …and, since the SQL statement must remain the same in order to be cached, CF discards any attempt to cache it.

    Try restructuring the SQL statement temporarily, without the WHERE clause looking for those date variables…and see what it produces.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have set up a system where notifications get sent to a user with
I have set up a Django application that uses images. I think I have
I have set the FlushMode property on an NHibernate session to FlushMode.Never, but when
I have set the itemRollOver and itemRollOut event listeners on a List component, but
I have set a list of items in a combobox, but when I debug
i have a set of web pages specifically designed to run on iPhones (using
I have been trying to get SqlCacheDependency working. I think I have everything set
I have a large data set that is updated once a day. I am
I have an entity that has an NON-ID field that must be set from
I have made my own jQuery plugin, but it has problem that values I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.