Search code examples
coldfusioncoldfusion-2016

Get current template's last modified date without accessing HDD


I know I can use getFileInfo(getCurrentTemplatePath()) to get the current template's last modified date, but it would be better if I could just grab it from memory. I have several possible uses in mind, but I'm not ready to defend them yet, so for now let's just say I'm asking out of curiosity.

I assume the application server must check the modified date at some point to decide if it needs to compile. If I have to use the underlying Java to get to it, that's fine (a pure CF approach would be better, of course, but I'm not holding out much hope).

If the modified date isn't available, then I'd settle for some sort of flag indicating if the current request triggered a recompile (actually, that might work just as well).


Solution

  • You are looking for coldfusion.runtime.TemplateClassLoader. It handles the lookup against the TemplateCache and either fetches an already compiled template class or invokes coldfusion.compiler.NeoTranslator to compile CFML into it.

    <cfset templateUri  = getCurrentTemplatePath()>
    <cfset lastCompiled = createObject("java", "coldfusion.runtime.TemplateClassLoader").getLastCompiledTime(templateUri)>
    <!--- lastCompiled = unix timestamp in milliseconds --->
    

    Needless to say, this is an implementation detail and you should not rely on it.