【问题标题】:Implement caching for new Vimeo PHP API为新的 Vimeo PHP API 实现缓存
【发布时间】:2017-08-04 20:02:12
【问题描述】:

我有一个使用 vimeo-php-lib 编写的项目,该项目已被弃用,因此我已将其更改为新的 php 库。可以在https://github.com/vimeo/vimeo.php找到。

但是,旧代码已使用以下行启用缓存:

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

如何为新 API 实现它?它似乎没有包含在示例中。

【问题讨论】:

    标签: php vimeo


    【解决方案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);
            }
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-10-04
      • 2010-09-08
      • 1970-01-01
      • 1970-01-01
      • 2012-02-21
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 2021-02-16
      相关资源
      最近更新 更多