PHP code example of apricity / micro-cache

1. Go to this page and download the library: Download apricity/micro-cache library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

apricity / micro-cache example snippets


public static function set(mixed $key, mixed $value, int $ttl = 0): string|int

$key = MicroCache::set('my_key', 'my_value', 3600);

public static function get(mixed $key): mixed

$value = MicroCache::get('my_key');
if (is_null($value)) {
    echo "Cache miss";
} else {
    echo "Cache value: " . $value;
}

public static function delete(mixed $key): void

MicroCache::delete('my_key');

public static function clear(): void

MicroCache::clear();

public static function has(mixed $key): bool

if (MicroCache::has('my_key')) {
    echo "Cache exists";
} else {
    echo "Cache does not exist or has expired";
}

use Apricity\MicroCache;

class SharedCache extends MicroCache {}

SharedCache::set('shared_cache_key', 'shared_value');

SharedCache::get('shared_cache_key'); // shared_value
MicroCache::get('shared_cache_key'); // shared_value

MicroCache::set('cache_key', 'cached_value');

SharedCache::get('cache_key'); // cached_value
MicroCache::get('cache_key'); // cached_value

use Apricity\MicroCache;

class UniqueCache extends MicroCache {
    protected static array $microCache = []; // Non-shared unique cache.
}

UniqueCache::set('unique_cache_key', 'unique_value');

UniqueCache::get('unique_cache_key'); // unique_value
MicroCache::get('unique_cache_key'); // null

MicroCache::set('cache_key', 'cached_value');

UniqueCache::get('cache_key'); // null
MicroCache::get('cache_key'); // cached_value