Is a simple file-based PHP caching library.

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

This package is not auto-updated.

Last update: 2024-05-01 00:34:38 UTC


README

Is a simple file-based PHP caching library.

Features

  1. Uses PHP's opcache to cache static data.
  2. Create multiple cache databases.
  3. Easy integration with any PHP framework or use with no framework at all.

Installation

  1. You can install via composer.
composer require jameslevi/nest
  1. If not using any framework, paste the following code to load the autoloader in your project.
require_once __DIR__.'/vendor/autoload.php';
  1. Import nest into your project.
use Graphite\Component\Nest\Nest;
  1. Set the default storage path for your project.
Nest::setStoragePath(__DIR__."/storage/cache");
  1. Set the default hash algorithm to use. The default algorithm is "md5".
Nest::setHashAlgorithm("md5");

Basic Example

Let us try a simple caching for database configuration.

<?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();

This will generate a PHP file with the following content.

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

Getting Started

  1. You can get values using the "get" method.
$db->get("host"); // Returns "localhost".
  1. You can add new key-value using "add" method.
// Array will be automatically converted into json string.
$db->add("tables", array(
    "user_logs", 
    "user_contacts", 
    "user_address"
)); 
  1. You can update key values using "set" method.
$db->set("password", "abc"); // Change the value of password from "123" to "abc".
  1. All added or updated data will be only saved unless you call the "write" method.
$db->write();
  1. You can check if a key-value exists using "has" method.
$db->has("charset"); // Returns true because charset exists from our cache.
  1. You can remove a key-value using "remove" method.
$db->remove("port"); // This will delete port from our cache.
  1. You can return cache data as array using "toArray" method.
$db->toArray();
  1. You can return json formatted cache data using "toJson" method.
$db->toJson();

Using Nest Facade

  1. You can return nest instance by calling a static method that defines the name of your cache database.
Nest::db(); // Is equal to "new Nest('db')"
  1. You can return key-value by providing the first argument.
Nest::db('charset'); // Will return "utf-8".
  1. You can update data by providingthe second argument.
Nest::db('username', 'james')->write(); // Will change the value of username from "root" to "james".
  1. You can add new data by calling "add" method.
Nest::db()->add('token', '1jds9ds93209sdds')->write();
  1. You can remove key-value using "remove" method.
Nest::db()->remove('token')->write();

Clear Cache

You can clear a cache database using "destroy" method.

Nest::destroy('db');

You can clear all your cache database using "destroyAll" method.

Nest::destroyAll();

Contribution

For issues, concerns and suggestions, you can email James Crisostomo via nerdlabenterprise@gmail.com.

License

This package is an open-sourced software licensed under MIT License.