Home  >  Article  >  PHP Framework  >  Laravel extension recommendation: ORM caching package 'LaraCache'

Laravel extension recommendation: ORM caching package 'LaraCache'

青灯夜游
青灯夜游forward
2022-10-13 19:55:291816browse

Laravel extension recommendation: ORM caching package 'LaraCache'

LaraCache is an ORM-based Laravel package for creating, updating, and managing cache items based on model queries. Using this package, you can cache queries that are used 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();
                })
        ];
    }
}

Use the cacheEntities method to define cached queries and Laracache will handle the rest. To use cached queries, you would call the model as shown in the following example:

use Mostafaznv\LaraCache\Facades\LaraCache;

$cache = Article::cache()->get('latest');
// 或者
$cache = LaraCache::retrieve(Article::class, 'latest');

With this package, you can control caching using the following functions:

  • Enable/Disable Cache
  • Manually update cache
  • Manually update all cache entities
  • Delete cache
  • Use fluent method or ttl()Method control CacheEntity Duration

I think the following manual cache update method is very simple and can refresh the cache on the fly:

Article::cache()->update('latest');2// or3LaraCache::update(Article::class, 'latest');

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

Original address: https://laravel-news.com/laracache-orm-caching-package-for-laravel

Translation address: https://learnku.com/ laravel/t/68860

[Related recommendations: laravel video tutorial]

The above is the detailed content of Laravel extension recommendation: ORM caching package 'LaraCache'. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete