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

Laravel extension recommendation: ORM caching package 'LaraCache'

青灯夜游
Release: 2022-10-14 20:22:31
forward
1866 people have browsed it

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();
                })
        ];
    }
}
Copy after login

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');
Copy after login

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');
Copy after login

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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template