PHP code example of solophp / file-cache

1. Go to this page and download the library: Download solophp/file-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/ */

    

solophp / file-cache example snippets




use Solo\FileCache;

$cache = new FileCache('/path/to/cache');

// Set a cache item with a 1-hour TTL
$cache->set('user_123', ['name' => 'John Doe'], 3600);

// Retrieve the cache item
$user = $cache->get('user_123', null);
if ($user !== null) {
    echo $user['name']; // Outputs: John Doe
}

// Check if a cache item exists
if ($cache->has('user_123')) {
    echo 'Cache exists!';
}

// Delete a cache item
$cache->delete('user_123');

// Clear all cache items
$cache->clear();

// Set multiple cache items
$cache->setMultiple([
    'key1' => 'value1',
    'key2' => 'value2',
], 600); // 10-minute TTL

// Retrieve multiple cache items
$items = $cache->getMultiple(['key1', 'key2'], 'default');
foreach ($items as $key => $value) {
    echo "$key: $value\n";
}

// Delete multiple cache items
$cache->deleteMultiple(['key1', 'key2']);

$ttl = new DateInterval('PT1H'); // 1 hour
$cache->set('session_data', ['token' => 'abc123'], $ttl);

$cache = new FileCache('/tmp/my_cache');