I'm parsing data from this RSS feed: http://rss.accuweather.com/rss/liveweather_rss.asp?metric=1&locCode=SAM|AR|AR005|MONTE%20MAIZ, The data there updates once every hour.
To parse it, I load the RSS at the initialization of my class and convert it into an usable object.
The problem with this is that every time the page loads, the RSS is parsed again, and the HTTP request delays the page loading.
I was thinkin开发者_JAVA技巧g of a cronjob to parse the RSS hourly, and then save the data in a JSON structure. But I'd really like to not depend on the cron, as not all shared hosting servers provide it (although they should).
Any other suggestions on how to do this?
You can use Zend_Cache. The server will load the feeds first time, but later calls will utilize the cache.
Actually, I've implemented what you have detailed:
private function _loadFeedEntries($url)
{
$cache = $this->_getCacheObj();
$md5 = md5($url);
if ($result = $cache->load("feed_{$md5}")) {
return $result;
}
$entries = array();
try {
$feed = @Zend_Feed::import($url);
foreach ($feed as $entry) {
$entries[] = $entry;
}
} catch (Exception $e) {
// ignore this feed.
}
$cache->save($entries, "feed_{$md5}");
return $entries;
}
private function _getCacheObj()
{
$frontendOptions = array(
'lifetime' => self::CACHE_LIFETIME,
'automatic_serialization' => true
);
$backendOptions = array(
'cache_dir' => self::CACHE_DIR
);
return Zend_Cache::factory(
'Core',
'File',
$frontendOptions,
$backendOptions
);
}
You still can make cronjob to call _loadFeedEntries(), which will cache the result. You can even make cronjob in your system to navigate to your site every CACHE_LIFETIME seconds, thus "keeping" the cache updated.
You could do (very) simple caching upon page request, to avoid relying on cron.
That is:
When a client requests your page, try loading the data from a cache file. If the cache file doesn't exist, or is too old, hit the service's RSS feed for an update and write it to a cache file.
If you write to the cache with
file_put_contents( FILENAME, json_encode( $some_data_structure ));
you can read it in with a simple
json_decode( file_get_contents( FILENAME ));
精彩评论