PHPackages                             nickjbedford/block-cache - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. nickjbedford/block-cache

ActiveLibrary[Caching](/categories/caching)

nickjbedford/block-cache
========================

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

1.3.1(5d ago)2190MITPHPPHP ^8.4CI passing

Since Nov 29Pushed 5d ago1 watchersCompare

[ Source](https://github.com/nickjbedford/block-cacher)[ Packagist](https://packagist.org/packages/nickjbedford/block-cache)[ Docs](https://github.com/nickjbedford/block-cacher)[ RSS](/packages/nickjbedford-block-cache/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (2)Versions (29)Used By (0)

BlockCacher
===========

[](#blockcacher)

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

[](#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 &amp; Retrieving Values

[](#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 &amp; Data

[](#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 &amp; Data

[](#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 &amp; Data

[](#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

[](#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'];
    ?>
