Services_ExchangeRates
[ class tree: Services_ExchangeRates ] [ index: Services_ExchangeRates ] [ all elements ]

Source for file HTTP_Cached.php

Documentation is available at HTTP_Cached.php

  1. <?php
  2. /**
  3.  * Cache_Lite is needed to cache the feeds
  4.  */
  5. require_once 'Cache/Lite.php';
  6. include_once 'HTTP/Request.php';
  7.  
  8. class Services_ExchangeRates_Transport_HTTP_Cached {
  9.     var $cache;
  10.  
  11.     function Services_ExchangeRates_Transport_HTTP_Cached($cache$request{
  12.         if (!is_object($cache)) {
  13.             $cache = new Cache_Lite();
  14.         }
  15.  
  16.         if (!is_object($request)) {
  17.             $request = new HTTP_Request();
  18.         }
  19.  
  20.         $this->cache $cache;
  21.         $this->request $request;
  22.     }
  23.  
  24.    /**
  25.     * Retrieves data from cache, if it's there.  If it is, but it's expired,
  26.     * it performs a conditional GET to see if the data is updated.  If it
  27.     * isn't, it down updates the modification time of the cache file and
  28.     * returns the data.  If the cache is not there, or the remote file has been
  29.     * modified, it is downloaded and cached.
  30.     *
  31.     * @param string URL of remote file to retrieve
  32.     * @return string File contents
  33.     */
  34.     function fetch($url{
  35.         $cacheID md5($url);
  36.                                         
  37.         if ($data $this->cache->get($cacheID)) {
  38.             return $data;
  39.         }
  40.                
  41.         // if $cache->get($cacheID) found the file, but it was expired, 
  42.         // $cache->_file will exist 
  43.         if (isset($this->cache->_file&& file_exists($this->cache->_file)) {
  44.             $this->request->addHeader('If-Modified-Since'gmdate("D, d M Y H:i:s"filemtime($this->cache->_file)) ." GMT");
  45.         }
  46.         
  47.         $this->request->sendRequest();
  48.         
  49.         if (!($this->request->getResponseCode(== 304)) {
  50.             // data is changed, so save it to cache
  51.             $data $this->request->getResponseBody();
  52.             $this->cache->save($data$cacheID);
  53.  
  54.             return $data;
  55.         }
  56.  
  57.         // retrieve the data, since the first time we did this failed
  58.         if ($data $this->cache->get($cacheID'default'true)) {
  59.             return $data;
  60.         }
  61.         
  62.         Services_ExchangeRates::raiseError("Unable to retrieve file ${url} (unknown reason)"SERVICES_EXCHANGERATES_ERROR_RETRIEVAL_FAILED);
  63.  
  64.         return false;
  65.     }
  66. }

Documentation generated on Mon, 11 Mar 2019 15:28:43 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.