PHP code example of julienlinard / php-cache

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

    

julienlinard / php-cache example snippets




ulienLinard\Cache\Cache;

// Initialize with configuration
Cache::init([
    'default' => 'file', // or 'array', 'redis'
    'drivers' => [
        'array' => [],
        'file' => [
            'path' => __DIR__ . '/cache',
            'ttl' => 3600, // Default TTL in seconds
        ],
        'redis' => [
            'host' => '127.0.0.1',
            'port' => 6379,
            'password' => null,
            'database' => 0,
        ],
    ],
]);

// Simple usage
Cache::set('user_123', ['name' => 'John', 'email' => '[email protected]'], 3600);
$user = Cache::get('user_123');

use JulienLinard\Cache\Cache;

Cache::init([
    'default' => 'array',
    'drivers' => [
        'array' => [
            'prefix' => 'myapp', // Optional prefix for all keys
            'ttl' => 3600, // Default TTL
        ],
    ],
]);

Cache::init([
    'default' => 'file',
    'drivers' => [
        'file' => [
            'path' => __DIR__ . '/cache', // Cache directory
            'prefix' => 'myapp',
            'ttl' => 3600,
            'file_permissions' => 0644, // File permissions
            'directory_permissions' => 0755, // Directory permissions
        ],
    ],
]);

Cache::init([
    'default' => 'redis',
    'drivers' => [
        'redis' => [
            'host' => '127.0.0.1',
            'port' => 6379,
            'password' => 'your_password', // Optional
            'database' => 0,
            'timeout' => 2.0,
            'persistent' => false, // Persistent connection
            'persistent_id' => null,
            'prefix' => 'myapp',
            'ttl' => 3600,
        ],
    ],
]);

// With default TTL
Cache::set('key', 'value');

// With custom TTL (in seconds)
Cache::set('key', 'value', 3600);

// Complex data
Cache::set('user', [
    'id' => 123,
    'name' => 'John',
    'email' => '[email protected]',
], 3600);

// Simple retrieval
$value = Cache::get('key');

// With default value
$value = Cache::get('key', 'default_value');

// Complex data
$user = Cache::get('user', []);

if (Cache::has('key')) {
    // Key exists
}

Cache::delete('key');

Cache::clear();

$values = Cache::getMultiple(['key1', 'key2', 'key3'], null);
// Returns: ['key1' => value1, 'key2' => value2, 'key3' => value3]

Cache::setMultiple([
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3',
], 3600); // Common TTL for all keys

$deleted = Cache::deleteMultiple(['key1', 'key2', 'key3']);
// Returns the number of deleted keys

// Increment
Cache::set('counter', 0);
Cache::increment('counter'); // 1
Cache::increment('counter', 5); // 6

// Decrement
Cache::decrement('counter'); // 5
Cache::decrement('counter', 2); // 3

$value = Cache::pull('key'); // Retrieves and deletes in one operation

// Use a specific driver
Cache::set('key', 'value', 3600, 'redis');
$value = Cache::get('key', null, 'redis');

// Or get the driver directly
$redisCache = Cache::driver('redis');
$redisCache->set('key', 'value');

// Create a tagged cache
$taggedCache = Cache::tags(['users', 'posts']);

// Store values with tags
$taggedCache->set('user_1', ['name' => 'John']);
$taggedCache->set('user_2', ['name' => 'Jane']);

// Get keys associated with a tag
$keys = $taggedCache->getKeysByTag('users');

// Invalidate all keys with a tag
$taggedCache->invalidateTags('users');
// or multiple tags
$taggedCache->invalidateTags(['users', 'posts']);

use JulienLinard\Cache\CacheManager;

$manager = CacheManager::getInstance([
    'default' => 'file',
    'drivers' => [
        'file' => ['path' => __DIR__ . '/cache'],
    ],
]);

// Get a driver
$driver = $manager->driver('file');

// Register a custom driver
$customDriver = new MyCustomDriver();
$manager->registerDriver('custom', $customDriver);

// Change the default driver
$manager->setDefaultDriver('redis');

use JulienLinard\Cache\KeyValidator;

// Validate a key
try {
    KeyValidator::validate('valid_key_123');
} catch (InvalidKeyException $e) {
    // Invalid key
}

// Sanitize a key
$cleanKey = KeyValidator::sanitize('invalid/key@test');
// Returns: 'invalid_key_test'

use JulienLinard\Cache\Exceptions\CacheException;
use JulienLinard\Cache\Exceptions\InvalidKeyException;
use JulienLinard\Cache\Exceptions\DriverException;

try {
    Cache::set('key', 'value');
} catch (InvalidKeyException $e) {
    // Invalid key
} catch (DriverException $e) {
    // Driver error
} catch (CacheException $e) {
    // Other cache error
}

// ✅ GOOD: Simple and descriptive keys
Cache::set('user_123', $userData);

// ❌ BAD: Keys with special characters
Cache::set('user/123', $userData); // Throws exception

// ✅ GOOD: Use prefixes
Cache::init([
    'drivers' => [
        'file' => ['prefix' => 'myapp'],
    ],
]);

// ✅ GOOD: Validate data before caching
$data = validateAndSanitize($userInput);
Cache::set('key', $data);

use JulienLinard\Cache\Cache;

function getUser(int $id): array
{
    $cacheKey = "user_{$id}";
    
    // Check cache
    if (Cache::has($cacheKey)) {
        return Cache::get($cacheKey);
    }
    
    // Retrieve from database
    $user = fetchUserFromDatabase($id);
    
    // Cache for 1 hour
    Cache::set($cacheKey, $user, 3600);
    
    return $user;
}

use JulienLinard\Cache\Cache;

// Store users with tag
$usersCache = Cache::tags('users');
$usersCache->set('user_1', $user1, 3600);
$usersCache->set('user_2', $user2, 3600);

// When a user is updated, invalidate the tag
function updateUser(int $id, array $data): void
{
    // Update in database
    updateUserInDatabase($id, $data);
    
    // Invalidate all entries with 'users' tag
    $usersCache = Cache::tags('users');
    $usersCache->invalidateTags('users');
}

use JulienLinard\Cache\Cache;

function renderView(string $template, array $data): string
{
    $cacheKey = 'view_' . md5($template . serialize($data));
    
    if (Cache::has($cacheKey)) {
        return Cache::get($cacheKey);
    }
    
    $html = renderTemplate($template, $data);
    Cache::set($cacheKey, $html, 1800); // 30 minutes
    
    return $html;
}

use JulienLinard\Cache\Cache;

function incrementPageViews(string $pageId): int
{
    $key = "page_views_{$pageId}";
    
    if (!Cache::has($key)) {
        // Initialize with 24h expiration
        Cache::set($key, 0, 86400);
    }
    
    return Cache::increment($key);
}

use JulienLinard\Cache\Cache;
use JulienLinard\Doctrine\EntityManager;

function getCachedEntity(EntityManager $em, string $entityClass, int $id): ?object
{
    $cacheKey = strtolower($entityClass) . "_{$id}";
    
    if (Cache::has($cacheKey)) {
        $data = Cache::get($cacheKey);
        // Rebuild entity from data
        return $em->getRepository($entityClass)->find($id);
    }
    
    $entity = $em->getRepository($entityClass)->find($id);
    
    if ($entity) {
        // Store entity data
        Cache::set($cacheKey, $entity->toArray(), 3600);
    }
    
    return $entity;
}

$cachePath = __DIR__ . '/cache';
if (!is_dir($cachePath)) {
    mkdir($cachePath, 0755, true);
}