• Reading time ~ 1 min
  • 02.07.2022

LaraCache is an ORM-based package for Laravel to create, update and manage cache items based on model queries. Using this package, you can cache queries that you use heavily throughout your application.

use Mostafaznv\LaraCache\Traits\LaraCache;
 
class Article extends Model
{
    use LaraCache;
 
    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();
                })
        ];
    }
}

Using the cacheEntities method to define cached queries, Laracache will take care of the rest. To use the cached queries, you'll call the model like the following example:

use Mostafaznv\LaraCache\Facades\LaraCache;
 
 
$cache = Article::cache()->get('latest');
// or
$cache = LaraCache::retrieve(Article::class, 'latest');

With this package, you can control cache with the following features:

  • Enable/disable cache
  • Update cache manually
  • Update all cache entities manually
  • delete cache
  • Control CacheEntity duration using fluent methods or a ttl() method

I thought the following manual cache update method was neat, refreshing your cache on-the-fly:

Article::cache()->update('latest');
// or
LaraCache::update(Article::class, 'latest');

You can learn about this package, get full installation instructions, and view the source code on GitHub.

Comments

No comments yet
Yurij Finiv

Yurij Finiv

Full stack

ABOUT

Professional Fullstack Developer with extensive experience in website and desktop application development. Proficient in a wide range of tools and technologies, including Bootstrap, Tailwind, HTML5, CSS3, PUG, JavaScript, Alpine.js, jQuery, PHP, MODX, and Node.js. Skilled in website development using Symfony, MODX, and Laravel. Experience: Contributed to the development and translation of MODX3 i...

About author CrazyBoy49z
WORK EXPERIENCE
Contact
Ukraine, Lutsk
+380979856297