Cfhttp with cachedwithin not working


#1

I stumbled across the factoid that you can add the cachedwithin attribute to your cfhttp requests. I’ve a somewhat large csv file that I’m accessing using the lucee version of cfhttp:

http cachedwithin="#createtimespan(0,1,0,0)#" url=“http://localhost/CBC/Assets/Account.csv” method=“get” delimiter="," firstrowasheaders=true textqualifier="""" name=“csv”;

I’ve also tried using the cfhttp function, with no success either.

Following the code above I follow up with loop query=‘csv’. It’s in this loop that I extract all the data I need. Problem I’m having is that after I add cachedwithin="#createtimespan(0,1,0,0)#" the code runs one time just fine, but on subsequent calls I get an error that states the csv query doesn’t exist.

Is this an error in Lucee, or do I need to add something to my http call, an attribute possibly, that will hold the cached version of the http call? I’ve been attempting to add the cachedwithin attribute for several versions of Lucee, all with the same outcome.

I’m currently running Lucee 5.2.6.6-SNAPSHOT. Again, I’ve been testing cachedwithin for several versions. I"m running Lucee on a mac mini, macOS High Sierra.


#2

@Hugh_Rainey I don’t have an answer, or a solution to the problem directly, but I do have a workaround that’ll get you going in the meantime:

// get the csv data from the standard object cache 
csvFile = cacheGet( '[some_descriptor]' );

// check if the data still exists in the cache
if( isNull( csvFile ) ) {
    // it doesn't, make the http call (w/o cachedWithin) to retrieve the file
    // note the name variable is set the same (csvFile)
    http url='[url]' [...] name='csvFile';
    // store the csv data in the standard object cache for one hour
    cachePut( '[some_descriptor]', csvFile, createTimeSpan( 0, 1, 0, 0 ) );
}

// process the csv data as normal

That said if you’re having troubles using cachedWithin with http I’d check for and then log a bug for it. I tend to shy away from cachedWithin myself and just use the object cache as my store so I’m in total control of it, but it ought to work either way for those that are using it.

HTH

– Denny


#3

@ddspringle – Thanks so much for the response! That’s a great workaround. I’ll look to see if the bug has been posted.

Hugh


#4

I agree with @ddspringle about opening a ticket if there is a bug, but for the meantime, another workaround would be to put the http call in a function and use cachedWithin there:

function getRemoteCsv(uri) cachedWithin=createtimespan(0,1,0,0) {

    http url=arguments.uri 
      method="get"
      delimiter="," 
      firstRowAsHeaders=true 
      textqualifier='"'
      name="local.csv";

    return csv;
}