oprokidnev/cacheable-rendering

There is no license information available for the latest version (dev-master) of this package.

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

This package is auto-updated.

Last update: 2024-04-22 01:33:42 UTC


README

Provides a set of utils for html caching including placeholder view helper calls interception and restore.

Installation

composer require oprokidnev/cacheable-rendering:^2.0

Initialize module 'Oprokidnev\\CacheableRendering' in application.config.php and run you application once. Then you will get a new file in config/autoload named cacheable-rendering.config.local.php. This is the config with storage settings. By default I choose use filesystem cache adapter, but you can change this settings with the compatible with \Zend\Cache\StorageFactory::factory method array.

View helpers

CachedCallback

This is the most typical example when you should use callback cache.

<?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

The same as standart partial helper, instead of $key parameter in invocation.

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

CachedCapture

Encapsulates expensive output action inside while code block. Good for the cases, when cachedCallback closure will require big amount of scope variables.

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

Interacting with cache storages