tiagogouvea / simple-php-cache
使用文件系统实现的简单 PHP 缓存类
Requires
- php: >=5.2.0
This package is not auto-updated.
Last update: 2024-09-28 16:46:36 UTC
README
从 cosenary/Simple-PHP-Cache 分支
关于
一个轻量级、简单但强大的 PHP5 缓存类,它使用文件系统进行缓存。
要求
- PHP 5.2.x 或更高版本
简介
基本上,缓存类将数据存储在 JSON 格式的文件中。如果您在 Cache 名称下存储数据,则将创建这些文件。
如果您使用 setCache()
设置新的 Cache 名称,则将生成新的缓存文件。Cache 将在新的文件中存储所有后续数据。设置方法允许您在不同的 Cache 文件之间切换。
快速开始
添加到您的 composer.json
{ "require": { "tiagogougea/simple-php-cache": "*" } }
然后运行 "composer update" 或直接运行 "composer require tiagogouvea/simple-php-cache"。
设置 Cache 类
设置 Cache 并不麻烦。
首先创建一个可写目录 cache/
并包含 Cache 类
<?php require_once 'cache.class.php'; // setup 'default' cache $c = new Cache(); ?>
现在我们已经设置了 Cache 实例,可以开始缓存了!
<?php // store a string $c->store('hello', 'Hello World!'); // generate a new cache file with the name 'newcache' $c->setCache('newcache'); // store an array $c->store('movies', array( 'description' => 'Movies on TV', 'action' => array( 'Tropic Thunder', 'Bad Boys', 'Crank' ) )); // get cached data by its key $result = $c->retrieve('movies'); // display the cached array echo '<pre>'; print_r($result); echo '<pre>'; // grab array entry $description = $result['description']; // switch back to the first cache $c->setCache('mycache'); // update entry by simply overwriting an existing key $c->store('hello', 'Hello everybody out there!'); // erase entry by its key $c->erase('hello'); ?>
您还可以利用 PHP5 中引入的方法链功能。
因此,您可以这样做
<?php $c->setCache('mycache') // generate new file ->store('hello', 'world') // store data string ->retrieve('hello'); // retrieve cached data ?>
可用方法
设置 Cache
new Cache(<数组>/<字符串>)
字符串
给您基本的设置。
这是您的 Cache 名称(标准 Cache 名称是 'default')
new Cache('YOUR-CACHE-NAME');
数组
允许您定义多个可选参数
new Cache(array(
'name' => 'YOUR-CACHE-NAME',
'path' => 'cache/',
'extension' => '.cache'
));
如果您在构造函数或 setCache()
方法中没有定义 Cache 名称,它将是 'default'。
存储数据
store($key, $data, <$expiration>)
key
值定义了一个与缓存的关联的标签。data
值可以是任何类型的对象(将被序列化)。expiration
值允许您定义过期时间。
要更改数据,您可以通过使用相同的键标识符来覆盖它。
除了数据外,Cache 还将存储一个时间戳。
一个示例 Cache 条目如下所示
{ "christmas": { "time": 1324664631, "expire": 28000, "data": "s:29:"A great time to bake cookies.";" // serialized } }
检索数据
retrieve($key, <$timestamp>)
通过其键获取特定的缓存数据。
要检索键的时间戳,将第二个参数设置为 true
。
retrieveAll(<$meta>)
这允许您一次性检索所有缓存数据。通过将 $meta
参数设置为 true
,您可以得到元数据。
删除数据
删除缓存数据有以下三种方法
erase($key)
通过其键删除单个条目。eraseAll()
从 Cache 文件中删除所有条目。eraseExpired()
删除所有已过期的条目。
<?php // returns the count of erased entries echo $c->eraseExpired() . ' expired items erased!'; ?>
检查缓存数据
isCached($key)
检查是否任何数据与给定的键相关联。
返回 true
或 false
。
设置 Cache 名称
setCache($name)
如果您想切换到另一个 Cache 或创建一个新的 Cache,请使用此方法设置新的 Cache 名称。
设置 Cache 路径
setCachePath($path)
Cache 文件夹的路径必须以反斜杠结尾:my_path_to_the_cache_folder/
获取 Cache 文件路径
getCacheDir()
此方法返回您当前 Cache 文件的路径(Cache 名称始终是 sh1 编码)
cache/7505d64a54e061b7acd54ccd58b49dc43500b635.cache
基准测试
如果您已经做了,请告诉我。
历史
即将推出:Simple Cache 2.0
内部“软缓存”的实现、哈希求和处理以及序列化的切换。感谢dariushha的贡献!
Simple Cache 1.6 - 2014年4月1日
update
更新文档。bug
修复了retrieveAll()
方法以反序列化数据。
Simple Cache 1.5 - 2014年1月1日
feature
添加了serialize
/unserialize
以存储任何类型的数据。
Simple Cache 1.4 - 2013年9月8日
bug
修复了store()
方法中加载文件两次的问题。bug
修复了retrieve()
方法 - 使其更安全(感谢dariushha)。
Simple Cache 1.3 - 2013年2月28日
update
更新了添加的retrieveAll()
方法的文档。feature
添加了retrieveAll()
方法(感谢rpnzl)。
Simple Cache 1.2 - 2012年5月9日
update
格式化代码。bug
修复了isCached()
方法,使其按预期工作(感谢TigerWolf)。
Simple Cache 1.1 - 2012年1月1日
change
扩展配置现在必须以点开头。feature
添加了store()
方法的过期处理。feature
添加了eraseExpired()
和eraseAll()
方法。feature
添加了确保可写目录存在的方法。
Simple Cache 1.0 - 2011年12月29日
release
首个公开版本。feature
添加了retrieve()
方法的带时间戳选项。
Simple Cache 0.9 - 2011年12月25日
update
在文档中添加了快速入门指南。feature
添加了方法链的可能性。bug
修复了构造函数配置字符串/数组处理。
Simple Cache 0.8 - 2011年12月24日
release
首个内部beta版本(已测试)。feature
添加了设置器和获取器方法。update
详细文档。
Simple Cache 0.5 - 2011年12月22日
release
首个内部alpha版本。update
简短文档。
致谢
版权所有 (c) 2011-2013 - 由Christian Metz编写 / @cosenary
在BSD许可证下发布。