Is there a way to clear all cached results of a si...
# cfml-general
d
Is there a way to clear all cached results of a single cfquery in
one request
?
my cfquery
Copy code
<cfquery name="defaultorgqry" datasource="mydbdatasource" 
    cachedafter="#cachedate#" 
    result="defaultorgresult"
    select BillingCategory from mydatabase.dbo.EM 
    where employee = <cfqueryparam value="#employeeid#" cfsqltype="cf_sql_integer" />
</cfquery>
a
cacheid attribute of cfquery?
d
I've never used that. is that a value I have to provide to the attribute?
a
I have not used it but looks like you need to provide it. Not sure though.
d
I guess so, I'm not how you are suppose to use that id.
it says you can use the id to remove cache but doesn't say how
a
cacheRemove() should do the trick.
d
snap, this method cacheRemoveAll() makes even easier.
I never knew about these methods. Thanks.
a
Better way would be to create a cacheregion and then provide the cacheid & cacheregion attributes of cfquery. Later on when you need to remove all the cache of the cfquery you can clear out the cache region. That would make sure that only that particular query's cache is deleted.
🔥 1
oh yeah I remember now how the cache works. when you create the cache using cacheput you need to provide an ID. Similary cacheid attribute of cfquery works. You can make use of uuid() I guess.
Happy coding!
d
My mind exploded right now.
😂 1
a
Keep holding and hanging on to it 😄
d
I've only ever used cfquery cachedwithin and cachedafter.
a
yeah those do the trick unless you need additional things just as you needed this particular scenario's solution.
d
Makes me think about how much different apps can be designed.
👍 1
d
Are you on Lucee @Daniel Mejia?
d
i'm not
But eventually I will be
why?
d
We dug into this a bit and ended up using cache tags. Let me find the link, one sec.
d
Nice. Can you explain what Lyle_Karstensen meant in this comment.
d
It sounds like he was saying it loops over every cache tag to clear instead of just calling out the tag directly by id and clearing it? So clearing might be slow(er). We haven’t noticed any issue at all. I also don’t know if he means clearing the cache is 20ms instead of 10ms or what.
1
d
Ok thanks. I get it. Also sounds like he is doing something custom with cfquery and MongoDB to make that faster.
d
🤷‍♂️
In our experience, it works great with everything default.
But we don’t have hundreds or thousands of distinct tags either.
d
I'd only have 1 if I could that feature.
😄 1
lol
wait, how many tags does he have. even if it was 1000 tags that should still be pretty fast
d
I’d say it is something that should be worried about once it becomes a problem. Not pre-optimizing for something that might not ever be an issue.
d
oh wait he probably creates a tag for all users and teams and combination with all the different queries. so that could be big. but yes good point.
So I will actually have 600+ cacheids for my specific query that I posted above.
her e is my first try at using cacheRegion
Copy code
// DEFAULT LABOR CATEGORY PER EMPLOYEE *********************
TS_LABORCATEGORY_REGION = 'ts_entry_laborcategory';

// IF url param 'tscachebust=true' clear all cache results for TS_LABORCATEGORY_REGION
if( structKeyExists(url,'tscachebust') and url.tscachebust == 'true'){
    // IF region exists then remove it and all its cached results
    if( cacheRegionExists( TS_LABORCATEGORY_REGION ) ){
        cacheRegionRemove( TS_LABORCATEGORY_REGION );
    }
}

// create region, does not throw error is it already exists
cacheRegionNew(region= TS_LABORCATEGORY_REGION ,throwOnError=false);

// Get user's default labor category
// query for each employee will cache for 1 day
defaultorgqry = queryExecute(
    'select BillingCategory from mydatabase.dbo.EM where employee = :employeeid',
    {employeeid: theEmpN},
    {
        datasource='mydbdatasource',
        cachedwithin = createTimespan(1,0,0,0),
        cacheRegion = TS_LABORCATEGORY_REGION,
        cacheid = 'id_'&theEmpN,
        result='tslcresult'
    }
);
z
CachedWithin="request" in Lucee
👍 1