PHPackages                             picamator/cachemanager - 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. picamator/cachemanager

AbandonedArchivedLibrary[Caching](/categories/caching)

picamator/cachemanager
======================

Wrapper over 3-rd party cache libraries to optimise saving API/'s search result

1.0.5(9y ago)314MITPHPPHP &gt;=7.0

Since Oct 7Pushed 9y ago2 watchersCompare

[ Source](https://github.com/picamator/CacheManager)[ Packagist](https://packagist.org/packages/picamator/cachemanager)[ RSS](/packages/picamator-cachemanager/feed)WikiDiscussions master Synced 2mo ago

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

CacheManager
============

[](#cachemanager)

[![PHP 7 ready](https://camo.githubusercontent.com/3a9e14d04b929ec197b11784e39abead9b2cf92a743caaae3b6f5a1d75361119/687474703a2f2f7068703772656164792e74696d6573706c696e7465722e63682f706963616d61746f722f43616368654d616e616765722f62616467652e737667)](https://travis-ci.org/picamator/CacheManager)[![Latest Stable Version](https://camo.githubusercontent.com/37a62c12e40ccc91b1619d85e5a875f643bc218a21a181993a9a51a9ecc770a5/68747470733a2f2f706f7365722e707567782e6f72672f706963616d61746f722f63616368656d616e616765722f762f737461626c652e737667)](https://packagist.org/packages/picamator/cachemanager)[![License](https://camo.githubusercontent.com/611bc7f585caf38bb3ec3fd2ab76be489891091e881bb13a531bf73dc3ca9617/68747470733a2f2f706f7365722e707567782e6f72672f706963616d61746f722f63616368656d616e616765722f6c6963656e73652e737667)](https://packagist.org/packages/picamator/cachemanager)[![SensioLabsInsight](https://camo.githubusercontent.com/33264c0d7e58a1d7e1513bfa1a9c9dab94862553cb06330c553928c03275f4a8/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f38623533333633372d333932642d346265382d383230342d3737666632326634363063612f6d696e692e706e67)](https://insight.sensiolabs.com/projects/8b533637-392d-4be8-8204-77ff22f460ca)

Master
------

[](#master)

[![Build Status](https://camo.githubusercontent.com/c3c8887db9715e57882e97bcd357954e4f59889b3c7e26f17732637d60b79b71/68747470733a2f2f7472617669732d63692e6f72672f706963616d61746f722f43616368654d616e616765722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/picamator/CacheManager)[![StyleCI](https://camo.githubusercontent.com/714f0b699dde122af5078a82a3bd59698eb1524aeaf6733a678bc0525db8c69b/68747470733a2f2f7374796c6563692e696f2f7265706f732f36383138393430392f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/68189409)[![Coverage Status](https://camo.githubusercontent.com/fb16234a9e1e6ec46b7db86d08e33d9c87b115a8ca40ccce17da952dbcbf595f/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f706963616d61746f722f43616368654d616e616765722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/picamator/CacheManager?branch=master)

Dev
---

[](#dev)

[![Build Status](https://camo.githubusercontent.com/7833a1daa09a9dded35518491c47f5eff98f14369994527e17e8ee79950a51a5/68747470733a2f2f7472617669732d63692e6f72672f706963616d61746f722f43616368654d616e616765722e7376673f6272616e63683d646576)](https://travis-ci.org/picamator/CacheManager)[![StyleCI](https://camo.githubusercontent.com/31d4c7a8c45d85a331e4db398d9d7f0af3f86910817cfc7aab7058d394e762bb/68747470733a2f2f7374796c6563692e696f2f7265706f732f36383138393430392f736869656c643f6272616e63683d646576)](https://styleci.io/repos/68189409)[![Coverage Status](https://camo.githubusercontent.com/dfb866be7095b40d0e877665668804802f380a68fc0f07a4132840ba22e53219/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f706963616d61746f722f43616368654d616e616765722f62616467652e7376673f6272616e63683d646576)](https://coveralls.io/github/picamator/CacheManager?branch=dev)

CacheManager is an application providing wrapper over 3-rd party cache libraries optimizing for saving RESTful API's or SQL search results.

The general approach to save search response is based on building cache key as a hash of search query. But that is not working well for two slightly different queries. Moreover it's failed to combine data from cache and server. CacheManager solves those problems for special cases like searching entities by it's ids.

Cache libraries
---------------

[](#cache-libraries)

CacheManager does not implement any default cache library neither own one. Instead CacheManager asks objects to implement [PSR-6](http://www.php-fig.org/psr/psr-6/). Having that with [Symfony DI](https://github.com/symfony/dependency-injection) and [PSR-6 Adapters](https://github.com/php-cache?utf8=%E2%9C%93&query=adapter) makes possible to use any cache library without limitation.

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

[](#requirements)

- PHP 7.0.x

Installation
------------

[](#installation)

1. Update to your `composer.json`

```
{
    "require": {
        "picamator/cachemanager": "~1.0"
    }
}
```

2. Run `composer install --no-dev`

Specification
-------------

[](#specification)

### RESTful API

[](#restful-api)

Assume application works with RESTful API, where:

- `customer` - entity name
- `query` - parameter to save search criteria
- `IN` - function similar to MySQL IN
- `fields` - parameter with comma separated entity's fields

Each of the samples below shows pair of API requests.

#### Sample 1

[](#sample-1)

1. `GET: customer\?query="id IN(1,2,3)&fields='name,address'"`
2. `GET: customer\?query="id IN(1,2)&fields='name'"`

The second request SHOULD use cache because it has information about `customer` with id 1 and 2.

#### Sample 2

[](#sample-2)

1. `GET: customer\?query="id IN(1,2,3)&fields='name'"`
2. `GET: customer\?query="id IN(1,2)&fields='name,address'"`

The second request SHOULD NOT use cache because it asks more information about `customer` that was saved in the cache. Therefore after server data was obtained it SHOULD be saved to cache overriding the previously saved data.

#### Sample 3

[](#sample-3)

1. `GET: customer\?query="id IN(1,2,3)&fields='name,address'"`
2. `GET: customer\?query="id IN(3,4)&fields='name'"`

The second query SHOULD use cache for `customer` with id 3 and application SHOULD ask server only information about id 4.

### SQL

[](#sql)

Let's application use MySQL with:

- `customer` - table
- `id`, `name`, and `address` - columns in `customer` table

Each of the samples below shows pair of SQL queries. SQL samples SHOULD behavior similar to corresponding RESTful API samples.

#### Sample 1

[](#sample-1-1)

1. `SELECT name, address FROM customer WHERE id IN(1,2,3)`
2. `SELECT name FROM customer WHERE id IN(1,2)`

#### Sample 2

[](#sample-2-1)

1. `SELECT name FROM customer WHERE id IN(1,2,3)`
2. `SELECT name, address FROM customer WHERE id IN(1,2)`

#### Sample 3

[](#sample-3-1)

1. `SELECT name, address FROM customer WHERE id IN(1,2,3)`
2. `SELECT name FROM customer WHERE id IN(3,4)`

Usage
-----

[](#usage)

CacheManager has two different Facade classes:

1. `CacheManager` - provides base operation over cache
2. `CacheManagerSubject` - makes extending `CacheManager` with `events`

It's up to application to use what kind of extensibility is needed. If it's not need to use `events` then `CacheManager` Facade is a best choose.

Here is a steps to use CacheManager:

1. Choose your cache library with [PSR-6](http://www.php-fig.org/psr/psr-6/) compatible adapter
2. Instantiate dependencies for CacheManager Facades using [DI](https://en.wikipedia.org/wiki/Dependency_injection)
3. Create `CacheManager` or `CacheManagerSubject`
4. Prepare `SearchCriteriaBuilder`
5. Apply operation on CacheManager Facades
6. Handle result

Generally the steps 1-3 executes only once during application bootstrap but 4-6 are executed several times as needed.

### Memcached

[](#memcached)

[MemcachedManager](https://github.com/picamator/MemcachedManager) is an example to use CacheManager with [Memcached](https://memcached.org/).

### Other cache libraries

[](#other-cache-libraries)

To use CacheManager with arbitrary Cache library it's need:

1. Implement [PSR-6](http://www.php-fig.org/psr/psr-6/) interface `Psr\Cache\CacheItemPoolInterface` over your library
2. or choose one from existing adapters [php-cache](https://github.com/php-cache)
3. Choose between `CacheManager` and `CacheManagerSubject`

There is illustrative code bellow shows how to make cache search with `CacheManagerSubject`. Please use DI library to build dependencies in real application.

```
