eduardtrayan/faststorage-bundle
Installation
composer require eduardtrayan/faststorage-bundle
Add to config/bundles.php:
EduardTrayan\FastStorageBundle\EduardtrayanFaststorageBundle::class => ['all' => true],
Publish Config
php artisan vendor:publish --provider="EduardTrayan\FastStorageBundle\EduardtrayanFaststorageBundle" --tag="config"
Edit config/eduardtrayan_faststorage.php to define your storage backends (e.g., memcache/redis).
First Use Case
Inject the FastStorageInterface into a service:
use EduardTrayan\FastStorageBundle\Storage\FastStorageInterface;
class MyService {
public function __construct(private FastStorageInterface $storage) {}
public function cacheData(string $key, $value, int $ttl = 3600) {
$this->storage->set($key, $value, $ttl);
}
}
Storage Abstraction
Use the FastStorageInterface to switch backends without changing business logic:
$this->storage->get('key'); // Retrieve
$this->storage->set('key', $data); // Store
$this->storage->delete('key'); // Delete
Contextual Storage Dynamically select storage via config:
# config/eduardtrayan_faststorage.php
default_storage: redis
storage:
redis: { host: '127.0.0.1', port: 6379 }
memcache: { host: '127.0.0.1', port: 11211 }
Override in runtime:
$this->storage->setStorage('memcache'); // Switch to memcache
TTL Management Set time-to-live (TTL) for keys:
$this->storage->set('temp_data', $data, 60); // Expires in 60 seconds
Batch Operations
Use mget()/mset() for bulk operations:
$this->storage->mset(['key1' => 'val1', 'key2' => 'val2']);
$values = $this->storage->mget(['key1', 'key2']);
redis/memcache drivers in Laravel queues by binding the FastStorageInterface to Illuminate\Contracts\Queue\Queue.Connection Failures
try-catch:
try {
$this->storage->get('critical_key');
} catch (\RuntimeException $e) {
// Fallback to DB or default value
}
Serialization Issues
Serializable or use json_encode():
$this->storage->set('user', json_encode($user));
$data = json_decode($this->storage->get('user'), true);
Config Overrides
config/eduardtrayan_faststorage.php. Override via service provider:
$this->app->bind(FastStorageInterface::class, function ($app) {
return $app['faststorage']->setStorage('redis');
});
redis-cli ping, telnet 127.0.0.1 11211).debugbar to inspect storage operations:
\Barryvdh\Debugbar\Debugbar::info('Storage', ['key' => $this->storage->get('key')]);
Custom Storage Drivers
Implement Eduardtrayan\FastStorageBundle\Storage\StorageInterface for new backends (e.g., array for testing):
class ArrayStorage implements StorageInterface {
private $data = [];
public function get($key) { /* ... */ }
public function set($key, $value, $ttl = null) { /* ... */ }
}
Register via config:
storage:
array: ~
Middleware for Storage
Create middleware to auto-switch storage based on request context (e.g., X-Storage-Engine header):
$this->storage->setStorage($request->header('x-storage-engine'));
Monitoring Extend the bundle to track metrics (e.g., hits/misses):
$this->storage->extend(function ($storage) {
$storage->addListener('get', function ($key, $result) {
\Log::info("Storage GET: {$key} => " . ($result ? 'HIT' : 'MISS'));
});
});
How can I help you explore Laravel packages today?