PHPackages                             bunkuris/has-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. bunkuris/has-cache

ActiveLibrary[Caching](/categories/caching)

bunkuris/has-cache
==================

HasCache trait for Laravel models with cache key managers

0.1.1(7mo ago)02.1k↓45%MITPHPPHP ^8.2CI passing

Since Oct 6Pushed 1mo agoCompare

[ Source](https://github.com/DavisPuciriuss/has-cache)[ Packagist](https://packagist.org/packages/bunkuris/has-cache)[ RSS](/packages/bunkuris-has-cache/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (15)Versions (3)Used By (0)

HasCache
========

[](#hascache)

[![Latest Version on Packagist](https://camo.githubusercontent.com/82e60beebd17dab39d23ab8d0da0b248db024447219569a1b9e158a23a29bc46/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f62756e6b757269732f6861732d63616368652e737667)](https://packagist.org/packages/bunkuris/has-cache)[![Total Downloads](https://camo.githubusercontent.com/b663290155e3d36f598f0ba89ae2da3e28916324245618007ab7a87d5d51d9af/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f62756e6b757269732f6861732d63616368652e737667)](https://packagist.org/packages/bunkuris/has-cache)[![PHP Version](https://camo.githubusercontent.com/623cc4376844622e37bb685566bcf7f510c4a3c5c76870acc8e0223c1d4148cb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f62756e6b757269732f6861732d63616368652e737667)](https://packagist.org/packages/bunkuris/has-cache)[![Tests](https://camo.githubusercontent.com/a637d5b8755f4d281febd8638b8fd00f638c5a1968e85728466b95f10dd5a9f2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f44617669735075636972697573732f6861732d63616368652f74657374732e796d6c3f6272616e63683d6d6173746572266c6162656c3d7465737473)](https://github.com/DavisPuciriuss/has-cache/actions/workflows/tests.yml)[![Coverage](https://camo.githubusercontent.com/521f49964c752ccb2e5ff5e12797fdd8e87f17afa287fd78e677e819d3a1534c/68747470733a2f2f636f6465636f762e696f2f67682f44617669735075636972697573732f6861732d63616368652f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/DavisPuciriuss/has-cache)

HasCache is a package that provides automatic cache invalidation for Laravel Eloquent models. It monitors model lifecycle events (create, update, delete) and automatically clears associated cache keys, preventing stale data without manual cache management. The package includes Redis-optimized async cache deletion, working hours-aware TTL management, and a type-safe cache key manager system.

Features
--------

[](#features)

- **Automatic Cache Invalidation**: Clears cache automatically when models are saved, updated, or deleted
- **Transaction-Aware**: Respects database transactions and clears cache after commit
- **Async Cache Deletion**: Non-blocking cache clearing using Redis UNLINK command
- **Working Hours TTL**: Different cache TTLs for business hours vs off-hours
- **Type-Safe Cache Keys**: PHPStan-friendly cache key managers with IDE autocomplete
- **Testing Helpers**: Built-in testing utilities to assert cache operations
- **Temporary Disable**: Can temporarily disable cache purging when needed
- **Chunk Processing**: Efficiently handles large numbers of cache keys

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

[](#requirements)

- PHP 8.2+
- Laravel 10.0+

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

[](#installation)

You can install this package as a typical composer package.

```
composer require bunkuris/has-cache
```

Publish the configuration file (optional):

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

Basic Usage
-----------

[](#basic-usage)

### Step 1: Add the HasCache Trait to Your Model

[](#step-1-add-the-hascache-trait-to-your-model)

```
