ahead4/cache

轻量级的文件系统缓存系统

v1.0.11 2016-02-19 10:50 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:09:47 UTC


README

Build status

这是一个基于文件和目录的轻量级缓存系统。

用法

步骤 1: 安装它

通过 composer

{
    "require": {
        "gregwar/cache": "1.0.*"
    }
}

或通过克隆存储库

git clone https://github.com/Gregwar/Cache.git

或下载它

步骤 2: 设置权限

您需要您的 PHP 脚本可以访问缓存目录,例如,您可以创建一个模式为 777 的 cache 目录

mkdir cache
chmod 777 cache

步骤 3: 访问缓存

要访问缓存,您可以这样做

<?php

include('vendor/autoload.php'); // If using composer

use Gregwar\Cache\Cache;

$cache = new Cache;
$cache->setCacheDirectory('cache'); // This is the default

// If the cache exists, this will return it, else, the closure will be called
// to create this image
$data = $cache->getOrCreate('red-square.png', array(), function($filename) {
    $i = imagecreatetruecolor(100, 100);
    imagefill($i, 0, 0, 0xff0000);
    imagepng($i, $filename);
});

header('Content-type: image/png');
echo $data;

这将渲染一个红色方块。如果缓存文件(看起来像 `cache/r/e/d/-/s/red-square.png')存在,它将被读取,否则,将调用闭包以创建缓存文件。

API

您可以使用以下方法

  • setCacheDirectory($directory): 设置缓存目录(见下文)。
  • setActualCacheDirectory($directory): 设置实际缓存目录(见下文)。
  • exists($filename, $conditions = array()): 检查缓存中是否存在 $filename 文件,检查条件(见下文)。
  • check($filename, $conditions = array()): exists 的别名。
  • getCacheFile($filename, $actual = false, $mkdir = false): 获取缓存文件。如果 $actual 标志为 true,将返回实际缓存文件名(见下文),如果 $mkdir 标志为 true,将创建缓存文件目录树。
  • set($filename, $contents): 将内容写入 $filename 缓存文件。
  • write($filename, $contents): set 的别名。
  • get($filename, $conditions = array()): 如果 $filename 缓存文件存在,将返回内容,否则,将返回 NULL
  • setPrefixSize($prefixSize): 设置目录的前缀大小,默认为 5。例如,对于 helloworld.txt 的缓存文件,将是 'h/e/l/l/o/helloworld.txt'
  • setDirectoryMode($directoryMode): 设置创建目录时的目录模式,默认为 0755。不影响之前创建的任何目录。
  • getOrCreate($filename, $conditions = array(), $function, $file = false): 这将检查 $filename 缓存文件是否存在并验证 $conditions(见下文)。如果缓存文件是好的,它将返回其内容。否则,它将调用 $function,传递目标文件,此函数可以写入参数中的文件或只返回数据。然后,返回缓存数据。如果设置 $file 标志,将返回缓存文件名而不是文件数据。

注意:考虑使用哈希为 $filename 缓存文件,以避免特殊字符。

条件

您可以使用条件来管理缓存中的文件过期,有两种过期方式

  • 使用 max-age,以秒为单位,设置文件的最大年龄
  • 使用 younger-than,通过传递另一个文件,这将比较修改日期,并在给定文件比缓存文件更年轻时重新生成缓存。

例如,如果您想将文件转换为大写

<?php

use Gregwar\Cache\Cache;

$cache = new Cache;

$data = $cache->getOrCreate('uppercase.txt',
    array(
        'younger-than' => 'original.txt'
    ),
    function() {
        echo "Generating file...\n";
        return strtoupper(file_get_contents('original.txt'));
});

echo $data;

这将创建 uppercase.txt 缓存文件,通过将 original.txt 转换为大写,如果缓存文件不存在或如果 original.txt 文件比缓存文件更新。

例如

php uppercase.php  # Will generate the cache file
php uppercase.php  # Will not generate the cache file
touch original.txt # Sets the last modification time to now
php uppercase.php  # Will re-generate the cache file

缓存目录和实际缓存目录

在某些情况下,您可能需要获取缓存文件名。例如,如果您正在缓存图像,您可能希望提供一个类似于 cache/s/o/m/e/i/someimage.png 的字符串,以便将其放入 <img> 标签中。这可以通过将 $file 参数传递给 getOrCreate 函数并设置为 true,或者直接使用 getCacheFile 方法(见上文)来实现。

然而,您用户的可见 cache 目录并不与您想要访问的绝对路径相同。为了做到这一点,您可以设置缓存目录和实际缓存目录。

缓存目录是用户可见的前缀(例如:cache/s/o/m/e/i/someimage.png),而实际缓存目录是用于实际访问图像的前缀(例如:/var/www/somesite/cache/s/o/m/e/i/someimage.png)。这样,文件将通过绝对路径访问,返回的缓存文件将直接可用于用户的浏览器。

许可证

此存储库受 MIT 许可证的约束,请参阅 LICENCE 文件。