PHPackages                             nilportugues/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. [Database &amp; ORM](/categories/database)
4. /
5. nilportugues/cache

ActiveLibrary[Database &amp; ORM](/categories/database)

nilportugues/cache
==================

Cache layer for PHP applications using the on Chain of responsability pattern

1.2.0(10y ago)7119[1 issues](https://github.com/nilportugues/cache/issues)MITPHPPHP &gt;=5.4

Since Apr 9Pushed 10y agoCompare

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

READMEChangelog (7)Dependencies (5)Versions (8)Used By (0)

Cache layer
===========

[](#cache-layer)

[![Build Status](https://camo.githubusercontent.com/3f467a2d0a5afe6f9b4d29e79108bbadefa3fc311b854cb3616b0ab7ba063117/68747470733a2f2f7472617669732d63692e6f72672f6e696c706f727475677565732f7068702d63616368652e737667)](https://travis-ci.org/nilportugues/php-cache) [![Coverage Status](https://camo.githubusercontent.com/fba278efecbc79c1248b654def0ea32e8bcd4ac538e40ad6e5aa5cf6ca5d13ab/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6e696c706f727475677565732f63616368652f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/r/nilportugues/cache?branch=master) [![Scrutinizer Code Quality](https://camo.githubusercontent.com/68fe42fd3a42b41e793b018e25f49fcdf7a93a7f2ecd53c5717ead82c4f93ae3/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6e696c706f727475677565732f63616368652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/nilportugues/cache/?branch=master) [![SensioLabsInsight](https://camo.githubusercontent.com/5848d0c13251a2a133dd45d204d9a7f4378e9a15be957f9bc4dfdcd08086ea18/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f39313863666136642d393937642d346534312d626463622d6430313937303438353037342f6d696e692e706e67)](https://insight.sensiolabs.com/projects/918cfa6d-997d-4e41-bdcb-d01970485074) [![Latest Stable Version](https://camo.githubusercontent.com/2c38eb6bb1807f663f5c9f8390c339ee2d2b017900a7dfc0a3cb2ea25c66b7aa/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f63616368652f762f737461626c65)](https://packagist.org/packages/nilportugues/cache) [![Total Downloads](https://camo.githubusercontent.com/9d70e5640f2dcfff50a3197622754960a504c5ec476c192c6adc6d63e7fd4543/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f63616368652f646f776e6c6f616473)](https://packagist.org/packages/nilportugues/cache) [![License](https://camo.githubusercontent.com/279ba3e33f59ab08e21a7598d5ca556eb7ed5cbd615d627380ee94afb184c7d5/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f63616368652f6c6963656e7365)](https://packagist.org/packages/nilportugues/cache)[![Donate](https://camo.githubusercontent.com/7b6de155df30b37b25eb5fec52f9213680c3dbf067dfb7d7e2850ac4096c7d05/68747470733a2f2f7777772e70617970616c6f626a656374732e636f6d2f656e5f55532f692f62746e2f62746e5f646f6e6174655f534d2e676966)](https://paypal.me/nilportugues)

Cache layer for PHP applications capable of being used standalone or with the Chain of Responsability pattern.

1. Installation
---------------

[](#1-installation)

The recommended way to install this package is through [Composer](http://getcomposer.org). Run the following command to install it:

```
php composer.phar require nilportugues/cache
```

---

2. Features
-----------

[](#2-features)

- One cache class, many adapters.
- All cache adapters can be used as standalone cache classes.
- Opt-in to be used as a chain of caches.
- Implementation normalizes all mechanisms and behaviours.
- Configuration files for all adapters in vanilla PHP and Symfony2 format provided in `config`and `migrations` directories.
- High quality, 100% tested code.

---

3. Drivers Available
--------------------

[](#3-drivers-available)

The package provides several implementations for a key-value cache.

The most sensitive choice is to make use of the Cache based adapters such as Redis and Memcached, which are the most performant and specialized.

Yet sometimes these are not available and other options should be considered, but we got you covered.

### Cache based:

[](#cache-based)

- **Memcached:** MemcachedAdapter *(php5-memcached)*
- **Redis:** RedisAdapter *(php5-redis)*, PredisAdapter *(Predis)*

### Full-text based:

[](#full-text-based)

- **SphinxQL:** SphinxAdapter
- **ElasticSearch:** ElasticSearchAdapter

### System based:

[](#system-based)

- **Memory:** InMemoryAdapter
- **FileSystem:** FileSystemAdapter

### Database based:

[](#database-based)

- **MySQL:** MySqlAdapter
- **PostgreSql:** PostgreSqlAdapter
- **Sqlite:** SqliteAdapter

---

4. Cache and CacheAdapter Interfaces
------------------------------------

[](#4-cache-and-cacheadapter-interfaces)

These are all the public methods available for the Cache and all of its available adapters:

- **get($key)**: Get a value identified by $key.
- **set($key, $value, $ttl = 0)**: Set a value identified by $key and with an optional $ttl.
- **defaultTtl($ttl)**: Allows to set a default ttl value if none is provided for set()
- **delete($key)**: Delete a value identified by $key.
- **isAvailable()**: Checks the availability of the cache service.
- **isHit()**: Check if value was found in the cache or not.
- **clear()**: Clears all expired values from cache.
- **drop()**: Clears all values from the cache.

### 4.1 - CacheAdapter

[](#41---cacheadapter)

Allows all of the public methods defined in Cache Interface.

Each CacheAdapter will have a custom constructor method due to its needs, but **last parameter is always a chainable value, being another CacheAdapter.**

---

5. Example
----------

[](#5-example)

```
The more cache levels the slower the cache system will be, so leverage the
cache to your needs.
Maybe you don't need a fallback mechanism at all! This is just an example.

```

**1st level cache**Redis (PredisAdapter) is our main cache, in a dedicated server.

**2nd level cache**Memcached (MemcachedAdapter) as fallback mechanism, available in the same machine as our PHP script.

**Application cache**InMemoryAdapter, used to avoid hiting the external caches on repeated operations and is shared by all cache layers. This comes enabled by default so you don't need to worry at all.

#### 5.1. Configuration

[](#51-configuration)

Using a Service Container, such as an array returning the services or a more popular solution such as Symfony's Service Container, build the caches.

For this example, we'll be building two caches, **user\_cache** and **image\_cache**. Both use Predis as first level cache and a fallback to Memcached if Predis cannot establish a connection during runtime.

```
