alirezaashrafi/php-file-cache

基于文件系统的快速键值缓存系统(无需像Redis或Memcache这样的数据库)

v2.0 2022-03-20 21:23 UTC

This package is auto-updated.

Last update: 2024-09-21 02:58:13 UTC


README

基于文件系统的快速键值缓存系统(无需像Redis或Memcache这样的数据库)

安装

本项目使用composer。

$ composer require alirezaashrafi/php-file-cache

用法

安装后,您可以在项目中使用 'Cache' 类

use AlirezaAshrafi\Cache;

如果已存在名为 'Cache' 的类,您可以使用 AS 来重命名引入的类

use AlirezaAshrafi\Cache as FileCache;
  • 初始化参数是目录和文件名
  • 第一个参数是缓存目录的绝对路径
  • 第二个参数是要保存结果的文件
$directory = __DIR__ . '/cache-dir';
$file = 'products';

$cache = new Cache($directory , $file);

// OR

$cache = new Cache(__DIR__ . '/cache-dir' , 'products');
  • 目录层次结构(path/cache-dir/products)

设置

$cache->set("product-1-name", "Iphone 13");
$cache->set("product-1-description", "Iphone 13 pro max 256G");

获取

$product_name = $cache->get("product-1-name", "default value"); // "Iphone 13"
$product_name = $cache->get("product-2-name", "default value"); // "default value"
  • 如果缓存存在,将返回值("Iphone 13 pro max")
  • 如果没有缓存存在,将返回默认值("默认值")

存在性检查

$cache->has("product-1-name"); // TRUE
$cache->has("product-2-name"); // FALSE
  • 存在性检查函数检查文件和内存中缓存的存在性
  • 存在性检查函数返回值为真或假

删除

$cache->delete("product-1-description"); // TRUE

获取所有

$cache->getAll();

清空(删除所有)

$cache->purgeAll();