定制 juampi92/laravel-query-cache 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

juampi92/laravel-query-cache

最新稳定版本:v1.2.0

Composer 安装命令:

composer require juampi92/laravel-query-cache

包简介

Provide easy interface for caching laravel queries

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package provides a set of macros to cache your Laravel Queries just like Cache::remember.

$featuredPost = Post::published()->orderByMostViews()
    ->cacheDay('post:featured') // <- Here
    ->first();

Installation

You can install the package via composer:

composer require juampi92/laravel-query-cache

That's it! No config or Trait necessary. The package auto-discovery will boot the macros.

Usage

Instead of doing:

Cache::remember('post:count', $ttl, () => Post::published()->count());

You now do:

Post::published()->cache('post:count', $ttl)->count();

You can use it in Eloquent Queries as well as in normal Queries.

DB::table('posts')
    ->whereNotNull('published_at')
    ->latest()
    ->cacheHour('post:latest')
    ->first();

Posts::published()
    ->cacheHour('post:count')
    ->count();

List of macros:

Post::cache('cache:key', $ttl)->get();
Post::cacheMinute('cache:key')->first();
Post::cacheHour('cache:key')->pluck('id');
Post::cacheDay("cache:key:$id")->find($id);
Post::cacheWeek('cache:key:paginate:10')->paginate(10);
Post::cacheForever('cache:key')->count();

Advanced usage

Different store

Post::query()
    ->where(...)
    ->cache('post:count')->store('redis')
    ->count();

Add your custom cache duration

This is maybe more advanced, but you can do so by opting out of discovery, and then importing it yourself:

use Juampi92\LaravelQueryCache\LaravelQueryCacheServiceProvider as BaseServiceProvider;

class LaravelQueryCacheServiceProvider extends BaseServiceProvider
{
    protected function getCustomTimes(): array
    {
        return array_merge(
            parent::getCustomTimes(),
            [
                'rememberForever' => null,
                'cacheFifteenMinutes' => 60 * 15,
            ]
        );
    }
}

The original method has

[
    'cacheForever' => null,
    'cacheMinute' => 60,
    'cacheHour' => 60 * 60,
    'cacheDay' => 60 * 60 * 24,
    'cacheWeek' => 60 * 60 * 24 * 7,
]

Disclaimer

This package is supposed to be a nice integration of Cache remember inside the Query Builder. If you're looking for a advanced eloquent specific cache, I recommend to check out laravel-eloquent-query-cache.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 17.25k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 25
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 25
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-03-05

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固