Skip to main content
WolfShade
Legend
May 24, 2011
Question

Clearing cache

  • May 24, 2011
  • 2 replies
  • 3588 views

Hello, everyone.

I'm working with someone else's CFC that contains many different methods, a few of which are queries that are being cached for two weeks.

One of the queries gets a list of table column names that is used to insert data into said table (gets column name, data type, is nullable).

For a few hours, I was wondering why only half of the form that is being submit was actually being updated in the database - now, I know; it's because the cached query wasn't getting the columns that I had just added, and the associated form fields.

I did some research and read, somewhere, that if you use the 'cachedwithin' parameter with a negative value, it will clear the cache.

Either someone is putting out bad information, or I'm on a server that has a setting that prevents that from happening:

I tried this with a negative value (-16 days, to overwrite the original 14 day cache) and as long as I kept it in place with a negative value, it was getting fresh data.

However, when I switched it back to the original 14 day cachedwithin value, it went back to getting the original cached data.

Is there a way to absolutely clear the cache of one query, so that even if the original cachedwithin parameter is used, it will start another fresh query and cache it, instead?

Thanks,

^_^

    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    Community Expert
    May 24, 2011

    Oh, I hope you wont be tempted to use <cfobjectcache       action = "clear">. It will certainly do the job, but will clear all cached queries.

    Inspiring
    May 25, 2011

    It might be worth voting for this:

    http://cfbugs.adobe.com/cfbugreport/flexbugui/cfbugtracker/main.html#bugId=82164

    I think the gist of what Aaron is suggesting is to add more granularity to how CFCACHE works; being able to clear individual queries from cache.

    It's a good idea, and one that people have been wanting for a while (like: anyone who every contemplates using the cachedwithin attribute of CFQUERY).

    --

    Adam

    BKBK
    Community Expert
    Community Expert
    May 25, 2011

    Adam Cameron. wrote:

    It might be worth voting for this:

    http://cfbugs.adobe.com/cfbugreport/flexbugui/cfbugtracker/main.html#b ugId=82164

    I think the gist of what Aaron is suggesting is to add more granularity to how CFCACHE works; being able to clear individual queries from cache.

    While I applaud an enhancement request for functionality to clear cached queries, I do believe associating that functionality with cfcache wont work.  Query resultsets are Coldfusion objects, whereas the cfcache tag applies to pages. The most appropriate tag to extend would be cfobjectcache.

    BKBK
    Community Expert
    Community Expert
    May 24, 2011

    No need for -16 days. It is sufficient to use -1 second.

    I would do something like

    <cfquery name="updateQuery" datasource="myDSN">
    INSERT INTO myTbl (col1, col2, col3)
    VALUES ('a', 'b', 'c')
    </cfquery>

    <cfquery name="getData" datasource="myDSN" cachedwithin="#createtimespan(0,0,0,-1)#">
    SELECT col1, col2 FROM myTbl   
    </cfquery>

    And here is the key: everything in the select query must be identical in the 2 cases, in every detail, with the only exception being the arguments of  createtimespan, which change from (0,0,0,-1) to (14,0,0,0). I also think you should either place the 2 queries above in a transaction tag, or reintroduce the 14-day query in a new request.

    Inspiring
    May 25, 2011

    No need for -16 days. It is sufficient to use -1 second.

    Or even just zero.  But yeah, this is the approach to use.

    I would parameterise the timespan, setting something like an application-scoped variable, or - if the query is in a CFC method (which is should be) - something in the variables scope of the object, but also something that can be passed in as an argument to the get method.  In the normal scheme of things, leave the timespan as [whatever cache timespan you want], but after an operation that changes the data, call the "get" method with a zero-timespan to flush it.  If, however, you have various different methods which gets data from the same table but using different SQL, then this approach will get bogged down very quickly.

    Another alternative is to keep all the data-centric business on the DB (where it belongs), including the decisions where, how and when to cache the data.

    --

    Adam