2017-08-04 84 views
1

我有一个用vimeo-php-lib编写的项目已被弃用,所以我已将它更改为新的php库。为新的Vimeo PHP API实现缓存

$api->enableCache(phpVimeo::CACHE_FILE, realpath(dirname(APPLICATION_PATH) . DIRECTORY_SEPARATOR .'data' .DIRECTORY_SEPARATOR. 'cache' .DIRECTORY_SEPARATOR. 'vimeo'), 3600); 

怎样去实施它的新API:可在https://github.com/vimeo/vimeo.php

然而,旧代码已缓存中找到使用下面的线启用?它似乎没有包含在例子中。

回答

1

我编辑了当前的Vimeo.php库,以便它将缓存结果,它当前每24小时过期。

_request函数被修改,并且添加了两个更多的函数。

const CACHE_FILE = 'file'; 
private $_cache_enabled = 'file'; 
private $_cache_dir = ''; 

/** 
* Internal function to handle requests, both authenticated and by the upload function. 
* 
* @param string $url 
* @param array $curl_opts 
* @return array 
*/ 
private function _request($url, $curl_opts = array()) 
{ 
    // Returned cached value 
    if ($this->_cache_enabled && ($response = $this->_getCached($url))) { 
     return $response; 
    } 

    // Merge the options (custom options take precedence). 
    $curl_opts = $this->_curl_opts + $curl_opts + $this->CURL_DEFAULTS; 

    // Call the API. 
    $curl = curl_init($url); 
    curl_setopt_array($curl, $curl_opts); 
    $response = curl_exec($curl); 
    $curl_info = curl_getinfo($curl); 

    if (isset($curl_info['http_code']) && $curl_info['http_code'] === 0) { 
     $curl_error = curl_error($curl); 
     $curl_error = !empty($curl_error) ? '[' . $curl_error .']' : ''; 

     throw new VimeoRequestException('Unable to complete request.' . $curl_error); 
    } 

    curl_close($curl); 

    // Retrieve the info 
    $header_size = $curl_info['header_size']; 
    $headers = substr($response, 0, $header_size); 
    $body = substr($response, $header_size); 

    $final_response = array(
     'body' => $body, 
     'status' => $curl_info['http_code'], 
     'headers' => self::parse_headers($headers) 
    ); 

    if ($this->_cache_enabled) { 
     $this->_cache($url, $final_response); 
    } 

    // Return it raw. 
    return $final_response; 
} 


/** 
* Cache a response. 
* 
* @param string $url The parameters for the response. 
* @param string $response The serialized response data. 
*/ 
private function _cache($url, $response) 
{ 
    $hash = md5(serialize($url)); 
    $response = json_encode($response); 

    if ($this->_cache_enabled == self::CACHE_FILE) { 
      $file = $this->_cache_dir.'/'.$hash.'.cache'; 
     if (file_exists($file)) { 
      unlink($file); 
     } 
     return file_put_contents($file, $response); 
    } 
} 

/** 
* Get the unserialized contents of the cached request. 
* 
* @param array $url The full list of api parameters for the request. 
*/ 
private function _getCached($url) 
{ 
    $hash = md5(serialize($url)); 
    $expire = 86400; 

    if ($this->_cache_enabled == self::CACHE_FILE) { 
     $file = $this->_cache_dir.'/'.$hash.'.cache'; 

     if (file_exists($file)) { 
      $last_modified = filemtime($file); 
      if (substr($file, -6) == '.cache' && ($last_modified + $expire) < time()) { 
       unlink($file); 
      } 
      } 

     if (file_exists($file)) { 
      return json_decode(file_get_contents($file), true); 
     } 

    } 
}