laminas/laminas-cache-storage-adapter-session
Session-based cache storage adapter for Laminas Cache. Stores cached items in PHP sessions, useful for per-user caching and small transient data. Integrates with Laminas\Cache\Storage; suitable for apps already relying on session state.
Installation:
composer require laminas/laminas-cache-storage-adapter-session
Basic Usage:
use Laminas\Cache\Storage\Adapter\Session;
$sessionAdapter = new Session();
$sessionAdapter->setItem('key', 'value');
$value = $sessionAdapter->getItem('key');
Laravel Integration:
use Laminas\Cache\Storage\Adapter\Session;
use Psr\Container\ContainerInterface;
$container->set(AdapterInterface::class, function (ContainerInterface $container) {
return new Session();
});
Use this adapter for short-lived, user-specific cached data (e.g., form inputs, user preferences, or temporary session-based computations). It’s ideal when:
Session-Aware Caching:
// Store user-specific data tied to their session
$adapter->setItem('user_prefs', ['theme' => 'dark']);
// Retrieve later in the same session
$prefs = $adapter->getItem('user_prefs');
Laravel Service Provider Integration:
use Illuminate\Support\ServiceProvider;
use Laminas\Cache\Storage\Adapter\Session;
class CacheServiceProvider extends ServiceProvider {
public function register() {
$this->app->singleton('laminas.session.cache', function () {
return new Session();
});
}
}
PSR-16 Cache Interface Compliance:
// Works with PSR-16-compliant consumers (e.g., Laravel's cache facade)
$cache = new Session();
$cache->set('temp_key', 'temp_value', 3600); // 1-hour TTL
Laravel Cache Facade:
Bind the adapter to Laravel’s cache system via CacheServiceProvider:
$this->app->bind('cache.store.session', function () {
return new Session();
});
Then use it in config:
'stores' => [
'session' => [
'driver' => 'laminas.session',
],
],
Middleware for Session Data: Use the adapter in middleware to cache user-specific responses:
public function handle($request, Closure $next) {
$cacheKey = 'user_dashboard_' . auth()->id();
$data = cache()->store('session')->get($cacheKey);
if (!$data) {
$data = $next($request)->getContent();
cache()->store('session')->put($cacheKey, $data, now()->addMinutes(10));
}
return response($data);
}
Fallback to File/Redis: Combine with other adapters for hybrid caching:
$fallback = new FileSystem();
$sessionAdapter = new Session($fallback); // Fallback if session fails
Session Dependency:
session_start() is called before use.session_id() is set; otherwise, the adapter will fail silently.TTL Ignored:
Memory Usage:
ini_get('session.gc_maxlifetime'); // Check session lifetime
Laravel Session Conflicts:
file, database), the adapter may overwrite or conflict with session data. Use distinct keys:
$adapter->setItem('cache_key_prefix_', $data); // Avoid collisions
if (empty($_SESSION)) {
session_start();
}
Check for errors with:
try {
$adapter->getItem('test');
} catch (\RuntimeException $e) {
// Handle session errors
}
Custom Session Handler: Extend the adapter to use a custom session handler:
use Laminas\Cache\Storage\Adapter\Session;
use Laminas\Cache\Storage\Adapter\SessionOptions;
$options = new SessionOptions();
$options->setHandler(new CustomSessionHandler());
$adapter = new Session($options);
PSR-16 Wrapper: Create a wrapper to add TTL support (since the adapter ignores it):
class SessionCache implements Psr\SimpleCache\CacheInterface {
private $adapter;
public function __construct(Session $adapter) {
$this->adapter = $adapter;
}
public function get($key, $default = null) {
return $this->adapter->getItem($key) ?? $default;
}
public function set($key, $value, $ttl = null) {
$this->adapter->setItem($key, $value);
return true;
}
// Implement remaining PSR-16 methods...
}
Laravel Cache Extension: Extend Laravel’s cache system to support the adapter:
namespace App\Extensions;
use Laminas\Cache\Storage\Adapter\Session;
use Illuminate\Cache\Repository;
class SessionCache extends Repository {
public function __construct() {
$this->store = new Session();
}
}
Register in config/cache.php:
'stores' => [
'session' => [
'driver' => 'session',
'class' => App\Extensions\SessionCache::class,
],
],
Session Name:
The adapter uses the default session name (PHPSESSID). Override it via ini_set('session.name', 'custom_name') if needed.
Garbage Collection:
Session garbage collection (session.gc_probability and session.gc_divisor) affects the adapter. Adjust in php.ini or runtime:
ini_set('session.gc_maxlifetime', 1440); // 24 minutes
How can I help you explore Laravel packages today?