Search by

juanchosl / simplecache

Juanchosl

A small collection of read/write for multiples cache systems using PSR-16

Package info

github.com/JuanchoSL/SimpleCache

pkg:composer/juanchosl/simplecache

Statistics

Installs: 167

Dependents: 0

Suggesters: 0

Stars: 1

1.0.8 2026-08-30 20:25 UTC

This package is auto-updated.

Last update: 2026-08-30 20:27:13 UTC


README

Description

A small collection of read/write functions for multiples cache systems

Install

composer require juanchosl/simplecache

Performance

From faster to slower (miliseconds), with a set and get data test, counting the time in miliseconds

set 10 elements get 10 elements
  1. Session: 0.0911
  2. Apcu: 0.1049
  3. Yac: 0.3328
  4. File: 6.731
  5. Redis: 7.4098
  6. Memcached: 8.9169
  7. Process: 9.8691
  8. Memcache: 25.527
  1. Apcu: 0.0889
  2. Session: 0.0958
  3. Yac: 0.1299
  4. Process: 0.2308
  5. File: 4.1699
  6. Memcached: 6.541
  7. Memcache: 11.636
  8. Redis: 20.7901
set 100 elements get 100 elements
  1. Session: 0.932
  2. Process: 0.962
  3. Apcu: 0.9041
  4. Yac: 1.009
  5. Redis: 69.5832
  6. File: 71.667
  7. Memcache: 165.0958
  8. Memcached: 601.537
  1. Process: 0.947
  2. Session: 0.8419
  3. Yac: 0.8719
  4. Apcu: 1.6818
  5. File: 18.5752
  6. Memcache: 82.8669
  7. Memcached: 97.2202
  8. Redis: 134.5811
set 1000 elements get 1000 elements
  1. Yac: 9.86
  2. Session: 9.871
  3. Apcu: 10.2232
  4. Process: 10.4311
  5. File: 593.3461
  6. Memcached: 857.6429
  7. Redis: 1663.4059
  8. Memcache: 3402.5531
  1. Session: 8.606
  2. Process: 9.0408
  3. Apcu: 9.0988
  4. Yac: 9.692
  5. File: 204.6311
  6. Memcached: 824.0871
  7. Memcache: 1020.0572
  8. Redis: 2280.118
  • Process: It's only valid for current request execution
  • Session: Only valid for a current user session
  • APCU: Cache into user scope, saved on memory for fast access. APCU Module is required
  • YAC: Cache into user scope, saved on memory for fast access. Alternative for APCU, YAC Module is required
  • Memcached: If Memcached service is available and Memcached library is installed
  • Redis: If Redis service is available and Redis library is installed
  • Memcache: If Memcached service is available and Memcache library is installed
  • File: The most compatible system, file into filesystem, but slower

How use it

Use directly one of the available libs

For create a cache instance

use JuanchoSL\SimpleCache\Repositories\ProcessCache;

$cache = new ProcessCache($_ENV['CACHE_ENVIRONMENT']);
//The max time to expire is 30 days if you do not set a time
$cache->setMaxTtl(3600 * 24);

For write a cache index

Set into $cache_key the $value, valid for $ttl seconds or default TTL if you don not pass a value

$result = $cache->set(string $cache_key, mixed $value, int $ttl = 0);

For read a cache index

Read from cache the contents of $cache_key and return his value or $default if it not exists or it is not valid

$cache_value = $cache->get(string $cache_key, $default = null);

For delete a cache index

Delete from cache the value with $cache_key

$result = $cache->delete(string $cache_key);

For write multiple cache indexes

Set into $cache_key the $values, an iterable containing a list of $cache_key => $value pairs, valid for $ttl seconds or default TTL if you don not pass a value

$result = $cache->setMultiple(iterable $values, \DateInterval|int $ttl = 0);

For read multiple cache indexes

Read from cache the contents of $cache_keys and return a list of $key => $value pairs. Missed keys will be filled with the $default value if it is provided and distinct of NULL, or ignored otherwise.

default values can be:

  • a value for all (distinct of null)
  • a list with the same array indexes of the required keys and an exclusive default value or each key
  • a collection of keys => default_value (primitive, class with an __invoke method or closure)
$cache_value = $cache->getMultiple(iterable $cache_keys, $default = null);

For delete a cache index

Delete from cache the values from the $cache_keys list

$result = $cache->deleteMultiple(iterable $cache_keys);

For replace a cache index

Replace into cache the value with $cache_key with the $new_value without change his expiration time

$result = $cache->replace(string $cache_key, mixed $new_value);

For change the time to live of cache index

Change the expiration time of $cache_key with the new one passed as $new_ttl

$result = $cache->touch(string $cache_key, \DateInterval|int $new_ttl);

For increment a cache index numeric value

Increments the value into $cache_key adding $numeric_increment to his value. If not exists it is created.

$result = $cache->increment(string $cache_key, int|float $numeric_increment, \DateInterval|int $ttl_if_not_exists = $max_ttl);

For decrement a cache index numeric value

Decrements the value into $cache_key subtracting $numeric_decrement to his value. If not exists it is created.

$result = $cache->decrement(string $cache_key, int|float $numeric_decrement, \DateInterval|int $ttl_if_not_exists = $max_ttl);

For check if the cache contains a $cache_key

Check if key exists, is not recommended, because can be return true and just another script can remove it

$result = $cache->has(string $cache_key);

For clear all cache indexes

Remove all data from cache

$result = $cache->clear();

Use the provided adapter for use with compatibility with PSR-16 Simple-Cache

Create a cache instance

After create a Cache Instance, you can use it with the provided PsrSimpleCacheAdapter in order to work conform the PSR-16 https://www.php-fig.org/psr/psr-16/

use JuanchoSL\SimpleCache\Repositories\ProcessCache;
use JuanchoSL\SimpleCache\Adapters\PsrSimpleCacheAdapter;

$lib = new ProcessCache($_ENV['CACHE_ENVIRONMENT']);
$cache = new PsrSimpleCacheAdapter($lib);