cosenary/simple-php-cache

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

dev-master 2015-01-03 18:28 UTC

This package is not auto-updated.

Last update: 2024-09-14 16:55: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(<数组>/<字符串>)

字符串 为您提供基本设置。
这是您的缓存名称(标准缓存名称为 '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年1月4日

  • 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 扩展配置现在必须以点开始。
  • featurestore() 方法中添加了过期处理。
  • feature 添加了 eraseExpired()eraseAll() 方法。
  • feature 添加了确保存在可写目录的方法。

Simple Cache 1.0 - 2011年12月29日

  • release 首个公开版本
  • featureretrieve() 方法中添加了时间戳选项。

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 许可证 下发布。