PHPackages                             bbc/ipr-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. bbc/ipr-cache

ActiveLibrary

bbc/ipr-cache
=============

A simple cache wrapper, making use of Doctrine Cache that implements fuzzy and stale-while-revalidate caching.

v1.1.0(9y ago)33.2k1MITPHPPHP &gt;=5.5

Since May 20Pushed 9y ago53 watchersCompare

[ Source](https://github.com/bbc/ipr-php-cache)[ Packagist](https://packagist.org/packages/bbc/ipr-cache)[ RSS](/packages/bbc-ipr-cache/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (3)Versions (4)Used By (1)

BBC\\iPlayerRadio\\Cache
========================

[](#bbciplayerradiocache)

A simple cache wrapper around Doctrine\\Cache that allows us to do standard, fuzzy and stale-while-revalidate caching.

[![Build Status](https://camo.githubusercontent.com/7ac395351966da4ffd707923970ee8b0601a0f9d179edb841cb841887970c46e/68747470733a2f2f7472617669732d63692e6f72672f6262632f6970722d7068702d63616368652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/bbc/ipr-php-cache)[![Latest Stable Version](https://camo.githubusercontent.com/5cf9e403e0930747acb23dad596cbd2d32b0191278268e8a759b9b4edd111f4d/68747470733a2f2f706f7365722e707567782e6f72672f6262632f6970722d63616368652f762f737461626c652e737667)](https://packagist.org/packages/bbc/ipr-cache)[![Total Downloads](https://camo.githubusercontent.com/1253d3f2fb1849c3875d9922bd3d2367edd1d24e6556d7b58214d8f52d8a9217/68747470733a2f2f706f7365722e707567782e6f72672f6262632f6970722d63616368652f646f776e6c6f6164732e737667)](https://packagist.org/packages/bbc/ipr-cache)[![License](https://camo.githubusercontent.com/e547bf0c76db498796c42e3030849cd2e7bc63ce0f74070ecb2851d02950c3f4/68747470733a2f2f706f7365722e707567782e6f72672f6262632f6970722d63616368652f6c6963656e73652e737667)](https://packagist.org/packages/bbc/ipr-cache)

- [Requirements](#requirements)
- [Usage](#usage)
    - [Getting Started](#getting-started)
    - [Reading Items](#reading-items)
    - [Fuzzy Caching](#fuzzy-caching)
    - [Pure Caching](#pure-caching)
    - [Stale While Revalidate Caching](#stale-while-revalidate-caching)
    - [Cache Prefixing](#cache-prefixing)
- [Interfaces](#interfaces)
- [Mocking the Cache](#mocking-the-cache)

Requirements
------------

[](#requirements)

- PHP &gt;= 5.5
- A cache backend that Doctrine/Cache understands

Usage
-----

[](#usage)

### Getting Started

[](#getting-started)

Install via Composer:

```
$ composer require bbc/ipr-cache
```

Now you need to construct an instance of `BBC\iPlayerRadio\Cache`, with an instance of `Doctrine\Common\Cache\Cache`passed to it. Here's a simple example:

```
$cacheAdapter = new Doctrine\Common\Cache\ArrayCache();
$cache = new BBC\iPlayerRadio\Cache\Cache($cacheAdapter);
```

The `$cacheAdapter` is the thing that actually does the reading and writing to the cache, our library just wraps it. Therefore we accept any of the `Doctrine\Common\Cache\*` classes, so if you're using Redis, or Memcached, or even plain old filesystem caches, this library will work with it. (More specifically, any class implementing the `Doctrine\Common\Cache\Cache` interface is accepted).

### Reading Items

[](#reading-items)

This library handles reading cache items slightly differently to what you might be used to. The "traditional" way, as used by Doctrine, works something like this:

```
if (($item = $cache->fetch($cacheKey) === false) {
    $data = '{somekey: somevalue}';
    $cache->save($cacheKey, $data, $lifetime);
}
```

This works great for a simple use-case, however if you want to be able to do things like fuzzing and stale-while-revalidate cleanly, this API style gets clunky quickly.

Instead, this library uses a repository pattern for reading and writing items into cache. This allows us to encapsulate the logic of the different cache modes more cleanly, even if it does look a bit strange at first!

Here's how we read an item from the cache:

```
