Forum Moderators: coopster

Message Too Old, No Replies

How to cache an xml value

         

gt651

1:22 pm on Jul 8, 2009 (gmt 0)

10+ Year Member



I have a temperature device with built-in web server that returns the follow when I access its state.xml page:

<datavalues>
<units>F</units>
<sensor1temp>xx.x</sensor1temp>
<sensor2temp>xx.x</sensor2temp>
<sensor3temp>xx.x</sensor3temp>
<sensor4temp>75.7</sensor4temp>
<relay1state>0</relay1state>
<relay2state>0</relay2state>
</datavalues>

I would like to display the sensor4temp (75.7) on a web page but I don't want users to access the device every time they access the web page. Is there a way to read the device every 10 minutes or so (on the server side) and save the sensor4temp value in file such that my web page would get the value from the file, on the server, and not directly from the device?

Any ideas would be greatly apprciated.

Thanks Gary

[edited by: coopster at 5:45 pm (utc) on July 9, 2009]
[edit reason] no domain signatures please and thanks :-) [/edit]

andrewsmd

2:15 pm on Jul 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would recommend a database and stored procedure, displaying dynamic data like this is always easier when it's stored in a database. Do you have access to some kind of database? If so, let me know and I'll go into more detail. Otherwise, if you want to read the file on a time base with php, you would need to use a cron job.

gt651

2:44 pm on Jul 8, 2009 (gmt 0)

10+ Year Member



I host the site at HostGator and I think I can use MySQL but I know nothing about it.

I can also schedule sever-side cron jobs there. How involved would it be to do it this way?

Thanks Gary

andrewsmd

2:57 pm on Jul 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've always run php on windows, so I really don't know about cron jobs, I just know they exist. Where you don't host it, you will need to find out if they are linux or windows based. You would also need to find out if you can schedule cron jobs (which I doubt you can). Doing this through mysql would be rather involved, but it's going to be a pretty big job either way. Any good PHP developer should know how to use a database backend anyways, I suggest you start here. Post any questions you have and I'll walk you through the process.

gt651

3:06 pm on Jul 8, 2009 (gmt 0)

10+ Year Member



HostGator is linux based and I can schedule cron jobs there.

andrewsmd

3:09 pm on Jul 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well in that case, you could create the file with cron and just schedule that cron job every 10 minutes. Look for a linux or cron forum for that. Once you are creating the file, you can then access it through php and create the page every time the user requests the page.

gt651

3:26 pm on Jul 8, 2009 (gmt 0)

10+ Year Member



OK thanks I'll look into setting up the cron job.

What would the php code look like to display the temperature value on my HTML page?

andrewsmd

3:35 pm on Jul 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That depends on how you want it to work. Can you paste code of how you display the data now.

gt651

3:42 pm on Jul 8, 2009 (gmt 0)

10+ Year Member



It's half way down the right side of this page:

<snipped personal url, see Terms of Service and Forum Charter please>

[edited by: coopster at 5:46 pm (utc) on July 9, 2009]
[edit reason] removed url per TOS [webmasterworld.com] [/edit]

andrewsmd

3:48 pm on Jul 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's not what I meant. I meant the source code of how that is displayed. Is it static? Meaning do you hard code the temperature and have to change it everyday manually?

gt651

3:57 pm on Jul 8, 2009 (gmt 0)

10+ Year Member



It's not static. I'm using an example I got from the module maker and it accesses the module on every page load.

andrewsmd

4:01 pm on Jul 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Once again, could you paste the source code of how that works. I can't give you much more help until I see that.

gt651

5:11 pm on Jul 8, 2009 (gmt 0)

10+ Year Member



Let me back up and digest what you've said so far, I don't think talking about the same thing.

Thanks Gary

andrewsmd

5:31 pm on Jul 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you have a simple site that says
hello world
and this sites is www.somewhere.com/index.php
the source code for index.php would look something like this
<html>
<head><title>some page</title></head>
<body>
<?php
echo("hello world");
?>
</body>
</html>
How did you create the page? Did you use dreamweaver or frontpage or something like that?

gt651

6:33 pm on Jul 8, 2009 (gmt 0)

10+ Year Member



I just used notepad and HTML.

If you had "72.5" in a file, how would you display it in your above example?

[edited by: coopster at 5:50 pm (utc) on July 9, 2009]
[edit reason] removed url per TOS [webmasterworld.com] [/edit]

penders

12:06 pm on Jul 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I wouldn't have thought you would need a database or CRON job to do this - that sounds overly complex. You only need to save 1 value. Perhaps save/cache the XML (or even just sensor4temp) in a static file.

1. User accesses webpage.

2. Check timestamp of cache (your static XML/Temperature file)

3. If timestamp <= 10 mins ago then serve the value in your cache to the user (ie. Read your static file.)

4. If timestamp > 10 mins ago then request state.xml (as you do at the moment), save this (the output) in your cache and serve this value to the user.

CRON will be working away even when no one is accessing your site. If you were wanting to record the values every 10mins, regardless of who was accessing the site, then you would need CRON.

andrewsmd

1:33 pm on Jul 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's what I assumed, "Is there a way to read the device every 10 minutes or so" penders is right if you don't need to do it unless someone is accessing the site. I would reccomend going about it that way. But, I thought maybe because it was a temperature that you wanted it to go every 10 minutes no matter what.