gpupo/cache
gpupo/cache is a lightweight PHP cache helper with a simple API for storing and retrieving values, aimed at reducing repeated computations and speeding up applications. Suitable for small projects or as a base for custom cache adapters.
Installation (if still needed for legacy projects):
composer require gpupo/cache
(Note: Avoid new projects—use symfony/cache instead.)
Basic PSR-6 Cache Pool Initialization:
use Gpupo\Cache\CachePool;
use Gpupo\Cache\CacheItem;
$cache = new CachePool(new \Gpupo\Cache\Storage\FileStorage('/path/to/cache'));
First Use Case:
$item = $cache->getItem('key');
if (!$item->isHit()) {
$item->set('value');
$cache->save($item);
}
Gpupo\Cache\Storage for available backends (e.g., FileStorage, RedisStorage).gpupo/cache usages in your project’s composer.json or vendor/ to audit dependencies.Cache Warming:
$cache = new CachePool($storage);
$items = $cache->getItems(['key1', 'key2']);
foreach ($items as $item) {
if (!$item->isHit()) {
$item->set(fetchExpensiveData($item->getKey()));
}
}
$cache->saveDeferred(); // Batch save
TTL Management:
$item = $cache->getItem('user:123');
$item->expiresAfter(3600); // 1 hour TTL
$item->set($userData);
$cache->save($item);
Tag-Based Invalidation (if supported by storage):
$cache->deleteItemsMatchingTag('user:*'); // Hypothetical; verify adapter support.
$app->singleton(\Psr\Cache\CacheItemPoolInterface::class, function ($app) {
return new CachePool(new FileStorage(storage_path('framework/cache')));
});
gpupo/cache with symfony/cache and update imports:
use Symfony\Contracts\Cache\CacheInterface;
$cache = new CacheInterface(new \Symfony\Component\Cache\Adapter\FilesystemAdapter());
Abandoned Package:
symfony/cache (PSR-6 compliant) or Laravel’s built-in cache (Illuminate\Cache).Storage Adapter Quirks:
FileStorage may lock files during writes, causing race conditions.PSR-6 Inconsistencies:
deleteItemsMatchingTag) may not be supported by all storage backends.CacheItem::isHit() behavior with empty/null values.No Laravel-Specific Features:
Cache::remember() helpers./path/to/cache/ for files or Redis keys to verify writes.try-catch to log exceptions:
try {
$cache->save($item);
} catch (\Psr\Cache\InvalidArgumentException $e) {
Log::error("Cache save failed: " . $e->getMessage());
}
Gpupo\Cache\Storage\StorageInterface for new backends (e.g., database).
class DbStorage implements StorageInterface { ... }
class LoggingPool implements CacheItemPoolInterface {
public function __construct(private CacheItemPoolInterface $pool) {}
public function getItem($key) {
Log::debug("Cache get: $key");
return $this->pool->getItem($key);
}
// ...
}
composer why gpupo/cache to find direct/indirect usages.composer.json:
"require": {
"symfony/cache": "^6.0"
}
gpupo/cache imports with symfony/cache or Laravel’s cache facade. Example:
// Before
use Gpupo\Cache\CachePool;
// After
use Symfony\Contracts\Cache\CacheInterface;
How can I help you explore Laravel packages today?