http://coldfusion.com logo
#lucee
Title
# lucee
d

danmurphy

02/02/2022, 5:24 PM
Is there a good way to clear the cache for a specific query instead of the entire query cache (using cachedWithin to put it in the cache)? We see this post about a “tags” argument/attribute but there is no documentation on how to use it with queryExecute and the tags aren’t mentioned anywhere in the docs. Any help on clearing the cache of a specific query would be much appreciated. Thoughts?
b

bhartsfield

02/02/2022, 5:44 PM
just run it again with a 0 cachedwithin value
d

danmurphy

02/02/2022, 5:47 PM
The problem is we are setting the query into the cache but want to programmatically bust the cache on that query after doing an insert somewhere else in the app. So we were hoping to find a way to bust the cache by an ID or tag or something, but can’t figure out how to target that specific query from another place in the app. Does that make sense?
b

bhartsfield

02/02/2022, 5:52 PM
It's been a long time since I've cached a query on the application side. But, when I did, i would just run the query again after the update/insert/delete (same name) but with a cachedwithin of 0. That clears it. Lucee has a bunch of caching read/clear so I suppose the ability to clear a single query out of the cache could be buried in there somewhere.
d

danmurphy

02/02/2022, 5:54 PM
Yeah, we’ve been banging our heads on the wall about it. The funny thing is, the options struct of queryExecute says “Struct containing query options, all cfquery tag attributes are supported except the name attribute.” So trying to pinpoint the same query somewhere else has been tough.
Hmm. So we might be out of luck?
b

bhartsfield

02/02/2022, 6:01 PM
Looking that way 😕
d

dswitzer

02/02/2022, 6:03 PM
@danmurphy Play around with the
getCacheHandlerCollection
object above. I do recall seeing methods that would clear the entire cache (I think the method was
reset()
), but there was just nothing that let me view all the queries in cache.
d

danmurphy

02/02/2022, 6:03 PM
@zackster can you confirm this? You commented on the ticket https://luceeserver.atlassian.net/browse/LDEV-1280 that says we should be able to set
tags
for the query going into the cache (even within query Execute) and then easily clear it using that same
tags
. What you say in your comment in that ticket seems to be exactly what we’re after, but it just doesn’t seem to work.
z

zackster

02/02/2022, 6:14 PM
do you have a query cache defined? I dug up the commits and the test case, see the ticket
d

danmurphy

02/02/2022, 6:16 PM
Yes.
Checking that out now. I’m having a hard time figuring out where that would go for queryExecute. We were guessing in the options struct?
z

zackster

02/02/2022, 6:20 PM
yep
OMG queryExecute is just a wrapper around cfquery 🙂 now you know how the sausage is made
d

danmurphy

02/02/2022, 6:34 PM
Here is what I’m doing to try and test this ☝️
(Hard to do on trycf because of using queryExecute without a datasource.)
But yeah, the dumps are exactly the same before and after calling
cacheClear( 'blehTags', 'QueryTestCache');
. Am I missing something? (very likely)
z

zackster

02/02/2022, 6:37 PM
use QueryNew and QoQ ?
d

danmurphy

02/02/2022, 6:40 PM
Oh man, I think I got it. It was hiding in the documentation all along… https://docs.lucee.org/reference/functions/cacheclear.html
2. an Array containing tags defined when put the element to the cache
So I changed the
cacheClear( 'blehTags', 'QueryTestCache')
to
cacheClear( ['blehTags'], 'QueryTestCache')
and it worked.
👍 2
z

zackster

02/02/2022, 6:41 PM
cool
d

danmurphy

02/02/2022, 6:43 PM
So there wasn’t a way to say filter=‘myfilter stuff’ or tags=‘myTag stuff’. I guess if it is an array, it just assumes you are targeting tags for the first argument?
So this seems to actually work great if you are programmatically busting the cache somewhere like I described previously, which is awesome. Is the
cacheClear()
function the only one that uses
tags
? It seems that way after a cursory look through the docs and a bit of testing.
d

Daniel Mejia

02/02/2022, 7:30 PM
thanks @danmurphy for this great post. I could use this too.
👍 1
a

Andreas

02/02/2022, 9:30 PM
Yeah, @danmurphy I'd upvote +10 this post. Going to copy the entire thread and add it to my cheat sheet stuff
🎉 1
z

zackster

02/03/2022, 5:34 PM
anyone feeling like writing a post on dev.lucee.org about this?
d

danmurphy

02/03/2022, 7:35 PM
Hmm, I can give it a shot. I’ve not been around there much. Just add a new topic you’re saying? Would this be in the documentation or dev category? (Or tell me if I’m in the wrong place entirely).
Ok, here’s a summary of my learning about how to use the “tags” attribute while caching queries and clearing the cache. Let me know if I missed something. https://dev.lucee.org/t/caching-queries-and-clearing-the-cache-with-the-tags-attribute/9618
🍺 1
👍 3
z

zackster

02/04/2022, 2:52 AM
Are you on twitter? Also we can link this from the caching category on Lucee docs too
d

danmurphy

02/04/2022, 5:48 PM
@zackster I’m really not. Feel free to tweet away.
z

zackster

02/04/2022, 5:49 PM
only to HT you 🙂
d

danmurphy

02/04/2022, 5:49 PM
I mean, my handle is
@murphmandan
, but I have about 3 followers probably 😄 .
z

zackster

02/04/2022, 6:31 PM
4 Views