trafficinc / php-easycache
使用PHP缓存第三方API调用的简单方法。
v1.0
2017-12-31 18:12 UTC
Requires
- php: >=5.4.0
This package is not auto-updated.
Last update: 2024-09-20 02:11:39 UTC
README
使用PHP缓存第三方API调用的简单方法。
安装 - Composer
{ "require": { "trafficinc/php-easycache": "~1.0" } }
然后包含composer自动加载器
require 'vendor/autoload.php'; $cache = new Trafficinc\Util\EasyCache();
用法
$cache = new Trafficinc\Util\EasyCache(); $cache->cache_path = 'cache/'; $cache->cache_time = 3600; if($data = $cache->get_cache('label')){ $data = json_decode($data); } else { $data = $cache->do_request('https://jsonplaceholder.typicode.com/posts/1'); $cache->set_cache('label', $data); $data = json_decode($data); } print_r($data);
清除缓存
// clear single cache file by id $cache->clear('label'); // clear whole cache $cache->clear();