PHPackages                             yared/laravel-smart-cache - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. yared/laravel-smart-cache

ActiveLibrary[Caching](/categories/caching)

yared/laravel-smart-cache
=========================

Automatic cache invalidation for Laravel — clear cache when models change, no manual work

00PHP

Since Mar 15Pushed 1mo agoCompare

[ Source](https://github.com/yared-ayele-debela/laravel-auto-cache)[ Packagist](https://packagist.org/packages/yared/laravel-smart-cache)[ RSS](/packages/yared-laravel-smart-cache/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel Smart Cache
===================

[](#laravel-smart-cache)

**Automatic cache invalidation for Laravel — clear cache when models change, no manual work.**

[![Latest Version](https://camo.githubusercontent.com/0cf80cbb1bab67cafaadec2420a6ff3f0aec97eb79e8520db754eba98953b719/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f79617265642f6c61726176656c2d736d6172742d63616368652e737667)](https://packagist.org/packages/yared/laravel-smart-cache)[![License](https://camo.githubusercontent.com/b5e874f03267d36707f95c4b5223f63f0b9eb3a6f9ec86b709cfbe23f674b954/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f79617265642f6c61726176656c2d736d6172742d63616368652e737667)](https://packagist.org/packages/yared/laravel-smart-cache)

The Problem
-----------

[](#the-problem)

In Laravel, developers write cache like this:

```
Cache::remember('products', 3600, function () {
    return Product::all();
});
```

But when a product changes:

```
Product::create([...]);
Product::update([...]);
Product::delete();
```

The cache still contains old data. Developers must manually clear it:

```
Cache::forget('products');
```

This becomes messy and error-prone.

The Solution
------------

[](#the-solution)

Smart Cache automatically clears cache when related models change.

```
use SmartCache;

// Register the dependency
SmartCache::watch(Product::class, 'products');

// Use it
SmartCache::remember('products', function () {
    return Product::all();
});
```

Now when `Product::create()`, `Product::update()`, or `Product::delete()` runs, the cache is **automatically cleared**. No manual work.

Installation
------------

[](#installation)

```
composer require yared/laravel-smart-cache
```

Publish config (optional):

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

Quick Start
-----------

[](#quick-start)

### 1. Register model → cache mapping

[](#1-register-model--cache-mapping)

```
use SmartCache;

SmartCache::watch(Product::class, 'products');
```

Or watch multiple keys:

```
SmartCache::watch(Product::class, ['products', 'homepage_products']);
```

### 2. Use SmartCache instead of Cache

[](#2-use-smartcache-instead-of-cache)

```
$products = SmartCache::remember('products', function () {
    return Product::all();
});
```

That's it. When any `Product` is created, updated, deleted, or restored, the `products` cache is automatically invalidated.

Core Features
-------------

[](#core-features)

### Automatic Model Watching

[](#automatic-model-watching)

The package listens to Laravel model events:

- `created`
- `updated`
- `deleted`
- `restored`

### Cache Dependency Mapping

[](#cache-dependency-mapping)

Map one model to multiple cache keys:

```
SmartCache::watch(Product::class, [
    'products',
    'homepage_products',
    'featured_products',
]);
```

### Cache Tag Support (Redis / Memcached)

[](#cache-tag-support-redis--memcached)

For tagged caches:

```
SmartCache::tags(['products'])
    ->watch(Product::class)
    ->remember('list', fn () => Product::all());
```

When a product changes, the entire `products` tag is flushed.

### Smart Query Hash Cache

[](#smart-query-hash-cache)

Automatically generate cache key from query:

```
$products = SmartCache::query(
    Product::where('category_id', 1)->where('price', '>', 100)
);
```

The cache key is generated from the query hash. When any `Product` changes, related query caches are invalidated.

### CacheableModel Trait

[](#cacheablemodel-trait)

Define cache keys directly on your model:

```
use Yared\SmartCache\Traits\CacheableModel;

class Product extends Model
{
    use CacheableModel;

    public static function cacheKeys(): array
    {
        return ['products', 'homepage_products'];
    }
}
```

Configuration
-------------

[](#configuration)

Publish the config file:

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

### Config-based mappings

[](#config-based-mappings)

Define mappings in `config/smart-cache.php`:

```
'mappings' => [
    'App\Models\Product' => ['products', 'homepage_products'],
    'App\Models\Category' => ['categories', 'menu_categories'],
],
```

### Debug mode

[](#debug-mode)

Enable to log cache hits, misses, and invalidations:

```
SMART_CACHE_DEBUG=true
```

### Dashboard

[](#dashboard)

Enable the cache monitoring dashboard:

```
SMART_CACHE_DASHBOARD=true
```

Then visit `/cache-monitor` to see:

- Cache hits / misses / hit rate
- Invalidations
- Model → cache mappings

Advanced Usage
--------------

[](#advanced-usage)

### Custom TTL

[](#custom-ttl)

```
SmartCache::remember('products', fn () => Product::all(), 7200); // 2 hours
```

### Manual invalidation

[](#manual-invalidation)

```
SmartCache::forget('products');
```

### Invalidate by model (programmatic)

[](#invalidate-by-model-programmatic)

```
SmartCache::invalidateForModel(Product::class, 'Manual refresh');
```

Package Structure
-----------------

[](#package-structure)

```
laravel-smart-cache/
├── src/
│   ├── Services/
│   │   ├── CacheManager.php
│   │   ├── CacheWatcher.php
│   │   └── TaggedCacheManager.php
│   ├── Listeners/
│   │   └── ModelEventListener.php
│   ├── Debug/
│   │   └── CacheDebugger.php
│   ├── Facades/
│   │   └── SmartCache.php
│   ├── Traits/
│   │   └── CacheableModel.php
│   └── SmartCacheServiceProvider.php
├── config/
│   └── smart-cache.php
└── README.md

```

Requirements
------------

[](#requirements)

- PHP 8.2+
- Laravel 10.x, 11.x, or 12.x
- Illuminate Cache (Redis/Memcached for tag support)

License
-------

[](#license)

MIT

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance59

Moderate activity, may be stable

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity11

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/5727fb2630a34a7641505a6a706230943683ed204aa0fa244343ae5622f61872?d=identicon)[yared-ayele-debela](/maintainers/yared-ayele-debela)

---

Top Contributors

[![yared-ayele-debela](https://avatars.githubusercontent.com/u/112660399?v=4)](https://github.com/yared-ayele-debela "yared-ayele-debela (3 commits)")

### Embed Badge

![Health badge](/badges/yared-laravel-smart-cache/health.svg)

```
[![Health](https://phpackages.com/badges/yared-laravel-smart-cache/health.svg)](https://phpackages.com/packages/yared-laravel-smart-cache)
```

###  Alternatives

[predis/predis

A flexible and feature-complete Redis/Valkey client for PHP.

7.8k305.7M2.4k](/packages/predis-predis)[snc/redis-bundle

A Redis bundle for Symfony

1.0k39.4M67](/packages/snc-redis-bundle)[react/cache

Async, Promise-based cache interface for ReactPHP

444112.4M40](/packages/react-cache)[wp-media/wp-rocket

Performance optimization plugin for WordPress

7431.3M3](/packages/wp-media-wp-rocket)[illuminate/cache

The Illuminate Cache package.

12835.6M1.4k](/packages/illuminate-cache)[colinmollenhour/php-redis-session-abstract

A Redis-based session handler with optimistic locking

6325.6M14](/packages/colinmollenhour-php-redis-session-abstract)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
