alexkratky/cachex

用于操作缓存的类。

v1.0.0 2020-05-12 11:03 UTC

This package is auto-updated.

Last update: 2024-09-12 20:31:20 UTC


README

用于操作缓存的类。

安装

composer require alexkratky/cachex

用法

require 'vendor/autoload.php';

use AlexKratky\Cache;
use AlexKratky\Logger;

Cache::setDirectory(__DIR__ . '/cache/');
Logger::setDirectory(__DIR__ . '/cache/');

Cache::save("test.json", array(
    "name" => "Alex"
));

与缓存一起工作

在 panx 框架中与缓存一起工作非常简单。如果您需要将一些变量保存到缓存中,可以通过调用 Cache::save($name, $data) 来实现,其中 $name 是变量的名称,$data 是其值。

保存数据后,您可以通过调用 Cache::get($name, $cacheTime) 来检索它们,其中 $name 是变量的名称,$cacheTime 是以秒为单位的时间。如果缓存中存储的数据早于这个限制,则将返回 false。第二个参数是可选的。如果您不传递任何值作为第二个参数,缓存类将使用默认值(10秒)。

Cache::get() 将返回数据或 false,如果变量未存储在缓存中或它太旧。

require 'vendor/autoload.php';

use AlexKratky\Cache;
use AlexKratky\Logger;

Cache::setDirectory(__DIR__ . '/cache/');
Logger::setDirectory(__DIR__ . '/cache/');

$c = Cache::get("user", 30);
if($c !== false) {
    var_dump($c);
}
$c_arr = array(
    "ID" => 1,
    "name" => "Alex",
    "email" => "example@example.com",
    "age" => 19,
    "admin" => true
);
Cache::save("user", $c_arr);
  • Cache::destroy(string $name): bool - 删除指定的缓存文件。
  • Cache::clearUnused($dir = null, $time = 86400) - 删除未使用的缓存文件(早于 $time)。必须从终端指定 $dir 参数(_DIR_)。
  • Cache::clearAll($dir = null) - 删除整个缓存目录。必须从终端指定 $dir 参数(_DIR_)。