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/ */
$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 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;
}