dm1tra/cache-library

A simple caching library for PHP

dev-master 2025-02-11 20:04 UTC

This package is auto-updated.

Last update: 2025-06-11 20:48:25 UTC


README

Installation

composer require dm1tra/cache-library

Usage

File Backend

require 'vendor/autoload.php';

try {
    $cache = new CacheLibrary\Cache('file', ['cache_dir' => '/path/to/cache']);
    $data = $cache->get('my_key');
    if (!$data) {
        $data = performExpensiveOperation();
        $cache->set('my_key', $data, 3600);
    }
    useData($data);
} catch (\Exception $e) {
    echo "Error: " . $e->getMessage();
}

Redis Backend

require 'vendor/autoload.php';

try {
    $cache = new CacheLibrary\Cache('redis', ['host' => '127.0.0.1', 'port' => 6379]);
    $data = $cache->get('my_key');
    if (!$data) {
        $data = performExpensiveOperation();
        $cache->set('my_key', $data, 3600);
    }
    useData($data);
} catch (\Exception $e) {
    echo "Error: " . $e->getMessage();
}

Memcached Backend

require 'vendor/autoload.php';

try {
    $cache = new CacheLibrary\Cache('memcached', ['host' => '127.0.0.1', 'port' => 11211]);
    $data = $cache->get('my_key');
    if (!$data) {
        $data = performExpensiveOperation();
        $cache->set('my_key', $data, 3600);
    }
    useData($data);
} catch (\Exception $e) {
    echo "Error: " . $e->getMessage();
}

Methods

set(string $key, $value, int $ttl = 3600): bool Set a value in the cache with a specified time-to-live (TTL).

Parameters:

  • $key: The key of the item to store.
  • $value: The value to store.
  • $ttl: The time-to-live in seconds.

Returns : True on success, false on failure. get(string $key) Get a value from the cache. Parameters :

  • $key: The key of the item to retrieve. Returns : The value of the item or null if not found. has(string $key): bool Check if an item exists in the cache.

Parameters :

  • $key: The key of the item to check. Returns : True if the item exists, false otherwise. delete(string $key): bool Delete an item from the cache.

Parameters :

  • $key: The key of the item to delete. Returns : True on success, false on failure. clear(): bool Clear all items from the cache.
  • Returns : True on success, false on failure.