rothkj1022 / php-cache-class
基于Erik Giberti的FileCache类的一个简单的基于文件的缓存,由lodev09/php-cache-class分支而来。
2.1.5
2023-05-17 14:46 UTC
Requires
- php: ^5.2 || ^7.0 || ^8.0
- guzzlehttp/guzzle: ^7.2
README
基于Erik Giberti的FileCache类的一个简单的基于文件的缓存。请参见这里
增强功能
- 数据被序列化和JSON编码
- 缓存数据通过
mcrypt加密 - 基于文件的缓存在此进行说明
安装
在你的php项目命令行中运行以下命令
$ composer require rothkj1022/php-cache-class
完成。
你也可以手动编辑composer.json,然后执行composer update
"require": {
"rothkj1022/php-cache-class": "^2.1.0"
}
入门
使用composer的示例用法
//load composer packages require('vendor/autoload.php'); //create new instance of the class use rothkj1022\FileCache; $cache = new FileCache\FileCache("tmp/");
不使用composer的示例用法
//require the class require_once("lib/FileCache.php"); //create new instance of the class use rothkj1022\FileCache; $cache = new FileCache\FileCache("tmp/"); //...
本地文件源示例
$cache_key = "client_list"; //see if we can get an existing cache if (!$clients_data = $cache->get($cache_key)) { //nope. Let's get the real one! $clients_data = json_decode(file_get_contents("clients.json")); //set the cache up! $expire = 3600; //1 hour $cache->set($cache_key, $clients_data, $expire); } var_dump($clients_data);
外部http GET请求示例
$uri = 'https://raw.githubusercontent.com/bahamas10/css-color-names/master/css-color-names.json'; $remote_data = $cache->file_get_contents($uri); var_dump($remote_data);
参考
代码参考,帮助你开始使用!
属性
protected $root = '/tmp/';- 附加到缓存的前缀值,应该是目录的完整路径。protected $error = null;- 用于存储可能已引发的错误消息private $_encryption_key = 'Fil3C@ch33ncryptionK3y'- 主要用于加密的密钥(您需要在类内部设置此密钥)
方法
公共方法
Cache::get($key)- 读取由缓存键指定的缓存中的数据Cache::set($key [, $data, $ttl])- 将数据保存到缓存。任何评估为false、null、''、boolean false、0的值将不会被保存。$ttl指定过期时间Cache::delete($key)- 删除指定的$key的缓存Cache::get_error()- 读取并清除内部错误Cache::have_error()- 可用于检查内部错误
私有方法
请参阅代码以查看所有使用的私有方法,如Cache::_encrypt($pure_string)等。
变更日志
版本 2.1.3
- 修复:停止在屏幕上输出guzzle请求错误
版本 2.1.2
- 集成guzzle以更有效地进行http get请求
版本 2.1.1
- 更改:将类重命名为Erik Giberti的原始名称,FileCache
版本 2.1.0
- 添加:Composer集成
- 添加:变更日志
致谢
2010 - 由Erik Giberti编写 2011-2014 - 由Jovanni Lo / @lodev09 重写 2018 - 由Kevin Roth / @rothkj1022 修改