PHP Classes

PHP Cache Manager Wrapper: Wrap over cache classes to optimize cache requests

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 67%Total: 170 All time: 8,842 This week: 78Up
Version License PHP version Categories
cache-manager-wrap 1.0.3MIT/X Consortium ...7.0Cache, Performance and optimization, P..., P...
Description 

Author

This package can wrap over cache classes to optimize cache requests.

It can take some parameters about queries of data, to a REST API or a database search for instance, and determines if the results in the cache are already available or need to be retrieved.

It also accesses the cache if the list of fields to be retrieved is a subset of values already stored in a cache entry.

The package can take query parameters for a context name, the type of entity being queried, a specific list of entity IDs, name of the entity fields to be returned, and the name of the id field.

The package calls PSR-6 compliant cache classes to access the cached data.

Innovation Award
PHP Programming Innovation award nominee
October 2016
Number 7


Prize: PhpStorm IDE 1 year individual subscription
Requests sent to APIs or database accesses may take a significant amount of time to process depending on the complexity of the requests. However, some API requests or database accesses may be actually retrieving the same data.

This class provides a solution to automatically cache the responses of APIs or the results of the datatase queries implemented in PHP by looking at API request parameters and checking if they were stored in previously cached responses.

The package uses PSR-6 compliant cache classes, so it can be used with any cache storage container that implements the PSR-6 interfaces.

Manuel Lemos
Picture of Sergii Pryz
  Performance   Level  
Name: Sergii Pryz <contact>
Classes: 10 packages by
Country: Ukraine Ukraine
Age: 40
All time rank: 133921 in Ukraine Ukraine
Week rank: 215 Up3 in Ukraine Ukraine Up
Innovation award
Innovation award
Nominee: 4x

Documentation

CacheManager

PHP 7 ready Latest Stable Version License SensioLabsInsight

Master

Build Status StyleCI Coverage Status

Dev

Build Status StyleCI Coverage Status

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

CacheManager does not implement any default cache library neither own one. Instead CacheManager asks objects to implement PSR-6. Having that with Symfony DI and PSR-6 Adapters makes possible to use any cache library without limitation.

Requirements

  • PHP 7.0.x

Installation

  1. Update to your `composer.json`
{
    "require": {
        "picamator/cachemanager": "~1.0"
    }
}

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

Specification

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

  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

  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

  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

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

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

Sample 2

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

Sample 3

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

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 compatible adapter
  2. Instantiate dependencies for CacheManager Facades using DI
  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

MemcachedManager is an example to use CacheManager with Memcached.

Other cache libraries

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

  1. Implement PSR-6 interface `Psr\Cache\CacheItemPoolInterface` over your library
  2. or choose one from existing adapters 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.

<?php
declare(strict_types = 1);

use \Picamator\CacheManager\Operation\Save;
use \Picamator\CacheManager\Operation\Search;
use \Picamator\CacheManager\Operation\Delete;

use \Picamator\CacheManager\ObjectManager;
use \Picamator\CacheManager\Cache\CacheItemFactory;
use \Picamator\CacheManager\Data\SearchResultFactory;

use \Picamator\CacheManager\Cache\KeyGenerator;
use \Picamator\CacheManager\CacheManager;
use \Picamator\CacheManager\CacheManagerSubject;

use \Picamator\CacheManager\Data\SearchCriteriaBuilder;

/ 
 * 1. Create dependencies objects
 */
// Use your implementation or existing adapters to fit PSR-6
/ @var \Psr\Cache\CacheItemPoolInterface $cacheItemPoolMock */
$cacheItemPoolMock = new CacheItemPoolMock();

// Use your implementation for extending CacheManagerSubject functionality
/ @var \Picamator\CacheManager\Spi\ObserverInterface $afterSearchMock */
$afterSearchMock = new AfterSearchMock();

// Object builder & factories
$objectManager          = new ObjectManager();
$cacheItemFactory       = new CacheItemFactory($objectManager);
$searchResultFactory    = new SearchResultFactory($objectManager);

// Building keys for saving data to cache
$cacheKeyGenerator      = new KeyGenerator();

// In real live please use Proxies or Lazy Loading
$operationSave          = new Save($cacheKeyGenerator, $cacheItemPoolMock, $cacheItemFactory);
$operationSearch        = new Search($cacheKeyGenerator, $cacheItemPoolMock, $searchResultFactory);
$operationDelete        = new Delete($cacheKeyGenerator, $cacheItemPoolMock);

/
 * 2. Instantiate cache manager 
 */
// Instantiate main cache manager object
$cacheManager           = new CacheManager($operationSave, $operationSearch, $operationDelete);

// Wrap Cache managed as Observer, it's possible to omit wrapper if application does not need such kind extensibility
$cacheManagerSubject    = new CacheManagerSubject($cacheManager);

// Attach observer to execute after search
$cacheManagerSubject->attach('afterSearch', $afterSearchMock);

/
 * 3. Provide search 
 */
// Prepare criteria
$searchCriteriaBuilder  = new SearchCriteriaBuilder($objectManager);
$searchCriteria = $searchCriteriaBuilder
                    ->setContextName('cloud')
                    ->setEntityName('customer')
                    ->setIdList([1, 2, 3])
                    ->setFieldList(['id', 'name'])
                    ->setIdName('id')
                    ->build();

$searchResult = $cacheManagerSubject->search($searchCriteria);

/
 * 4. Handle search result 
 */
// result api details
$searchResult->count();         // number of returned data from cache e.g. 2
$searchResult->getData();       // array of cache items
$searchResult->getMissedData(); // array of missed in cache id's e.g. [1]
$searchResult->hasData();       // boolean to show does something fit $searchCriteria in cache

API&SPI

API

API includes:

SPI

SPI includes:

  • interfaces inside Spi directory
  • events: `beforeSave`, `afterSave`, `beforeSearch`, `afterSearch`, `beforeDelete`, `afterDelete`

Documentation

Developing

To configure developing environment please:

  1. Follow install and run Docker container
  2. Run inside project root in the Docker container `composer install`

IDE style configuration

Please make sure that your IDE has imported .editorconfig. For more information please visit http://editorconfig.org/.

Backward compatibility

Please follow steps bellow to keep Backward compatibility:

  • keep stable API & SPI SHOULD
  • keep stable constructor injection signature
  • keep stable throwing Exceptions type

Backward compatibility validation

To check backward compatibility please run integration tests in MemcachedManager.

Contribution

If you find this project worth to use please add a star. Follow changes to see all activities. And if you see room for improvement, proposals please feel free to create an issue or send pull request. Here is a great guide to start contributing.

Please note that this project is released with a Contributor Code of Conduct. By participating in this project and its community you agree to abide by those terms.

License

CacheManager is licensed under the MIT License. Please see the LICENSE file for details.


  Files folder image Files (59)  
File Role Description
Files folder imagedev (3 directories)
Files folder imagedoc (1 directory)
Files folder imagesrc (3 files, 6 directories)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file .styleci.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Doc. Documentation
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE.txt Doc. Documentation
Accessible without login Plain text file README.md Doc. Example

  Files folder image Files (59)  /  dev  
File Role Description
Files folder imagedocker (1 file, 1 directory)
Files folder imagephpdoc (3 files)
Files folder imagetests (1 directory)

  Files folder image Files (59)  /  dev  /  docker  
File Role Description
Files folder imageapp (1 file)
  Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (59)  /  dev  /  docker  /  app  
File Role Description
  Accessible without login Plain text file Dockerfile Data Auxiliary data

  Files folder image Files (59)  /  dev  /  phpdoc  
File Role Description
  Accessible without login Plain text file phpdoc.sh Data Auxiliary data
  Accessible without login Plain text file phpdoc.xml Data Auxiliary data
  Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (59)  /  dev  /  tests  
File Role Description
Files folder imageunit (1 file, 1 directory)

  Files folder image Files (59)  /  dev  /  tests  /  unit  
File Role Description
Files folder imagesrc (4 files, 2 directories)
  Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data

  Files folder image Files (59)  /  dev  /  tests  /  unit  /  src  
File Role Description
Files folder imageCache (2 files)
Files folder imageOperation (3 files)
  Plain text file BaseTest.php Class Class source
  Plain text file CacheManagerSubjectTest.php Class Class source
  Plain text file CacheManagerTest.php Class Class source
  Plain text file ObjectManagerTest.php Class Class source

  Files folder image Files (59)  /  dev  /  tests  /  unit  /  src  /  Cache  
File Role Description
  Plain text file CacheItemFactoryTest.php Class Class source
  Plain text file KeyGeneratorTest.php Class Class source

  Files folder image Files (59)  /  dev  /  tests  /  unit  /  src  /  Operation  
File Role Description
  Plain text file DeleteTest.php Class Class source
  Plain text file SaveTest.php Class Class source
  Plain text file SearchTest.php Class Class source

  Files folder image Files (59)  /  doc  
File Role Description
Files folder imageuml (3 files)

  Files folder image Files (59)  /  doc  /  uml  
File Role Description
  Accessible without login Image file class.diagram.png Data Auxiliary data
  Accessible without login Plain text file README.md Doc. Documentation
  Accessible without login Image file use-case.diagram.png Data Auxiliary data

  Files folder image Files (59)  /  src  
File Role Description
Files folder imageApi (2 files, 3 directories)
Files folder imageCache (2 files)
Files folder imageData (6 files)
Files folder imageException (4 files)
Files folder imageOperation (3 files)
Files folder imageSpi (2 files, 1 directory)
  Plain text file CacheManager.php Class Class source
  Plain text file CacheManagerSubject.php Class Class source
  Plain text file ObjectManager.php Class Class source

  Files folder image Files (59)  /  src  /  Api  
File Role Description
Files folder imageCache (2 files)
Files folder imageData (4 files)
Files folder imageOperation (3 files)
  Plain text file CacheManagerInterface.php Class Class source
  Plain text file ObjectManagerInterface.php Class Class source

  Files folder image Files (59)  /  src  /  Api  /  Cache  
File Role Description
  Plain text file CacheItemFactoryInterface.php Class Class source
  Plain text file KeyGeneratorInterface.php Class Class source

  Files folder image Files (59)  /  src  /  Api  /  Data  
File Role Description
  Plain text file SearchCriteriaBuilderInterface.php Class Class source
  Plain text file SearchCriteriaInterface.php Class Class source
  Plain text file SearchResultFactoryInterface.php Class Class source
  Plain text file SearchResultInterface.php Class Class source

  Files folder image Files (59)  /  src  /  Api  /  Operation  
File Role Description
  Plain text file DeleteInterface.php Class Class source
  Plain text file SaveInterface.php Class Class source
  Plain text file SearchInterface.php Class Class source

  Files folder image Files (59)  /  src  /  Cache  
File Role Description
  Plain text file CacheItemFactory.php Class Class source
  Plain text file KeyGenerator.php Class Class source

  Files folder image Files (59)  /  src  /  Data  
File Role Description
  Plain text file Event.php Class Class source
  Plain text file EventBuilder.php Class Class source
  Plain text file SearchCriteria.php Class Class source
  Plain text file SearchCriteriaBuilder.php Class Class source
  Plain text file SearchResult.php Class Class source
  Plain text file SearchResultFactory.php Class Class source

  Files folder image Files (59)  /  src  /  Exception  
File Role Description
  Plain text file ExceptionInterface.php Class Class source
  Plain text file InvalidArgumentException.php Class Class source
  Plain text file InvalidCacheKeyException.php Class Class source
  Plain text file RuntimeException.php Class Class source

  Files folder image Files (59)  /  src  /  Operation  
File Role Description
  Plain text file Delete.php Class Class source
  Plain text file Save.php Class Class source
  Plain text file Search.php Class Class source

  Files folder image Files (59)  /  src  /  Spi  
File Role Description
Files folder imageData (2 files)
  Plain text file ObserverInterface.php Class Class source
  Plain text file SubjectInterface.php Class Class source

  Files folder image Files (59)  /  src  /  Spi  /  Data  
File Role Description
  Plain text file EventBuilderInterface.php Class Class source
  Plain text file EventInterface.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:170
This week:0
All time:8,842
This week:78Up
 User Ratings  
 
 All time
Utility:95%StarStarStarStarStar
Consistency:100%StarStarStarStarStarStar
Documentation:95%StarStarStarStarStar
Examples:-
Tests:-
Videos:-
Overall:67%StarStarStarStar
Rank:470