anahkiasen/flatten 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

anahkiasen/flatten

Composer 安装命令:

composer require anahkiasen/flatten

包简介

A package for the Illuminate framework that flattens pages to plain HTML

README 文档

README

Build Status Latest Stable Version Total Downloads Scrutinizer Quality Score Code Coverage Dependency Status Support via Gittip

Flatten is a powerful cache system for caching pages at runtime. What it does is quite simple : you tell him which page are to be cached, when the cache is to be flushed, and from there Flatten handles it all. It will quietly flatten your pages to plain HTML and store them. That whay if an user visit a page that has already been flattened, all the PHP is highjacked to instead display a simple HTML page. This will provide an essential boost to your application's speed, as your page's cache only gets refreshed when a change is made to the data it displays.

Setup

Installation

Flatten installs just like any other package, via Composer : composer require anahkiasen/flatten.

Then if you're using Laravel, add Flatten's Service Provider to you config/app.php file :

Flatten\FlattenServiceProvider::class,

And its facade :

'Flatten' => Flatten\Facades\Flatten::class,

Configuration

All the options are explained in the config.php configuration file. You can publish it via artisan vendor:publish.

Usage

The pages are cached according to two parameters : their path and their method. Only GET requests get cached as all the other methods are dynamic by nature. All of the calls you'll make, will be to the Flatten\Facades\Flatten facade. Query strings are taken into account in the cache and pages will different query strings will have different caches.

Building

Flatten can cache all authorized pages in your application via the artisan flatten:build command. It will crawl your application and go from page to page, caching all the pages you allowed him to.

Flushing

Sometimes you may want to flush a specific page or pattern. If per example you cache your users's profiles, you may want to flush those when the user edit its informations. You can do so via the following methods :

// 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');

You can also directly inject the responsible class, in a model observer class per example:

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);
    }
}

Runtime caching

You don't have to cache all of a page, you can fine-tune your cache in smaller cached sections.

In PHP you'd do it like this :

<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 }); ?>

You can also specify for how long you want that section to be cached by adding an argument to 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 also hooks into the Blade templating engine for a leaner syntax. Let's rewrite our above example :

<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

Kickstarting

You can speed up Flatten even more by using the Flatten::kickstart method. It requires a little more boilerplate code but can enhance performances dramatically. Basically you want to call that before everything else (ie. before even loading Composer). On Laravel you'd put that code at the top of bootstrap/autoload.php.

You use it like that :

require __DIR__.'/../vendor/anahkiasen/flatten/src/Flatten/Flatten.php';

Flatten\Flatten::kickstart();

If you have things in your saltshaker, you'll need to find faster raw methods to get these and pass the salts as arguments :

require __DIR__.'/../vendor/anahkiasen/flatten/src/Flatten/Flatten.php';

$salt = mysql_query('SOMETHING');
Flatten\Flatten::kickstart($salt);

Using outside of Laravel

Flatten can easily be used outside of Laravel, for this you'll basically only ever use two methods. What you basically want to do is call Flatten::start at the top of the page, and Flatten::end at the bottom.

<?php
require 'vendor/autoload.php';

use Flatten\Facades\Flatten;
?>

<?php Flatten::start() ?>
<!DOCTYPE html>
	<head></head>
	<body></body>
</html>
<?php Flatten::end() ?>

anahkiasen/flatten 适用场景与选型建议

anahkiasen/flatten 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.03k 次下载、GitHub Stars 达 331, 最近一次更新时间为 2013 年 01 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「performance」 「cache」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 anahkiasen/flatten 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 anahkiasen/flatten 我们能提供哪些服务?
定制开发 / 二次开发

基于 anahkiasen/flatten 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 13.03k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 333
  • 点击次数: 31
  • 依赖项目数: 1
  • 推荐数: 2

GitHub 信息

  • Stars: 331
  • Watchers: 12
  • Forks: 41
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-01-13