h-space/api-cacher-bundle

API 请求智能缓存器,用于性能优化

dev-master 2015-03-27 08:11 UTC

This package is not auto-updated.

Last update: 2024-10-02 07:48:20 UTC


README

ApiCacher 是一个用于 API 端点提供的数据的 symfony2 缓存系统。它用于优化使用外部 API 服务进行数据操作的网站的性能。

安装

Composer

"require": {
        "h-space/api-cacher-bundle": "dev-master"
    }
$ composer install

使用方法

包含库

    use HSpace\Bundle\ApiCacherBundle\Library\ApiCacher;

初始化类

     $cacher = new ApiCacher();

单次请求

     $url = 'https://example.com/api/endpoint?api_key=YOUR_API_KEY'; 
     $fields = null; // if post request, fields is an array with post data else null
     $json_decode = true;
     $rebuild_cache = true; // rebuild cache at the the end of the proccess, more details below
     $response = $cacher->request($url,$fields,$json_decode,$rebuild_cache);

多次请求

     $url_one = 'https://example.com/api/endpoint?api_key=YOUR_API_KEY'; 
     $url_two = 'https://example.com/api/endpoint_two?api_key=YOUR_API_KEY'; 
     $fields = null; // if post request, fields is an array with post data else null
     $json_decode = true;
     $rebuild_cache = true; // rebuild cache at the the end of the proccess, more details below
     $cacher->request_multi($url_one,$fields,$json_decode,$rebuild_cache);
     $cacher->request_multi($url_two,$fields,$json_decode,$rebuild_cache);
     $cacher->execute($json_decode);
     $responses = $cacher->multi_output();
 
 //Accessing the responses
    $url_one_response = $responses[$url_one];
    $url_two_response = $responses[$url_two];

清除缓存监听器

要清除所有缓存文件,请将 eraseCache=true $_GET 参数包含在您的请求中。

示例:http://yoursite.com/homepage?eraseCache=true

启用缓存文件的自动刷新

在您的 app/config/routing.yml 中导入资源

_hspace:
   resource: "@HSpaceApiCacherBundle/Resources/config/routing.yml"

在类/控制器的末端调用函数

        $route = $this->generateUrl('hspace_reload_cache',array(), true); // for controller usage, you have to generate the  url if you are outside of controller
        $cacher->teardown($route);
        

版本

1.0.0

许可证

MIT

自由软件,太棒了!