简体   繁体   中英

cache a feed or a finished page?

I just started using SimpleXML to get a feed and display data from that XML feed on one of my webpages. See my first post https://stackoverflow.com/questions/5925368/how-to-use-the-weather-gov-xml-feed-on-a-website I have a basic knowledge of PHP so I may be missing something basic.

If I understand what is happening correctly, every time someone looks at my page before it displays the php, the script first has to go and get the feed. Then it does what I have asked it to do with the feed and then displays the page.

I would think everything would be faster if I was to cache either the feed or formatted the feed and cache that. Which is better caching the raw feed or format the feed and cache the result? How do I go about caching either?

I am hoping that someone can point me in the direction of a tutorial that will teach me how to cache things with php or maybe someone has some example code that I could learn from and/or adapt for my project?

Thanks.

Google "simple php cache tutorial" or look at this one .

Instead of echo or print-ing text to the screen as you probably are now, build up a variable using the technique of string concatenation like so:

$html = '<h3>Weather View</h3>';

foreach( element in your xml feed){
$html .= 'Some more information';
}
//then when done
file_put_contents('weather_cache.txt', $html );

Essentially you ought to cache a segment of html, which you then use PHP to include at the correct place in your webpages, probably using file_get_contents();

The logic in the tutorial will tell you how to check the date of the cache and then decide to a) go and refresh the xml and recreate the cache or b) lift up and display the cached file

If you are using PHP for a personal website you probably can not install APC((opcode)cache)(way to go if you can install/use). My advice then would be to use something like redistogo (cache) which is free for small websites.

If I understand what is happening correctly, every time someone looks at my page before it displays the php, the script first has to go and get the feed. Then it does what I have asked it to do with the feed and then displays the page.

Like you are saying you should cache the feed(redis). The best way to do this is offline(this way user(s) don't have to wait for your script to fetch feed which takes a lot of time(relative)...) by using some sort of cronjob. There are a lot of free sites which over you cronjobs like for example http://www.onlinecronjobs.com/ .

I would think everything would be faster if I was to cache either the feed or formatted the feed and cache that. Which is better caching the raw feed or format the feed and cache the result? How do I go about caching either?

I think I would cache both formats. Because it would be hard to get the raw feed from the formatted feed if you needed it.

I would probably cache them using SETEX with the desired time to expire. get a new copy from the website when cache has expired. I used Predis to talk to redistogo in the past.

I am hoping that someone can point me in the direction of a tutorial that will teach me how to cache things with php or maybe someone has some example code that I could learn from and/or adapt for my project?

There are a lot of tutorials about using redis . For example designing a twitter-clone . If you do a proper google search you will find a lot of results.

I recommend you to cache content of the RSS feed.
There you can learn, how to use APC, which can be your opcode cacher and very fast data cacher.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM