定制 label-li/laravel-page-cache 二次开发

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

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

label-li/laravel-page-cache

Composer 安装命令:

composer require label-li/laravel-page-cache

包简介

Page Cache for Laravel PHP Static Page

README 文档

README

Latest Stable Version License

This package allows you to easily cache responses as static files on disk for lightning fast page loads.

This package can cache static PHP page on local disk to provide a fast loading of pages

Introduction

It would be a heavy loading if lots of visitors plus the page loading lots of content from database with lots of logic Page cache can reduce the loading.

installation

  1. Install package with composer:
composer require label-li/laravel-page-cache
  1. Publish the config to config/pagecache.config
php artisan vendor:publish --provider="LabelLi\LaravelPageCache\PageCacheServiceProvider"

Middleware

If you want to cache all route in middleware web, i.e., all http traffics:

  1. Open app/Http/Kernel.php
  2. Copy \LabelLi\LaravelPageCache\Middleware\CacheResponse::class, as a new line to the web middleware group:
protected $middlewareGroups = [
    'web' => [
        \LabelLi\LaravelPageCache\Middleware\CacheResponse::class,
        ...
    ],
];

If you want to cache specific route page-cache:

  1. Open app/Http/Kernel.php
  2. Copy 'page-cache' => \LabelLi\LaravelPageCache\Middleware\CacheResponse::class, as a new line to the $routeMiddleware:
protected $routeMiddleware = [
    'page-cache' => \LabelLi\LaravelPageCache\Middleware\CacheResponse::class,
    ...
];

Config

Change the value in config/pagecache.php to what you want:

'CacheTime' => 3600, // In seconds
'CachePath' => 'pagecache', // In storage

Usage

Using the middleware

If you choose to cache specific route, e.g. page-cache, use the page-cache middleware as follow:

Route::middleware('page-cache')->get('/page', 'PageController@show');

/page will be cached to a file under the storage/**$CachePath** directory.

Clearing the cache

The cache will be cleared with the CacheTime set in config/pagecache.php. It is default 3600s.

If you want to clear it manually, please run the following command

php artisan PageCache:clear

You may optionally pass a URL slug to the command, to only delete the cache for a specific page:

php artisan PageCache:clear {slug}

To clear everything under a given path, use the --recursive flag:

php artisan PageCache:clear {slug} --recursive

Remark

If the Console function is not working, most likely to be the problem of permission for the cache folder. Please check with ls -la to check for the permission.

Credit

Thanks to Joseph Silber. The idea to run cache function directly at middleware is from his package. His package is very great but I have different usage. So I write another page cache package to archive my usage.

统计信息

  • 总下载量: 90
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-08-15