1. Go to this page and download the library: Download webfiori/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/ */
webfiori / cache example snippets
WebFiori\Cache\Cache;
use WebFiori\Cache\FileStorage;
use WebFiori\Cache\KeyManager;
// Set up encryption key (recommended for production)
$_ENV['CACHE_KEY'] = KeyManager::generateKey();
// Create a cache instance
$cache = new Cache(new FileStorage('/path/to/cache'));
// Store and retrieve
$cache->set('greeting', 'Hello, World!', 3600);
echo $cache->get('greeting'); // Hello, World!
// Auto-populate on cache miss using a generator callback
$data = $cache->get('user_data', function () {
return fetchUserDataFromDatabase();
}, 3600);
// Check, delete, flush
$cache->has('greeting'); // true
$cache->delete('greeting');
$cache->flush();
use WebFiori\Cache\Cache;
use WebFiori\Cache\FileStorage;
$cache = new Cache(new FileStorage('/path/to/cache'));
// Store with default TTL (60 seconds)
$cache->set('key', 'value');
// Store with custom TTL and override existing
$cache->set('key', 'new_value', 3600, true);
// Retrieve (returns null on miss)
$data = $cache->get('key');
// Generator with parameters
$profile = $cache->get('user_profile', function ($userId, $
use WebFiori\Cache\CacheFacade;
CacheFacade::set('key', 'value', 60);
echo CacheFacade::get('key');
// withPrefix() returns a Cache instance
CacheFacade::withPrefix('users_')->set('count', 100, 60);
use WebFiori\Cache\KeyManager;
// Generate and set a key programmatically
$key = KeyManager::generateKey();
KeyManager::setEncryptionKey($key);
use WebFiori\Cache\Cache;
use WebFiori\Cache\RedisStorage;
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$storage = new RedisStorage($redis, 'wf_cache:');
$cache = new Cache($storage);
$cache->set('key', 'value', 3600);
echo $cache->get('key'); // value
use WebFiori\Cache\Storage;
use WebFiori\Cache\Item;
class MemoryStorage implements Storage {
public function store(Item $item) { /* ... */ }
public function read(string $key, ?string $prefix) { /* ... */ }
public function readItem(string $key, ?string $prefix): ?Item { /* ... */ }
public function has(string $key, ?string $prefix): bool { /* ... */ }
public function delete(string $key) { /* ... */ }
public function flush(?string $prefix) { /* ... */ }
public function purgeExpired(): int { /* ... */ }
}
$cache = new Cache(new MemoryStorage());
// v2 (static singleton) // v3 (instance-based)
Cache::set('k', 'v', 60); $cache = new Cache(new FileStorage($path));
Cache::get('k'); $cache->set('k', 'v', 60);
$cache->get('k');
Cache::withPrefix('x')->get('k'); $cache->withPrefix('x')->get('k');
// ⚠️ prefix leaked to all future calls // ✅ prefix is scoped, original unchanged
Cache::setDriver($driver); $cache = new Cache($driver);
Cache::create($driver, true, 'pfx'); new Cache($driver, true, 'pfx');
use WebFiori\Cache\CacheFacade;
CacheFacade::set('k', 'v', 60);
CacheFacade::get('k');