SkillAgentSearch skills...

Laracache

LaraCache is an ORM based package for Laravel to create, update and manage cache items based on model queries

Install / Use

/learn @mostafaznv/Laracache
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

LaraCache

GitHub Workflow Status Codecov branch Quality Score GitHub license Packagist Downloads Latest Version on Packagist

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. 🍺

Donate


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

  1. Install the package via composer:
    composer require mostafaznv/laracache
    
  2. Publish config file:
    php artisan vendor:publish --provider="Mostafaznv\LaraCache\LaraCacheServiceProvider"
    
  3. Done

Usage

  1. 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();
                    })
            ];
        }
    }
    
  2. 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:

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

View on GitHub
GitHub Stars272
CategoryDevelopment
Updated3mo ago
Forks7

Languages

PHP

Security Score

97/100

Audited on Dec 16, 2025

No findings