dwi-wijonarko/laravel-simple-cache
最新稳定版本:1.0.0
Composer 安装命令:
composer require dwi-wijonarko/laravel-simple-cache
包简介
Simple cache management for Laravel with tags, warming, and monitoring dashboard
README 文档
README
A simple and elegant cache management package for Laravel with tag support, cache warming, pattern-based clearing, and a beautiful monitoring dashboard.
Features
- 🏷️ Tag-based caching - Organize and clear cache by tags
- 🔥 Cache warming - Pre-load cache with predefined data
- 🎯 Pattern clearing - Clear multiple cache keys using Redis patterns
- 📊 Dashboard - Beautiful web interface to monitor and manage cache
- 🛠️ Artisan commands - CLI tools for cache management
- 🔌 Trait support - Easy integration with Eloquent models
- 📝 Helper functions - Convenient global helpers
- 🚀 Fluent API - Chainable methods for clean code
Requirements
- PHP 8.0 or higher
- Laravel 9.x, 10.x, or 11.x
- Redis (optional, for advanced features)
Installation
Install the package via Composer:
composer require dwi-wijonarko/laravel-simple-cache
Publish the configuration file:
php artisan vendor:publish --tag=simple-cache-config
Optionally, publish the views:
php artisan vendor:publish --tag=simple-cache-views
Configuration
The configuration file config/simple-cache.php contains:
return [ 'default_ttl' => 3600, // Default cache TTL in seconds 'auto_clear_on_save' => true, // Auto clear cache on model save 'auto_clear_on_delete' => true, // Auto clear cache on model delete 'dashboard_enabled' => true, // Enable/disable dashboard 'dashboard_route' => 'cache-dashboard', // Dashboard route 'dashboard_middleware' => ['web', 'auth'], // Dashboard middleware 'warmers' => [], // Cache warmers configuration ];
Usage
Basic Usage
use DwiWijonarko\SimpleCache\Facades\CacheManager; // Store cache CacheManager::put('key', 'value', 3600); // Retrieve cache $value = CacheManager::get('key'); // Remember cache $users = CacheManager::remember('users', function () { return User::all(); }, 3600); // Forget cache CacheManager::forget('key');
Tagged Cache
// Store with tags CacheManager::tags(['users', 'active']) ->put('active_users', $users, 3600); // Retrieve with tags $users = CacheManager::tags(['users', 'active']) ->get('active_users'); // Clear by tags CacheManager::tags(['users'])->flush();
Cache Warming
Define warmers in config/simple-cache.php:
'warmers' => [ 'homepage:stats' => [ 'callback' => fn() => [ 'users' => User::count(), 'posts' => Post::count(), ], 'tags' => ['homepage', 'stats'], 'ttl' => 3600, ], ],
Warm cache via command:
php artisan cache:warm php artisan cache:warm --force
Pattern-based Clearing (Redis only)
// Clear all user cache keys CacheManager::clearByPattern('users:*');
Model Integration
Use the Cacheable trait in your models:
use DwiWijonarko\SimpleCache\Traits\Cacheable; class User extends Model { use Cacheable; protected $cachePrefix = 'user'; protected $cacheTags = ['users']; protected $cacheTtl = 3600; } // Usage $users = User::cached(); $user->cacheModel(); $user->clearCache();
Helper Functions
// Get cache manager instance $cache = simple_cache(); // Tagged cache helper cache_tags(['users', 'active'])->put('key', 'value');
Artisan Commands
# Display cache statistics php artisan cache:stats # Clear cache by tag php artisan cache:clear-tag users posts # Warm cache php artisan cache:warm php artisan cache:warm --force
Dashboard
Access the cache dashboard at: http://your-app.test/cache-dashboard
The dashboard provides:
- Real-time cache statistics
- Clear cache by tag
- Clear cache by pattern (Redis)
- Warm cache button
Configure dashboard access in config/simple-cache.php:
'dashboard_enabled' => true, 'dashboard_route' => 'cache-dashboard', 'dashboard_middleware' => ['web', 'auth'],
API Reference
CacheManager Methods
// Chainable configuration ->tags(array|string $tags) ->ttl(int $seconds) ->store(string $store) // Cache operations ->remember(string $key, Closure $callback, int $ttl = null) ->rememberForever(string $key, Closure $callback) ->put(string $key, $value, int $ttl = null) ->get(string $key, $default = null) ->has(string $key) ->forget(string $key) ->flush() // Advanced operations ->clearByPattern(string $pattern) ->warm(array $warmers) ->stats()
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Security
If you discover any security-related issues, please email dwi.wijonarko@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Support
For support, please open an issue on GitHub.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-11