iprodev/php-easycache

一个简单且快速的脚本,用于在PHP中轻松缓存第三方API调用。

1.2.1 2016-02-13 00:20 UTC

This package is not auto-updated.

Last update: 2024-10-02 10:17:00 UTC


README

PHP EasyCache 类是一个简单且快速的方式来缓存第三方API调用,并支持无损数据压缩。

安装

通过 composer 安装

{
    "require": {
        "iprodev/php-easycache": "~1.2"
    }
}

运行 composer install 然后按常规使用

require 'vendor/autoload.php';
$cache = new iProDev\Util\EasyCache();

用法

一个非常基础的用法示例

$cache = new iProDev\Util\EasyCache();
$latest_tweet = $cache->get_data('tweet', 'http://search.twitter.com/search.atom?q=from:chawroka&rpp=1');
var_dump($latest_tweet);

一个更高级的示例

$cache = new iProDev\Util\EasyCache();
$cache->cache_path = 'cache/';
$cache->cache_time = 3600;

$data = $cache->get('identity_keyword');

if(!$data) {
	$data = $cache->get_contents('http://some.api.com/file.json');
	$cache->set('identity_keyword', $data);
}

var_dump($data);

一个压缩数据的示例

$cache = new iProDev\Util\EasyCache();
$cache->compress_level = 9;

$data = $cache->get_data('identity_keyword', 'http://some.api.com/file.json');

var_dump($data);

API 方法

$data = "Some data to cache";

// Call EasyCache
$cache = new iProDev\Util\EasyCache();

// SET a Data into Cache
$cache->set('identity_keyword', $data);

// GET a Data from Cache
$data = $cache->get('identity_keyword');

// Check that the data is cached
$is_cached = $cache->is_cached('identity_keyword');

// Get the Data from URL and cache it
$data = $cache->get_data('identity_keyword', 'http://search.twitter.com/search.atom?q=from:chawroka&rpp=1');

// Helper function for retrieving data from url without cache
$data = $cache->get_contents('http://search.twitter.com/search.atom?q=from:chawroka&rpp=1');

// REMOVE a Cache item
$cache->delete('identity_keyword');

// REMOVES all Cache expired items
$cache->flush_expired();

// REMOVES all Cache items
$cache->flush();

配置

// Call EasyCache
$cache = new iProDev\Util\EasyCache();

// Path to cache folder (with trailing /)
$cache->cache_path = 'cache/';

// Cache file extension
$cache->cache_extension = '.cache';

// Length of time to cache a file (in seconds)
$cache->cache_time = 3600;

// Lossless data compression level. 1 - 9; 0 to disable
$cache->compress_level = 0;

致谢

PHP EasyCache 由 Hemn Chawroka 创建,来自 iProDev。在MIT许可下发布。