atiksoftware / static-cache
将响应作为静态HTML文件缓存到磁盘上,以实现页面加载速度极快。
Requires
- php: >=5.5.9
- illuminate/contracts: 5.0 - 5.8|^6.0|^7.0|^8.0|^9.0
- illuminate/filesystem: 5.0 - 5.8|^6.0|^7.0|^8.0|^9.0
- symfony/http-foundation: 2.6 - 4|^5.0|^6.0
Suggests
- illuminate/console: Allows clearing the cache via artisan
README
此包允许您轻松地将响应作为静态文件缓存到磁盘上,以实现页面加载速度极快。
注意:此包受到JosephSilber/page-cache的启发。
简介
尽管静态网站生成器如Jekyll和Jigsaw在当今非常受欢迎,但动态PHP网站仍然为大多数静态网站提供了许多价值。一个合适的PHP网站允许您轻松地将动态功能添加到所需的地方,并且还意味着在推送网站更新时不需要进行构建步骤。
话虽如此,对于网站上的真正静态页面,确实没有必要启动一个完整的PHP应用程序来仅用于服务静态页面。从磁盘上提供简单的HTML页面要快得多,对服务器的压力也小得多。
解决方案?全页缓存。
使用此包中包含的中间件,您可以有选择地将对任何给定请求的响应缓存到磁盘上。对同一页面的后续调用将直接作为静态HTML页面提供服务!
安装
使用Composer安装static-cache
包
$ composer require atiksoftware/static-cache
服务提供商
注意:如果您正在使用Laravel 5.5+,服务提供商将自动注册。您可以完全跳过此步骤。
打开config/app.php
并将新项目添加到providers
数组中
Atiksoftware\StaticCache\LaravelServiceProvider::class,
中间件
打开app/Http/Kernel.php
并将新项目添加到web
中间件组中
protected $middlewareGroups = [ 'web' => [ \Atiksoftware\StaticCache\Middleware\CacheResponse::class, /* ... keep the existing middleware here */ ], ];
中间件足够智能,只会缓存具有200 HTTP状态代码的响应,并且仅针对GET请求。
如果您只想有选择地缓存对您网站的特定请求,则应向routeMiddleware
数组添加新映射
protected $routeMiddleware = [ 'static-cache' => \Atiksoftware\StaticCache\Middleware\CacheResponse::class, /* ... keep the existing mappings here */ ];
注册后,您可以在单个路由上使用此中间件。
URL重写
为了在缓存后直接提供静态文件,您需要正确配置您的Web服务器以检查这些静态文件。
-
对于nginx
更新
location
块的try_files
指令,以包括对static-cache
目录的检查location = / { try_files /static-cache/__index.html /index.php?$query_string; } location / { try_files $uri $uri/ /static-cache/$uri.html /static-cache/$uri.json /static-cache/$uri.xml /index.php?$query_string; }
-
对于apache
打开
public/.htaccess
并在标签为Handle Front Controller
的块之前添加以下内容# Serve Cached Page If Available... RewriteCond %{REQUEST_URI} ^/?$ RewriteCond %{DOCUMENT_ROOT}/static-cache/__index.html -f RewriteRule .? static-cache/__index.html [L] RewriteCond %{DOCUMENT_ROOT}/static-cache%{REQUEST_URI}.html -f RewriteRule . static-cache%{REQUEST_URI}.html [L] RewriteCond %{DOCUMENT_ROOT}/static-cache%{REQUEST_URI}[%{QUERY_STRING}].html -f RewriteRule . static-cache%{REQUEST_URI}[%{QUERY_STRING}].html [L] RewriteCond %{DOCUMENT_ROOT}/static-cache%{REQUEST_URI}.json -f RewriteRule . static-cache%{REQUEST_URI}.json [L] RewriteCond %{DOCUMENT_ROOT}/static-cache%{REQUEST_URI}[%{QUERY_STRING}].json -f RewriteRule . static-cache%{REQUEST_URI}[%{QUERY_STRING}].json [L] RewriteCond %{DOCUMENT_ROOT}/static-cache%{REQUEST_URI}.xml -f RewriteRule . static-cache%{REQUEST_URI}.xml [L] RewriteCond %{DOCUMENT_ROOT}/static-cache%{REQUEST_URI}[%{QUERY_STRING}].xml -f RewriteRule . static-cache%{REQUEST_URI}[%{QUERY_STRING}].xml [L]
忽略缓存文件
为了确保您不会将本地缓存的文件提交到git仓库,请将此行添加到您的.gitignore
文件中
/public/static-cache
使用方法
使用中间件
注意:如果您已将中间件添加到全局
web
组,则所有成功的GET请求将自动缓存。无需再次将中间件直接放在路由上。如果您将其注册为路由中间件,则应在您希望缓存的任何路由上使用该中间件。
要缓存给定请求的响应,请使用static-cache
中间件
Route::middleware('static-cache')->get('/haberler', [\App\Http\Controllers\App\PostController::class, 'index'])->name('posts'); Route::middleware('static-cache')->get('/haberler/kategori/{category:slug}', [\App\Http\Controllers\App\PostController::class, 'by_category'])->scopeBindings()->name('posts.by_category'); Route::middleware('static-cache')->get('/haberler/ilce/{district:slug}', [\App\Http\Controllers\App\PostController::class, 'by_district'])->scopeBindings()->name('posts.by_district'); Route::middleware('static-cache')->get('/haberler/yazar/{user:slug}', [\App\Http\Controllers\App\PostController::class, 'by_user'])->scopeBindings()->name('posts.by_user'); Route::middleware('static-cache')->get('/haber/{post:slug}', [\App\Http\Controllers\App\PostController::class, 'view'])->scopeBindings()->name('post'); Route::middleware('static-cache')->get('/haberler/{tag:name}', [\App\Http\Controllers\App\PostController::class, 'by_tags'])->name('posts.by_tags');
现在,每篇帖子都将缓存在public/static-cache
目录下的文件中,与请求的URL结构紧密匹配。对这篇帖子的后续请求将直接从磁盘提供,甚至不会触及您的应用程序!
清除缓存
由于响应被缓存到磁盘上的静态文件中,因此您应用中对这些页面的任何更新都不会反映在您的网站上。要更新您网站上的页面,您应该使用以下命令清除缓存
php artisan static-cache:clear
一般来说,将此操作添加到您的部署脚本中是一种良好的做法。这样,每当您向网站推送更新时,页面缓存将自动清除。
如果您正在使用 Forge 的快速部署功能,您应该将此行添加到部署脚本的末尾。这将确保在您向网站推送更新时清除缓存。
您可以选择向命令传递一个URL别名,以仅删除特定页面的缓存
php artisan static-cache:clear posts/first-post
php artisan static-cache:clear posts*
php artisan static-cache:clear posts/first*
自定义要缓存的内容
默认情况下,所有带有200 HTTP响应代码的GET请求都会被缓存。如果您想更改这一点,请创建自己的中间件,扩展该包的基本中间件,并使用您自己的逻辑覆盖shouldCache
方法。
-
运行
make:middleware
Artisan命令以创建中间件文件php artisan make:middleware CacheResponse
-
用以下内容替换
app/Http/Middleware/CacheResponse.php
文件的内容<?php namespace App\Http\Middleware; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Atiksoftware\StaticCache\Middleware\CacheResponse as BaseCacheResponse; class CacheResponse extends BaseCacheResponse { protected function shouldCache(Request $request, Response $response) { // In this example, we don't ever want to cache pages if the // URL contains a query string. So we first check for it, // then defer back up to the parent's default checks. if ($request->getQueryString()) { return false; } return parent::shouldCache($request, $response); } }
-
最后,更新
app/Http/Kernel.php
文件中的中间件引用,以指向您自己的中间件。
许可证
页面缓存包是开源软件,使用MIT许可。