sonata-project/cache
Deprecated Sonata cache library providing adapters for cache backends and counters. Includes Redis (PRedis) implementations to set/get cached values and increment counters, with simple key arrays and TTL support via Composer install.
Installation:
composer require sonata-project/cache
Ensure predis/predis (for Redis) or ext-memcached (for Memcached) is installed.
Basic Cache Usage:
use Sonata\Cache\Adapter\Cache\PRedisCache;
$adapter = new PRedisCache([
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
]);
// Set cache with multi-key support
$keys = ['user' => 1, 'profile' => 'settings'];
$adapter->set($keys, 'cached_data', 3600); // 1-hour TTL
// Retrieve cache
$cacheElement = $adapter->get($keys);
$data = $cacheElement->getData(); // 'cached_data'
Basic Counter Usage:
use Sonata\Cache\Adapter\Counter\PRedisCounter;
$counterAdapter = new PRedisCounter([
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
]);
$counter = $counterAdapter->increment('page_views');
$views = $counter->getValue(); // e.g., 1
Laravel Integration (Quick Start):
Bind the adapter to Laravel’s service container in AppServiceProvider:
public function register()
{
$this->app->singleton('sonata.cache', function ($app) {
return new PRedisCache([
'host' => config('cache.redis.host'),
'port' => config('cache.redis.port'),
]);
});
}
src/Adapter/Cache/ for cache adapters and src/Adapter/Counter/ for counter logic.Scenario: Cache user-specific settings with a composite key (e.g., user:{id}:theme).
$keys = ['user' => $userId, 'setting' => 'theme'];
$adapter->set($keys, $theme, 86400); // Cache for 1 day
$theme = $adapter->get($keys)->getData();
$keys = ['user' => 123, 'preferences' => 'notifications'];
$adapter->set($keys, $prefs, 3600);
$serializedKey = json_encode($keys);
Cache::put($serializedKey, $data, 3600);
$counter = $counterAdapter->increment('api_requests');
$requestCount = $counter->getValue();
// app/Facades/Counter.php
public static function increment($key, $value = 1)
{
return app('sonata.counter')->increment($key, $value);
}
PRedisCache with MemcachedCache (if supported).
$adapter = new Sonata\Cache\Adapter\Cache\MemcachedCache([
'host' => '127.0.0.1',
'port' => 11211,
]);
$adapter->set($keys, $data, 60);
foreach ($data as $keys => $value) {
$adapter->set($keys, $value, 3600);
}
set($keys, $data, $ttl) for multi-key caching.get($keys)->getData() to fetch cached values.has($keys) to verify cache presence.delete($keys) to remove cached entries.increment($key, $value = 1) to increase a counter.$counter->getValue() to retrieve the current count.decrement method).Extend Laravel’s CacheStore to use Sonata’s adapter:
// app/Providers/AppServiceProvider.php
use Sonata\Cache\Adapter\Cache\PRedisCache;
use Illuminate\Cache\Repository;
public function register()
{
$this->app->bind('sonata.cache', function () {
return new PRedisCache([
'host' => config('cache.redis.host'),
'port' => config('cache.redis.port'),
]);
});
$this->app->extend('cache.store', function ($app, $store) {
if ($store === 'sonata') {
return new Repository($app['sonata.cache']);
}
return $store;
});
}
Configure in .env:
CACHE_DEFAULT=sonata
Configure logging via the adapter’s constructor:
use Psr\Log\LoggerInterface;
$adapter = new PRedisCache([
'host' => '127.0.0.1',
'logger' => $this->app->make(LoggerInterface::class),
]);
For Laravel’s single-key cache, serialize multi-keys:
$serializedKey = json_encode($keys);
Cache::put($serializedKey, $data, $ttl);
Wrap adapter calls in try-catch blocks:
try {
$data = $adapter->get($keys)->getData();
} catch (\Exception $e) {
Log::error("Cache error: " . $e->getMessage());
// Fallback logic
}
Deprecated Package:
PHP Version Requirement:
Multi-Key Limitations:
json_encode($keys) for serialization/deserialization.No Native Laravel Integration:
Cache facade or service provider.Counter Limitations:
decrement method or atomic operations for complex counters.Predis/Memcached Dependencies:
predis/predis or ext-memcached, which may not be installed.composer.json and installed in deployment.Key Collisions:
user:{id}:{setting}).TTL Granularity:
How can I help you explore Laravel packages today?