rl404/simple-php-cache

一个轻量级、简单但强大的PHP5缓存类,使用文件系统进行缓存。

v1.6.1 2018-11-09 06:22 UTC

This package is not auto-updated.

Last update: 2024-09-22 06:53:24 UTC


README

原始版本分支而来。

Simple PHP Cache

关于

一个轻量级、简单但强大的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(<数组>/<字符串>)

字符串 给您提供基本设置。这是您的缓存名称(标准缓存名称为 'default'

new Cache('YOUR-CACHE-NAME');

数组 允许您定义多个可选参数

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)

检查是否与给定键关联了任何数据。返回 truefalse

设置缓存名称

setCache($name)

如果您想切换到另一个缓存或创建一个新的缓存,请使用此方法设置新的缓存名称。

设置缓存路径

setCachePath($path)

缓存文件夹的路径必须以反斜杠结尾: my_path_to_the_cache_folder/

获取缓存文件路径

getCacheDir()

该方法返回当前缓存文件的路径(缓存名称始终是 sh1 编码的)

cache/7505d64a54e061b7acd54ccd58b49dc43500b635.cache

基准测试

如果您已经做过基准测试,请告诉我。

历史

即将推出:Simple Cache 2.0 实现内部“软缓存”,哈希总和处理和转换为序列化。感谢 @dariushha 的贡献!

Simple Cache 1.6 - 2014年4月1日

  • 更新 更新文档。
  • 错误 修复了 retrieveAll() 方法以反序列化数据。

Simple Cache 1.5 - 2014年1月1日

  • feature 增加了 serialize / unserialize 功能以存储任何类型的数据。

Simple Cache 1.4 - 2013年8月9日

  • 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 首个内部测试版本
  • feature 添加了设置器和获取器方法
  • update 详细文档

Simple Cache 0.5 - 2011年12月22日

  • release 首个内部测试版本
  • update 简短文档

致谢

版权所有 (c) 2011-2013 - 由 Christian Metz 编程 / @cosenary 发布,遵循 BSD 许可协议