nolikein/cache

Create and manage cache content. Respecting the PHP PSR-6

1.0.1 2020-11-03 17:42 UTC

This package is auto-updated.

Last update: 2024-06-29 04:28:02 UTC


README

PHP Packagist version Packagist License Gitlab pipeline status

Description

This librairy is inspired from PHP PSR-6. It allows you to create cache content. You have to create Items object which contain data and registering them to an object named a Pool.

Installation

Composer from bash

composer require nolikein/cache ^1.0.0

Composer from Docker:

docker run --rm --interactive --tty -v $PWD:/app composer require nolikein/cache ^1.0.0

Usage

Level 1 - Basic using

use Nolikein\Cache\Item;
use Nolikein\Cache\Pool;

// Select a cache directory by creating its manager
$pool = new Pool(__DIR__ . '/go_to_your_cache_directory');

// Create a cache element with data and an expiration time
$element = new item($name='aSuperName', $content='The data of my cache element', $expireAt=(new \Datetime('tomorrow'))); // expire tomorrow

// Register an element:
$pool->save($element);

// Clear all cache content from the cache directory:
$pool->clear();

Level 2 - Control everything =D

use Nolikein\Cache\Item;
use Nolikein\Cache\Pool;

// Select a cache directory by creating its manager
$pool = new Pool(__DIR__ . '/go_to_your_cache_directory');

// Multiple mean to construct the object
$element = new item($name='aSuperName', $content='Hello world !',   $expireAt=(new \Datetime('tomorrow'))); // expire tomorrow
$element = new item($name='aSuperName', $content=150,               $expireAfter=3600); // expire after 3600 seconds
$element = new item($name='aSuperName', $content=$myObject,         $expireAfter=(new \DateInterval('PT10S'))); // expire after 10 seconds

// NOTE : You cannot set an anonymous class as a content. You are only able to set an object from a declared class.

// Methods of a cache item:
    // Gets its name
    $item->getKey();

    // Gets its content/data if the element has not expired
    $item->get();

    // Tells if the element has expired
    $item->isHit();

    // Set a new content/data
    $item->set($content);

    // Set an expiration time
        // From an object implementing \DatetimeInterface
        $item->expiresAt((new \Datetime('tomorrow')));

        // From an int (which represent the interval from now in seconds)
        $item->expiresAfter(3600);

        // From a \DateInterval object
        $item->expiresAfter((new \DateInterval('PT10S')));


// Register an element dynamically in the pool but not in a file:
$pool->saveDeferred($element);

// If you want to register all deferred/dynamic content in files:
$pool->commit();

// NOTE: the clear() method will delete ALL deferred/dynamic content AND cache files
$pool->clear();

Contributing

I have no method to give to you if you want to contribute. Just talk me as a human.

Licence

The project is under lincence MIT.