ORM Caching Package for Laravel

Packages

June 17th, 2022

ORM Caching Package for Laravel

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.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.