anahkiasen / flatten
一个用于 Illuminate 框架的包,可以将页面转换为纯 HTML
Requires
- illuminate/cache: 5.0.*|5.1.*
- illuminate/config: 5.0.*|5.1.*
- illuminate/container: 5.0.*|5.1.*
- illuminate/contracts: 5.0.*|5.1.*
- illuminate/http: 5.0.*|5.1.*
- illuminate/support: 5.0.*|5.1.*
- mrclay/minify: ^2.2
Requires (Dev)
- fabpot/php-cs-fixer: ^1.10
- illuminate/view: 5.0.*|5.1.*
- mockery/mockery: ^0.9.4
- phpunit/phpunit: ^4.8
- symfony/var-dumper: ^2.7
README
Flatten 是一个强大的运行时缓存系统,用于在运行时缓存页面。它所做的工作非常简单:你告诉他哪些页面需要缓存,何时需要刷新缓存,然后 Flatten 会处理所有的事情。它将静默地将你的页面转换为纯 HTML 并存储它们。这意味着如果用户访问已经转换为 HTML 的页面,所有的 PHP 代码都会被劫持以显示简单的 HTML 页面。这将为你应用程序的速度提供必要的提升,因为只有当显示的数据发生变化时,页面缓存才会刷新。
设置
安装
Flatten 可以像任何其他包一样通过 Composer 安装:composer require anahkiasen/flatten
。
如果你使用 Laravel,请将 Flatten 的服务提供者添加到你的 config/app.php
文件中
Flatten\FlattenServiceProvider::class,
及其外观
'Flatten' => Flatten\Facades\Flatten::class,
配置
所有选项都在 config.php 配置文件中解释。你可以通过 artisan vendor:publish
命令发布它。
用法
页面是根据两个参数进行缓存的:它们的路径和它们的方法。只有 GET 请求被缓存,因为所有其他方法本质上都是动态的。你将进行的所有调用都将通过 Flatten\Facades\Flatten
外观进行。缓存会考虑查询字符串,具有不同查询字符串的页面将具有不同的缓存。
构建
Flatten 可以通过 artisan flatten:build
命令缓存你的应用程序中所有授权的页面。它将爬取你的应用程序并从页面到页面进行,缓存你允许它缓存的页面。
刷新
有时你可能想要刷新特定的页面或模式。例如,如果你缓存了用户的个人资料,那么当用户编辑其信息时,你可能想要刷新这些页面。你可以通过以下方法完成此操作
// Manual flushing Flatten::flushAll(); Flatten::flushPattern('users/.+'); Flatten::flushUrl('http://localhost/users/taylorotwell'); // Flushing via an UrlGenerator Flatten::flushRoute('user', 'taylorotwell'); Flatten::flushAction('UsersController@user', 'taylorotwell'); // Flushing template sections (see below) Flatten::flushSection('articles');
你还可以直接注入负责的类,例如在模型观察者类中
use Flatten\CacheHandler; class UserObserver { protected $cache; public function __construct(CacheHandler $cache) { $this->cache = $cache; } public function saved(User $user) { $this->cache->flushRoute('users.show', $user->id); } }
运行时缓存
你不必缓存整个页面,你可以对较小的缓存部分进行微调。
在 PHP 中,你可以这样做
<h1>This will always be dynamic</h1> <?php foreach ($articles as $article): ?> <?= $article->name ?> <?php endforeach; ?> <h1>This will not</h1> <?php Flatten::section('articles', function () use ($articles) { ?> <?php foreach ($articles as $article): ?> <?= $article->name ?> <?php endforeach; ?> <?php }); ?>
你还可以通过向 section
添加一个参数来指定该部分缓存多长时间
<!-- This section will be cached for 10 minutes --> <?php Flatten::section('articles', 10, function () use ($articles) { ?> <?php foreach ($articles as $article): ?> <?= $article->name ?> <?php endforeach; ?> <?php }); ?>
Flatten 还为 Blade 模板引擎提供钩子以实现更简洁的语法。让我们重新编写上面的示例
<h1>This will always be dynamic</h1> @foreach($articles as $articles) {{ $article->name }} @endforeach <h1>This will not</h1> @cache('articles', 10) @foreach($articles as $article) {{ $article->name }} @endforeach @endcache
启动
你可以通过使用 Flatten::kickstart
方法进一步加快 Flatten 的速度。它需要更多的模板代码,但可以显著提高性能。基本上,你希望在 一切 之前调用它(即甚至在加载 Composer 之前)。在 Laravel 中,你将在 bootstrap/autoload.php
的顶部放置此代码。
你可以这样使用它
require __DIR__.'/../vendor/anahkiasen/flatten/src/Flatten/Flatten.php'; Flatten\Flatten::kickstart();
如果你在盐罐里有东西,你需要找到更快的方法来获取这些并作为参数传递盐
require __DIR__.'/../vendor/anahkiasen/flatten/src/Flatten/Flatten.php'; $salt = mysql_query('SOMETHING'); Flatten\Flatten::kickstart($salt);
在 Laravel 之外使用
Flatten 可以轻松地在 Laravel 之外使用,为此你基本上只会使用两个方法。你基本上想在页面的顶部调用 Flatten::start
,并在底部调用 Flatten::end
。
<?php require 'vendor/autoload.php'; use Flatten\Facades\Flatten; ?> <?php Flatten::start() ?> <!DOCTYPE html> <head></head> <body></body> </html> <?php Flatten::end() ?>