jameslevi/nest

是一个简单的基于文件的PHP缓存库。

v1.0.2 2021-05-04 03:21 UTC

This package is not auto-updated.

Last update: 2024-10-02 02:21:29 UTC


README

是一个简单的基于文件的PHP缓存库。

特性

  1. 使用PHP的opcache来缓存静态数据。
  2. 创建多个缓存数据库。
  3. 易于与任何PHP框架集成或完全不使用框架使用。

安装

  1. 您可以通过composer安装。
composer require jameslevi/nest
  1. 如果不使用任何框架,请将以下代码粘贴到您的项目中以加载自动加载器。
require_once __DIR__.'/vendor/autoload.php';
  1. 将nest导入到您的项目中。
use Graphite\Component\Nest\Nest;
  1. 设置您的项目的默认存储路径。
Nest::setStoragePath(__DIR__."/storage/cache");
  1. 设置要使用的默认哈希算法。默认算法是"md5"。
Nest::setHashAlgorithm("md5");

基本示例

让我们尝试对数据库配置进行简单的缓存。

<?php

use Graphite\Component\Nest\Nest;

// Set the default storage path.
Nest::setStoragePath(__DIR__."/storage/cache");

// Set the default hash algorithm.
Nest::setHashAlgorithm("md5");

// Create a new nest database instance.
$db = new Nest("db");

// Add data to cache.
$db->add("host", "localhost");
$db->add("port", 8080);
$db->add("username", "root");
$db->add("password", "123");
$db->add("database", "users");
$db->add("charset", "utf-8");

// Generate or update the cache file.
$db->write();

这将生成以下内容的PHP文件。

<?php return array (
  '67b3dba8bc6778101892eb77249db32e' => 'localhost',
  '901555fb06e346cb065ceb9808dcfc25' => '3306',
  '14c4b06b824ec593239362517f538b29' => 'root',
  '5f4dcc3b5aa765d61d8327deb882cf99' => '123',
  '11e0eed8d3696c0a632f822df385ab3c' => 'users',
  'dbd153490a1c3720a970a611afc4371c' => 'utf-8',
);

入门

  1. 您可以使用"get"方法获取值。
$db->get("host"); // Returns "localhost".
  1. 您可以使用"add"方法添加新的键值。
// Array will be automatically converted into json string.
$db->add("tables", array(
    "user_logs", 
    "user_contacts", 
    "user_address"
)); 
  1. 您可以使用"set"方法更新键值。
$db->set("password", "abc"); // Change the value of password from "123" to "abc".
  1. 所有添加或更新的数据都将在您调用"write"方法之前仅保存。
$db->write();
  1. 您可以使用"has"方法检查键值是否存在。
$db->has("charset"); // Returns true because charset exists from our cache.
  1. 您可以使用"remove"方法删除键值。
$db->remove("port"); // This will delete port from our cache.
  1. 您可以使用"toArray"方法以数组的形式返回缓存数据。
$db->toArray();
  1. 您可以使用"toJson"方法以JSON格式返回缓存数据。
$db->toJson();

使用Nest Facade

  1. 您可以通过调用定义您的缓存数据库名称的静态方法来返回nest实例。
Nest::db(); // Is equal to "new Nest('db')"
  1. 您可以通过提供第一个参数来返回键值。
Nest::db('charset'); // Will return "utf-8".
  1. 您可以通过提供第二个参数来更新数据。
Nest::db('username', 'james')->write(); // Will change the value of username from "root" to "james".
  1. 您可以通过调用"add"方法来添加新数据。
Nest::db()->add('token', '1jds9ds93209sdds')->write();
  1. 您可以使用"remove"方法删除键值。
Nest::db()->remove('token')->write();

清除缓存

您可以使用"destroy"方法清除缓存数据库。

Nest::destroy('db');

您可以使用"destroyAll"方法清除所有缓存数据库。

Nest::destroyAll();

贡献

有关问题、疑虑和建议,您可以通过nerdlabenterprise@gmail.com给James Crisostomo发邮件。

许可

本软件包是开源软件,受MIT许可证的许可。