winnipass / simple-php-cache
一个轻量级、简单但功能强大的PHP5缓存类,使用文件系统进行缓存。
Requires
- php: >=5.2.0
- ext-curl: *
This package is not auto-updated.
Last update: 2024-09-28 20:30:31 UTC
README
关于
一个轻量级、简单但功能强大的PHP5缓存类,使用文件系统进行缓存。
您的反馈始终受到欢迎。
要求
- PHP 5.2.x 或更高版本
简介
基本上,缓存类以JSON格式将数据存储在文件中。如果您在缓存名称下存储数据,将会创建这些文件。
如果您使用 setCache()
设置新的缓存名称,将生成一个新的缓存文件。缓存将把所有后续数据存储在新文件中。设置方法允许您在不同缓存文件之间切换。
快速开始
设置缓存类
设置缓存并不麻烦。
首先创建一个可写目录 cache/
并包含缓存类
<?php require_once 'cache.class.php'; // setup 'default' cache $c = new 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 ?>
可用方法
设置缓存
new Cache(<数组>/<字符串>)
string
提供基本设置。
这是您的缓存名称(标准缓存名称是 'default')
new Cache('YOUR-CACHE-NAME');
array
允许您定义多个可选参数
new Cache(array(
'name' => 'YOUR-CACHE-NAME',
'path' => 'cache/',
'extension' => '.cache'
));
如果您在构造函数或 setCache()
方法中未定义缓存名称,它将是 'default'。
存储数据
store($key, $data, <$expiration>)
key
值定义了一个与缓存数据相关联的标签。data
值可以是任何类型的对象(将被序列化)。expiration
值允许您定义过期时间。
要更改数据,您可以通过使用相同的键标识符来覆盖它。
除了数据外,缓存还会存储一个时间戳。
一个示例缓存条目如下所示
{ "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()
从缓存文件中删除所有条目。eraseExpired()
删除所有过期条目。
<?php // returns the count of erased entries echo $c->eraseExpired() . ' expired items erased!'; ?>
检查缓存数据
isCached($key)
检查是否与给定的键相关联了任何数据。
返回 true
或 false
。
设置缓存名称
setCache($name)
如果您想切换到另一个缓存或创建一个新的缓存,请使用此方法设置新的缓存名称。
设置缓存路径
setCachePath($path)
缓存文件夹的路径必须以反斜杠结尾: my_path_to_the_cache_folder/
获取缓存文件路径
getCacheDir()
此方法返回当前缓存文件的路径(缓存名称始终是sh1编码)
cache/7505d64a54e061b7acd54ccd58b49dc43500b635.cache
基准测试
如果您已经做过,请让我知道。
历史
即将推出:Simple Cache 2.0
实现了内部“软缓存”,哈希处理和切换到序列化。感谢 @dariushha 的贡献!
Simple Cache 1.6 - 04/01/2014
update
更新文档。bug
已修复retrieveAll()
方法以反序列化数据。
Simple Cache 1.5 - 2014年01月01日
feature
添加了serialize
/unserialize
以存储任何类型的数据。
Simple Cache 1.4 - 2013年08月09日
bug
已修复在store()
方法中两次加载文件的问题。bug
已修复retrieve()
方法 - 使其更安全(感谢 @dariushha)。
Simple Cache 1.3 - 2013年02月28日
update
更新了有关添加的retrieveAll()
方法的文档。feature
添加了retrieveAll()
方法(感谢 @rpnzl)。
Simple Cache 1.2 - 2012年05月09日
update
格式化代码bug
已修复isCached()
方法,使其按预期工作(感谢 @TigerWolf)。
Simple Cache 1.1 - 2012年01月01日
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 许可证 下发布。