jameslevi / nest
Is a simple file-based PHP caching library.
v1.0.2
2021-05-04 03:21 UTC
Requires
- php: >=5.3.0
- jameslevi/objectify: ^1.0
- jameslevi/string: ^1.0
This package is not auto-updated.
Last update: 2025-04-30 05:00:32 UTC
README
Is a simple file-based PHP caching library.
Features
- Uses PHP's opcache to cache static data.
- Create multiple cache databases.
- Easy integration with any PHP framework or use with no framework at all.
Installation
- You can install via composer.
composer require jameslevi/nest
- If not using any framework, paste the following code to load the autoloader in your project.
require_once __DIR__.'/vendor/autoload.php';
- Import nest into your project.
use Graphite\Component\Nest\Nest;
- Set the default storage path for your project.
Nest::setStoragePath(__DIR__."/storage/cache");
- 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
- You can get values using the "get" method.
$db->get("host"); // Returns "localhost".
- 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" ));
- You can update key values using "set" method.
$db->set("password", "abc"); // Change the value of password from "123" to "abc".
- All added or updated data will be only saved unless you call the "write" method.
$db->write();
- You can check if a key-value exists using "has" method.
$db->has("charset"); // Returns true because charset exists from our cache.
- You can remove a key-value using "remove" method.
$db->remove("port"); // This will delete port from our cache.
- You can return cache data as array using "toArray" method.
$db->toArray();
- You can return json formatted cache data using "toJson" method.
$db->toJson();
Using Nest Facade
- 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')"
- You can return key-value by providing the first argument.
Nest::db('charset'); // Will return "utf-8".
- You can update data by providingthe second argument.
Nest::db('username', 'james')->write(); // Will change the value of username from "root" to "james".
- You can add new data by calling "add" method.
Nest::db()->add('token', '1jds9ds93209sdds')->write();
- 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.