Installation:
composer require zingle-com/stash-package
Register the service provider in config/app.php after Laravel core providers but before custom providers:
ZingleCom\Stash\StashServiceProvider::class,
Publish config/assets:
php artisan vendor:publish --provider="ZingleCom\Stash\StashServiceProvider"
First Use Case:
Store/retrieve data via stash facade:
use ZingleCom\Stash\Facades\Stash;
// Store data (expires in 1 hour)
Stash::put('user.cache', $user, 3600);
// Retrieve data
$cachedUser = Stash::get('user.cache');
// Check existence
if (Stash::has('user.cache')) { ... }
Key Files:
config/stash.php: Driver/configuration (e.g., driver: 'file', path: storage/framework/cache).app/Stash.php: Facade alias (auto-generated after publishing).Driver-Based Storage:
file, database, and redis drivers (configured in config/stash.php).'driver' => env('STASH_DRIVER', 'file'),
'redis' => [
'connection' => 'cache',
],
Contextual Storage:
user.profile.* vs. api.rate_limits.*):
Stash::put('user.profile.settings', $settings, 86400); // 24h TTL
TTL Management:
3600 = 1 hour). Omit to persist indefinitely.$response = Stash::remember('api.github.user', 7200, function () {
return Http::get('https://api.github.com/user')->json();
});
Integration with Laravel Features:
public function handle($request, Closure $next)
{
return Stash::remember('route.home', 300, function () use ($request) {
return $next($request);
});
}
event(new OrderPlaced($order));
Stash::put('order.processing.' . $order->id, $order, 300);
Bulk Operations:
Stash::flush() to clear all cached items (e.g., after config changes).Stash::forget('user.cache');
Custom Drivers:
ZingleCom\Stash\Contracts\Driver to create a new storage backend (e.g., Memcached).$this->app->bind('stash.driver', function ($app) {
return new CustomDriver();
});
Tag-Based Invalidation:
user.* tags):
Stash::put('user.123', $user, 3600, ['users']);
Stash::flushByTags(['users']); // Clears all tagged keys
Fallback Logic:
Cache facade for hybrid storage:
$data = Stash::get('fallback.example') ?? Cache::get('fallback.example');
Driver Mismatches:
config/stash.php) matches your environment (e.g., redis requires Redis server).stash.log (if enabled) for driver initialization errors.TTL Precision:
file driver). Use it for approximate expiration.Key Collisions:
laravel.*). Stick to a consistent namespace (e.g., app.*).File Driver Permissions:
storage/framework/cache directory is writable:
chmod -R 775 storage/framework/cache
Memory Leaks:
Stash::put() calls (without TTL) can bloat storage. Audit keys periodically:
$keys = Stash::keys('app.*'); // List all keys with prefix
Enable Logging:
Add to config/stash.php:
'log' => [
'enabled' => true,
'channel' => 'single',
],
Check logs for driver operations:
log:stash.*
Inspect Storage:
storage/framework/cache.stash table:
SELECT * FROM stash WHERE key LIKE 'user.%';
Clear Stale Data:
Run the stash:clear artisan command to wipe all cached items:
php artisan stash:clear
Custom Events:
StashStored, StashRetrieved, or StashDeleted events to log/audit operations:
Event::listen(StashEvents::STASH_STORED, function ($event) {
Log::info("Stored: {$event->key}", $event->data);
});
Macros:
Stash facade for project-specific methods:
Stash::macro('getUser', function ($id) {
return $this->get("user.{$id}");
});
Usage:
$user = Stash::getUser(123);
Testing:
Stash facade in tests:
$this->mock(ZingleCom\Stash\Facades\Stash::class)->shouldReceive('get')
->with('test.key')->andReturn($mockData);
How can I help you explore Laravel packages today?