iazaran/smart-cache

Smart Cache is a caching optimization package designed to enhance the way your Laravel application handles data caching. It intelligently manages large data sets by compressing, chunking, or applying other optimization strategies to keep your application performant and efficient.

1.1.0 2025-04-23 07:48 UTC

This package is auto-updated.

Last update: 2025-04-23 08:10:13 UTC


README

Laravel SmartCache is a caching optimization package designed to enhance the way your Laravel application handles data caching. It intelligently manages large data sets by compressing, chunking, or applying other optimization strategies to keep your application performant and efficient.

๐Ÿš€ Features

  • ๐Ÿ” Auto-detects large cache payloads
  • ๐Ÿ“ฆ Compresses data before caching
  • ๐Ÿงฉ Chunks large arrays or objects into manageable parts
  • ๐Ÿง  Intelligent serialization
  • โ™ป๏ธ Seamless retrieval and reconstruction
  • โš™๏ธ Extensible strategy pattern for custom optimizations
  • ๐Ÿ›ก๏ธ Optional fallback for incompatible drivers
  • ๐Ÿ”„ Laravel-style helper function support

๐Ÿ“ฆ Installation

composer require iazaran/smart-cache

Laravel 8+ is supported. Tested with Redis and file cache drivers.

โš™๏ธ Configuration

After installation, publish the config file:

php artisan vendor:publish --tag=smart-cache-config

The config file allows you to define thresholds, compression strategies, chunk sizes, and enabled features.

๐Ÿงช Usage

Use SmartCache just like Laravel's Cache facade:

use SmartCache\Facades\SmartCache;

SmartCache::put('key', $largeData, now()->addMinutes(10));
$data = SmartCache::get('key');

Or use the global helper function, similar to Laravel's cache() helper:

// Get a value
$value = smart_cache('key');

// Get with default value
$value = smart_cache('key', 'default');

// Store a value
smart_cache(['key' => $largeData], now()->addMinutes(10));

// Access the SmartCache instance
$cache = smart_cache();

Or inject it into your services:

public function __construct(\SmartCache\Contracts\SmartCache $cache)
{
    $this->cache = $cache;
}

๐Ÿ”ง Optimization Strategies

SmartCache includes several strategies out of the box:

  • Compression: Uses gzip or other drivers
  • Chunking: Splits large data structures
  • Encoding: Serializes data safely
  • Driver-Aware: Avoids incompatible features based on driver

These strategies can be customized or extended by implementing SmartCache\Contracts\OptimizationStrategy.

๐Ÿ“‚ Example

$data = range(1, 10000);

SmartCache::put('numbers', $data, 600);

// Behind the scenes:
// - Checks size
// - Compresses or chunks as needed
// - Stores metadata for retrieval

// Or with helper function
smart_cache(['numbers' => $data], 600);

๐Ÿงฐ Artisan Commands

php artisan smart-cache:clear
php artisan smart-cache:status

Built with โค๏ธ for developers who care about performance.