Laracache
LaraCache is an ORM based package for Laravel to create, update and manage cache items based on model queries
Install / Use
/learn @mostafaznv/LaracacheREADME
LaraCache
Using this package, you can cache your heavy and most used queries.
All you have to do is to define the CacheEntity objects in the model and specify a valid name and ttl for them.
LaraCache will handle the rest of process automatically. It will create and update cache entities based on ttl that you've defined for each entity.
Manually updating the cache entities of models after dispatching model events (creating, updating and deleting) isn't required, LaraCache manages them in the background and ensures the most up-to-date version of each cache entity.
In addition to the core LaraCache package, I have developed a complementary package called Nova LaraCache. Nova LaraCache seamlessly integrates LaraCache with Laravel Nova, the administration panel for Laravel applications. It offers a user-friendly interface within the Laravel Nova administration panel, enabling users to conveniently moderate and manage cache entities.
I am on an open-source journey 🚀, and I wish I could solely focus on my development path without worrying about my financial situation. However, as life is not perfect, I have to consider other factors.
Therefore, if you decide to use my packages, please kindly consider making a donation. Any amount, no matter how small, goes a long way and is greatly appreciated. 🍺
Requirements:
- PHP 8.3 or higher
- Laravel 12 or higher
To install LaraCache on older Laravel/PHP versions, consult the compatibility table below to determine which LaraCache release to use:
| Laravel Version | PHP Version | LaraCache Version | |-----------------|---------------|-------------------------------------------------------------| | 8.40.0 — 12.x | 8.0.2 — 8.5 | 2.5.2 | | 12.x | 8.3 or higher | master (3.x) |
Installation
-
Install the package via composer:
composer require mostafaznv/laracache -
Publish config file:
php artisan vendor:publish --provider="Mostafaznv\LaraCache\LaraCacheServiceProvider" -
Done
Usage
-
Add LaraCache trait to the model
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Mostafaznv\LaraCache\Traits\LaraCache; class Article extends Model { use LaraCache; /** * Define Cache Entities Entities * * @return CacheEntity[] */ public static function cacheEntities(): array { return [ CacheEntity::make('list.forever') ->cache(function() { return Article::query()->latest()->get(); }), CacheEntity::make('latest') ->validForRestOfDay() ->cache(function() { return Article::query()->latest()->first(); }) ]; } } -
Retrieve Cache
use App\Models\Article; use Mostafaznv\LaraCache\Facades\LaraCache; $cache = Article::cache()->get('latest'); // or $cache = LaraCache::retrieve(Article::class, 'latest');
Table of Contents:
- Installation
- Usage
- CacheEntity Methods
- Disable/Enable Cache
- Update Cache Manually
- Delete Cache Manually
- Artisan Commands
- Laravel Nova Support
- Config Properties
- Complete Example
CacheEntity Methods
| method | Arguments | description |
|---------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| setDriver | driver (type: string) | Specifies custom driver for cache entity |
| isQueueable | status (type: bool, default: 'true')<hr>onConnection (type: string, default: '')<hr>onQueue (type: string, default: '') | This option specifies whether the cache operation should be performed in the background or not.<br>Note: By using the onConnection and onQueue arguments, you have the ability to specify custom connection and queue names for each cache entity. |
| debounce | status (type: bool, default: 'true')<hr>waitTime (type: int, default: 5)<hr>onConnection (type: string, default: '')<hr>onQueue (type: string, default: '') | This option specifies whether the cache operation should be debounced or not.<br>Note: To use the debouncing mechanism, a proper queue system must be configured and running. |
| refreshAfterCreate | status (type: bool, default: true) | Specifies if the cache should refresh after create a record |
| refreshAfterUpdate | status (type: bool, default: true) | Specifies if the cache should refresh after update a record |
| refreshAfterDelete | status (type: bool, default: true) | Specifies if the cache should refresh after delete a record |
| refreshAfterRestore | status (type: bool, default: true) | Specifies if the cache should refresh after restore a record |
| forever |
Related Skills
node-connect
339.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
83.8kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
339.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
83.8kCommit, push, and open a PR
