Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Caching a CFHTTP feed into the Application Scope of the Server

New Here ,
Jan 09, 2008 Jan 09, 2008
Hi

I have the following CFHTTP tag script, how do I check that it is caching this data correctly into the Application scope of the server?

So that users do not have to access the Internet site to get the feed as the CFHTTP caches the data within the 500 minute timeframe set?

This is my Application.cfm page

----
<CFAPPLICATION NAME="application">

<cfif
not structKeyExists(Application, "TimeStamp")
or
(dateDiff("n", Application.TimeStamp, now()) gt 500)>

<cfset Application.TimeStamp = now() />

<cfhttp url=" http://feeds.bbc.co.uk/weather/feeds/rss/5day/id/3314.xml" method="GET" resolveurl="No"></cfhttp>
<cfset Application.weather_xml = xmlParse(cfhttp.FileContent) />

</cfif>

-------------

This is the code to output the feed.

<cfoutput>
<img src="#application.weather_xml.rss.channel.image.url.xmlText#" alt="#application.weather_xml.rss.channel.item.DESCRIPTION.xmlText#" width="70px" height="70px">

<br>

<cfloop index="x" from="1" to="#ArrayLen(application.weather_xml.rss.channel.item)#">

#replace(application.weather_xml.rss.channel.item.title.xmlText, ',', '<br>', 'ALL')#

</cfloop>
</cfoutput>
TOPICS
Advanced techniques
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 09, 2008 Jan 09, 2008
I love your application. It deserves a page of its own. Every page of your site starts by including the Application.cfm code. I am assuming that you would not want the weather code to run on every page. Also, you should beef up your cfapplication tag a little. Here is an idea. The second test answers your question about caching, and says more besides.



Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 09, 2008 Jan 09, 2008
Thanks.

If I try to run the code on a computer with internet access the CFDUMP works.

However if I try and run the code on a computer with no internet access I get the following error

Element WEATHER_XML.RSS.CHANNEL.IMAGE.URL.XMLTEXT is undefined in APPLICATION.

Why is this as the rss data should be stored in the server application memory ?

As I do not want users to go out to this Internet site to retrieve the RSS feed it should be done by the Coldfusion server and then the RSS data displayed on the users browser
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 09, 2008 Jan 09, 2008
My first instinct is that it has less to do with cfdump and more to do with the broken connection. When you cut internet access the img tag can no longer connect to the URL http://www.bbc.co.uk/weather/images/symbols/animated_sym/10.gif. That URL is the value of the parameter application.weather_xml.rss.channel.image.url.xmlText, which then becomes undefined.

One way to get it to work is to copy the image http://www.bbc.co.uk/weather/images/symbols/animated_sym/10.gif to Coldfusion's root directory, for example. Then use a local URL, thus:

<img src=" http://127.0.0.1:8500/images/10.gif" alt="#application.weather_xml.rss.channel.item.DESCRIPTION.xmlText#" width="70px" height="70px">

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 09, 2008 Jan 09, 2008
If I copy all of the images to the root directory of the Coldfusion server, how would it know what image to display ?

Because at present the string looks like

<img src="#application.weather_xml.rss.channel.image.url.xmlText#" alt="#application.weather_xml.rss.channel.item.DESCRIPTION.xmlText#" width="70px" height="70px">

What would I put in <img src=" If I used <img src=" http://127.0.0.1:8500/images/10.gif" then I am hardcoding the image into the page?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 09, 2008 Jan 09, 2008
If I used <img src=" http://127.0.0.1:8500/images/10.gif" then I am hardcoding the image into the page?

That is true. I should have been clearer. I used the local URL only to show that the undefined variable was caused by the broken connection.

You could extend the idea and cache the image as well. Here is a thought:



Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 09, 2008 Jan 09, 2008
Could you use a regular expression on the

#application.weather_xml.rss.channel.image.url.xmlText# string

to remove the

http://www.bbc.co.uk/weather/images/symbols/animated_sym/

to leave just the weather icon .gif (i.e. 10.gif) part to be returned and then display the image locally as I would have saved all the images locally to the CF server.

i.e.

http://127.0.0.1:8500/images/#application.weather_xml.rss.channel.image.url.xmlText#

If so any idea on what I need to do in the regular expression to remove

http://www.bbc.co.uk/weather/images/symbols/animated_sym/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 09, 2008 Jan 09, 2008
I think the part to remove is http://127.0.0.1:8500 not http://www.bbc.co.uk/weather/images/symbols/animated_sym/. Without the BBC's full URL you cannot download the image in the first place. If you use the file names above and the following structure from the webroot

/Application.cfm
/bbcWeatherFeed.cfm
/images/10.gif

then, in the file bbcWeatherFeed.cfm, you can use a relative URL in the img tag, thus

<img src="/images/10.gif" alt="#application.weather_xml.rss.channel.item.DESCRIPTION.xmlText#" width="70px" height="70px">


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 09, 2008 Jan 09, 2008
Got it, the following seems to work fine

#REReplace(application.weather_xml.rss.channel.image.url.xmlText, " http://www.bbc.co.uk/weather/images/symbols/animated_sym/", "", "All")#

which just leaves me with 10.gif, 11.gif etc
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 11, 2008 Jan 11, 2008
I am getting the following error Intermittently on the page, (sometimes the page works fine for hours other times it is much less than that -any ideas ?

-------ERROR------
The system has attempted to use an undefined value, which usually indicates a programming error, either in your code or some system code.
Null Pointers are another name for undefined values.
---------------------

If I change the value of the line

<cfif not structKeyExists(Application, "TimeStamp") or (dateDiff("n", Application.TimeStamp, now()) gt 500)>

to another number i.e. gt 400 and save the page it works fine again ?




Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 11, 2008 Jan 11, 2008
What is the value of your application's timeout? Less than 500 minutes by any chance?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 11, 2008 Jan 11, 2008
Its set to 1 day as shown below

<cfapplication name = "Application"
applicationTimeout = "#createTimespan(1,0,0,0)#"
sessionManagement = "yes"
sessionTimeout = "#createTimeSpan(0,0,20,0)#"
setClientCookies = "true">
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 11, 2008 Jan 11, 2008
I would copy the following code block to Application.cfm so that every request includes it.

<cfif not structKeyExists(Application, "TimeStamp") or (dateDiff("n", Application.TimeStamp, now()) gt 500)>
... etc., etc.
</cfif>



Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 22, 2008 Jan 22, 2008
I have been trying this, but I am still getting the following intermittant error

The web site you are accessing has experienced an unexpected error.
Please contact the website administrator.

The following information is meant for the website developer for debugging purposes.

Error Occurred While Processing Request
The system has attempted to use an undefined value, which usually indicates a programming error, either in your code or some system code.
Null Pointers are another name for undefined values.

My application code is

<cfapplication name = "Application"
applicationTimeout = "#createTimespan(0,5,0,0)#"
sessionManagement = "yes"
sessionTimeout = "#createTimeSpan(0,0,20,0)#"
setClientCookies = "true">

<cfif not structKeyExists(Application, "TimeStamp") or (dateDiff("n", Application.TimeStamp, now()) gt 300)>
<cfset Application.TimeStamp = now() />
<cfhttp url=" http://feeds.bbc.co.uk/weather/feeds/rss/5day/id/3314.xml" method="GET" resolveurl="No"></cfhttp>
<cfset Application.weather_xml = xmlParse(cfhttp.FileContent) />
</cfif>

Any ideas on why I am getting the error ? Is it to do with the feed the code or the CF server ?

I tried using CFTRY / CATCH around the code so that if the feed is not displaying correctly instead of showing the error it would show the text 'Feed is currently unavailable' but have not got this working
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 22, 2008 Jan 22, 2008
LATEST
I wonder whether the application times out. What happens when you change attribute values to name = "App" and applicationTimeout = "#createTimespan(1,0,0,0)#"?


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources