Is there a good way to clear the cache for a speci...
# lucee
d
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
just run it again with a 0 cachedwithin value
d
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
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
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
Looking that way 😕
d
@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
@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
do you have a query cache defined? I dug up the commits and the test case, see the ticket
d
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
yep
OMG queryExecute is just a wrapper around cfquery 🙂 now you know how the sausage is made
d
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
use QueryNew and QoQ ?
d
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
cool
d
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
thanks @danmurphy for this great post. I could use this too.
👍 1
a
Yeah, @danmurphy I'd upvote +10 this post. Going to copy the entire thread and add it to my cheat sheet stuff
🎉 1
z
anyone feeling like writing a post on dev.lucee.org about this?
d
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
Are you on twitter? Also we can link this from the caching category on Lucee docs too
d
@zackster I’m really not. Feel free to tweet away.
z
only to HT you 🙂
d
I mean, my handle is
@murphmandan
, but I have about 3 followers probably 😄 .
z