nickjbedford/block-cache 问题修复 & 功能扩展

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

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

nickjbedford/block-cache

Composer 安装命令:

composer require nickjbedford/block-cache

包简介

Provides code block level HTML, text and object caching to aggressively cache the generated elements of PHP web pages.

README 文档

README

BlockCacher provides an efficient file-based caching mechanism that can store serialised data, text and HTML content using simple store and get methods. HTML and data generation times can be reduced to fractions of a millisecond after caching, and this cacher is designed to be used wherever it can.

It provides straight-forward key-based storage and retrieval as well as start/end output buffer caching as well as lazy data generation.

Keys are directly used as filenames on disk, allowing key-based cache clearing using wildcards. The file system is now abstracted through the BlockCacher\IFileSystem interface so it can be replaced with other file-systems.

Usage

You must first instantiate an instance of BlockCacher with a cache storage directory. Only single-level cache directories are supported. Cache keys must not include sub-directories.

The first instance of BlockCacher will become the default and can then be retrieved through the global blockCacher() function, or BlockCacher::getDetault().

The cache directory specified will be created by default if it does not exist, although this can be prevented in the constructor parameters.

$cacher = BlockCacher\BlockCacher::createDefault(__DIR__ . '/cache');
$sameCacher = blockCacher();

BlockCacher is not a singleton but it does provide a default instance capability. This allows other cache directories to be used in parallel for more specific use cases.

$otherCacher = new BlockCacher\BlockCacher(__DIR__ . '/other-cache');

Storing & Retrieving Values

BlockCacher doesn't store expiry times and instead asks for the maximum age of the cache whenever cached data is to be retrieved. This removes the need to "package" values with expiry times when they are being stored on the disk.

Storing Text & Data

To store data in the cache, use the store() and storeText() methods.

$serialisable = [ ... ];
$cacher->store('dataKey', $serialisable);

$text = "This text is stored directly without serialisation.";
$cacher->storeText('textKey', $text);

Retrieve Text & Data

To retrieve data from the cache, use the get() and getText() methods. Pass in the maximum age of the cache in seconds to ensure that existing cache files that over the desired age are ignored. The default expiry lifetime is set to 86,400 seconds, or one day.

NOTE: Cache files are ignored but not deleted if they have expired.

$data = $cacher->get('dataKey', 3600);
// $data = [ ... ]

$text = $cacher->getText('textKey', 3600);
// $text = "This text is stored directly without serialisation."

Generating Blocks of HTML, Text & Data

BlockCacher includes helper methods for generating blocks of HTML content and serialisable data. These are start() + end() as well as generate() + html().

Generating HTML Blocks

The start() method determines if a block should be regenerated based on the cache status then starts a buffer if there is a cache miss. The end() method then closes the buffer, stores the contents and echoes it.

if ($cacher->start('cached-list.html'))
{
    $data = $this->getArticleData();
    $items = $data['items'];
    ?><div>
        <h1>
            <?php echo htmlentities($data['title']) ?>
        </h1>
        <ul>
        <?php foreach($items as $item) { ?>
            <li>
                <?php echo htmlentities($item['text']); ?>
            </li>
        <?php ?>
    </div><?php
}
$cacher->end();

The end() method must be called outside the if() statement. This will echo the content and clean up the buffer stack regardless of the status of the cache.

Lazily Generating Data

To generate cacheable data only if necessary, use the generate() method, which takes a closure that will generate the data to be cached if it does not already exist.

$data = $cacher->generate('cached-data.object', function() use($someVar)
{
    $data = // calculate data...
    
    printf("Cache does not exist, generating data...");
    return $data;
});

Lazily Generating Text (or HTML)

To generate cacheable text only if necessary, use the generateText() method, which takes a closure that will generate the text to be cached if it does not already exist.

$text = $cacher->generateText('cached-data.txt', function()
{    
    printf("Cache does not exist, generating data...");
    return 'Some text content...';
});

An Alternative For HTML

You can now use the new html() function and pass an output buffer generator function. This functions similarly to generate() except that the generator function should echo its content directly to the output buffer, not return it to the cacher method.

$html = $cacher->html('some-block.html', function()
{
    ?><p>
        This is a block of HTML that may take some time to generate.
    </p><?
});

The HTML content can then be echoed to the higher level output buffer.

Clearing Caches

Since cached data and text is stored in files under the cache directory, clearing caches uses glob file patterns. Use the clear() method and specify a filename or file pattern.

$cacher->clear('*.html'); // clear all HTML files
$cacher->clear('*.object'); // clear all .object files
$cacher->clear(); // clear ALL cache files

NOTE: Regular Cache Pruning

It can be prudent to prune old files regularly in large systems to ensure the clear() is fast when matching file patterns. With hundreds of thousands of cache files, glob() can take a long time to build its list of matching files.

clear() takes an optional $minimumAge parameter to specify the minimum age of files that can be cleared.

// clear all cache files older than 30 days
$minimumAge = 86400 * 30;
$cacher->clear('*', true, false, $minimumAge);

File System Interface

Version 0.4 introduces the new BlockCacher\IFileSystem interface. By default, the BlockCacher\NativeFileSystem implementation is used, which wraps the existing native IO functions that were used by BlockCacher.

To use a different file system, implement the IFileSystem interface then pass in an instance of your class to the BlockCacher constructor.

class CustomFileSystem implements BlockCacher\IFileSystem
{
    function pathExists(string $path) : bool { }
    function isFile(string $path) : bool { }
    function createDirectory(string $path) : bool { }
    function readFile(string $path) : ?string { }
    function writeFile(string $path,string $contents) : bool{ }
    function deleteFile(string $path) : bool { }
    function getModifiedTime(string $path) : int { }
    function searchFiles(string $globPattern) : array { }
}

$fileSystem = new CustomFileSystem();
$cacher = new \BlockCacher\BlockCacher('cache', '', true, $fileSystem);

nickjbedford/block-cache 适用场景与选型建议

nickjbedford/block-cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 190 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 11 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 nickjbedford/block-cache 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 190
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 21
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-11-29