lufiipe/simplecache

Simple file system cache library for PHP

1.0.0 2025-03-28 16:39 UTC

This package is auto-updated.

Last update: 2025-03-29 15:37:43 UTC


README

Simple cache library for PHP based on PSR-16. It uses a hybrid system: fast in-memory storage for temporary data and file storage for persistent data.

Install

composer require lufiipe/simplecache

Usage

use LuFiipe\SimpleCache\Cache;

$cache = new Cache();
$cache->set('key', 'value');
$value = $cache->get('key');

Setup the cache

The Cache class accepts the following arguments:

  • string $cacheDir : Path to a writable folder for storing cache files. If not provided, default system 'tmp' folder will be used.
  • null|int|\DateInterval $defaultTtl : Default Time to Live in secondes. By defaults 3600 secondes.
use LuFiipe\SimpleCache\Cache;

$cache = new Cache(__DIR__ . '/cache', 1800);

// Equivalent to:
$ttl = \DateInterval::createFromDateString('30 minutes');
$cache = new Cache(__DIR__ . '/cache', $ttl);

Retrieving items from the cache

Cache::get()

The get() method retrieves items from the cache. If the item is not found, it returns null. Alternatively, you can provide a second argument to specify a default value to return when the item is missing.

use LuFiipe\SimpleCache\Cache;

$cache = new Cache();

$value = $cache->get('key');

$value = $cache->get('key', 'default');

Cache::getMultiple()

The getMultiple() method retrieves multiple cache items by their keys.

use LuFiipe\SimpleCache\Cache;

$cache = new Cache();
$value = $cache->getMultiple(['key1', 'key2', 'key3']);

Like the get() method, you may pass a second argument to specifying the default value you wish to be returned if the item doesn't exist.

use LuFiipe\SimpleCache\Cache;

$cache = new Cache();
$cache->set('key1', 'value');

$value = $cache->getMultiple(['key1', 'badkey'], 'default');

The above example will output:

array(2) {
  [key1] => string(5) "value",
  [badkey] => string(7) "default",
}

Storing items in the cache

Cache::set()

The set() method store items in the cache. If the storage time is not passed to the set() method, the item will be stored using the cache instance's default TTL (by defaults 3600 secondes). Instead of passing the number of seconds as an integer, you may also pass a DateInterval instance representing the desired expiration time of the cached item.

use LuFiipe\SimpleCache\Cache;

$cache = new Cache();
$cache->set('key', 'value');

$cache->set('key', 'value', 28800);
$cache->set('key', 'value', new \DateInterval('PT8H'))

Cache::setMultiple()

setMultiple() is similar to set(), but instead of a single key/value item, it works on multiple items. The expiration time applies to all the items at once.

use LuFiipe\SimpleCache\Cache;

$cache = new Cache();
$cache->setMultiple([
    'key1' => 'value1',
    'key2' => 'value2',
]);

$cache->set('key', 'value', 28800);
$cache->set('key', 'value', new \DateInterval('PT8H'));

Removing items from the cache

Cache::delete()

You can delete items from the cache using the delete() method.

use LuFiipe\SimpleCache\Cache;

$cache = new Cache();

$cache->delete('key1');

Cache::deleteMultiple()

The deleteMultiple() method removes multiple cache items at once using a list of keys.

use LuFiipe\SimpleCache\Cache;

$cache = new Cache();

$cache->deleteMultiple(['key1', 'key2']);

Cache::clear()

You can clear the entire cache with the clear() method.

use LuFiipe\SimpleCache\Cache;

$cache = new Cache();

$cache->clear();

Checking if an item exists

The has() method checks whether an item exists in the cache. If the item is not found, it will return false.

use LuFiipe\SimpleCache\Cache;

$cache = new Cache();

if ($cache->has('key')) {
    // ...
}

Tests

By default, during testing, the cache uses the OS temporary files directory. To use a specific directory, copy the file phpunit.xml.dist to phpunit.xml and update the value of the CACHE_DIR variable.

Then, run:

php vendor/bin/phpunit

License

The MIT License (MIT). Please see License File for more information.