oprokidnev/cacheable-rendering

此包的最新版本(dev-master)没有可用的许可证信息。

dev-master / 2.0.x-dev 2017-10-17 09:31 UTC

This package is auto-updated.

Last update: 2024-09-22 02:31:55 UTC


README

提供了一组用于HTML缓存的工具,包括占位符视图辅助函数调用拦截和恢复。

安装

composer require oprokidnev/cacheable-rendering:^2.0

在application.config.php中初始化模块 'Oprokidnev\\CacheableRendering' 并运行您的应用程序一次。然后您将在config/autoload中看到一个新文件名为 cacheable-rendering.config.local.php。这是一个包含存储设置的配置文件。默认情况下,我选择使用文件系统缓存适配器,但您可以使用兼容 \Zend\Cache\StorageFactory::factory 方法的数组来更改此设置。

视图辅助函数

CachedCallback

这是您应该使用回调缓存的最典型示例。

<?php
/*
 * Returned info is url dependant
 */
$key = $_SERVER['REQUEST_URI'];

/*
 * Cache big array rendering operation into html.
 */
echo $this->cachedCallback($key, function ($cacheTags) use ($serviceFromController) { ?>
    <?php foreach($veryBigIteratorWithLazyLoading->getItems() as $item): ?>
        <div class="item"><?= $item->getName()?></div>
    <?php endforeach; ?>
    <?php $this->placeholder('modals')->captureStart(); // placeholder call happens here ?>
    <div class="modal">
        <!-- Some modal body, that will be restored on cache read -->
    </div>
    <?php $this->placeholder('modals')->captureEnd(); ?>
    <?php
    $cacheTags[] = 'database_cache';
    return 'Some finish message';
}); ?>

CachedPartial

与标准部分辅助函数相同,只是在调用时使用 $key 参数。

echo $this->cachedPartial($someKey, $locationOrViewModal, $varsOrNothing);

CachedCapture

将昂贵的输出操作封装在 while 代码块中。适用于当 cachedCallback 闭包需要大量作用域变量时的情况。

<?php while ($this->cachedCapture($key  = 'capture', $tags = ['default'])): ?>
    <?php sleep(1); ?>
    some havy output action here.
<?php endwhile; ?>

与缓存存储交互