[![Total Downloads](https://img.shields.io/packagist/dt/phpfastcache/phpfastcache.svg?maxAge=86400)](https://packagist.org/packages/phpfastcache/phpfastcache) [![Latest Stable Version](https://img.shields.io/packagist/v/phpfastcache/phpfastcache.svg?maxAge=86400)](https://packagist.org/packages/phpfastcache/phpfastcache) [![License](https://img.shields.io/packagist/l/phpfastcache/phpfastcache.svg?maxAge=86400)](https://packagist.org/packages/phpfastcache/phpfastcache) [![Coding Standards](https://img.shields.io/badge/CI-PSR6-orange.svg?maxAge=86400)](https://github.com/php-fig/cache) [![Coding Standards](https://img.shields.io/badge/CS-PSR16-orange.svg?maxAge=86400)](https://github.com/php-fig/simple-cache) [![Code Climate](https://codeclimate.com/github/PHPSocialNetwork/phpfastcache/badges/gpa.svg)](https://codeclimate.com/github/PHPSocialNetwork/phpfastcache) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/PHPSocialNetwork/phpfastcache/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/PHPSocialNetwork/phpfastcache/?branch=master) [![Build Status](https://travis-ci.org/PHPSocialNetwork/phpfastcache.svg?branch=master)](https://travis-ci.org/PHPSocialNetwork/phpfastcache) [![Semver compliant](https://img.shields.io/badge/Semver-2.0.0-yellow.svg?maxAge=86400)](https://semver.org/spec/v2.0.0.html) [![Patreon](https://img.shields.io/badge/Support%20us%20on-Patreon-f96854.svg?maxAge=86400)](https://www.patreon.com/geolim4) #### :warning: Please note that the V7 is a major (BC breaking) update of PhpFastCache ! > As the V7 is **absolutely** not compatible with previous versions, please read carefully the [migration guide](./docs/migration/MigratingFromV6ToV7.md) to ensure you the smoothest migration possible. One of the biggest change is the configuration system which is now an object that replace the primitive array that we used to implement back then. Also please note that the V7 requires at least PHP7 or higher to work properly. >:new: The V8 is now released, upgrade Phpfastcache by following our migration guide in `docs/migration` --------------------------- Simple Yet Powerful PHP Caching Class --------------------------- More information in [Wiki](https://github.com/PHPSocialNetwork/phpfastcache/wiki) The simplicity of abstraction: One class for many backend cache. You don't need to rewrite your code many times again. ### PHP7 Strict types enforced As of the V7 PhpFastCache enforces the php7 strict types to make sure that it's completely php7 compatible and is type aware. This ensure you that the library is completely reliable when it come to manipulate variable types. ### Supported drivers at this day * :bulb: Feel free to propose a driver by making a new **[Pull Request](https://github.com/PHPSocialNetwork/phpfastcache/compare)**, they are welcome ! | Regular drivers | High performances drivers | Development drivers | |---------------------------------|------------------------------------|-------------------------------| | `Apc(u)` | `Cassandra` | `Devnull` | | `Cookie` | `CouchBase` | `Devfalse` | | `Files` | `Couchdb` | `Devtrue` | | `Leveldb` :question: | `Mongodb` | `Memstatic` | | `Memcache(d)` | `Predis` | | | `Sqlite` | `Redis` | | | `Wincache` :question: | `Riak` | | | `Xcache` :question: | `Ssdb` | | | `Zend Disk Cache` | `Zend Memory Cache` | | \* Driver descriptions available in DOCS/DRIVERS.md :question: Please note that as of PHP7 some php extensions were not yet updated (and will may not), we keep the code here but there's no guarantee that they are still working especially due to the cores changes introduced by Opcache. ### Symfony/Drupal developers are not forgotten ! Starting with v5, phpFastCache comes with a [Symfony Bundle](https://github.com/PHPSocialNetwork/phpfastcache-bundle). It is now a mature project Flex -ready, so feel free to give at try and report any bug (if any). Also a [Drupal 8 Module](https://github.com/PHPSocialNetwork/phpfastcache-drupal) is currently in development, add it to your starred projects to get notified of the first public release. --------------------------- Not a "Traditional" Caching --------------------------- phpFastCache is not like the traditional caching methods which keep reading and writing to files, sqlite or keeping open massive amounts of connections to memcache, redis, mongodb...\ Also, when you use high performances drivers, your miss hits will be drastically reduced.\ Slightly different from the usual caching libraries you will find everywhere on the internet, the phpFastCache library reduces the I/O and CPU load as much as possible. ```php has('test-key')){ // Setter action $data = 'lorem ipsum'; $Psr16Adapter->set('test-key', 'lorem ipsum', 300);// 5 minutes }else{ // Getter action $data = $Psr16Adapter->get('test-key'); } /** * Do your stuff with $data */ ``` Internally, the Psr16 adapter calls the PhpFastCache Api via the cache manager. --------------------------- Introducing to events --------------------------- :mega: As of the V6, PhpFastCache provides an event mechanism. You can subscribe to an event by passing a Closure to an active event: ```php onCacheGetItem(function(ExtendedCacheItemPoolInterface $itemPool, ExtendedCacheItemInterface $item){ $item->set('[HACKED BY EVENT] ' . $item->get()); }); ``` An event callback can get unbind but you MUST provide a name to the callback previously: ```php onCacheGetItem(function(ExtendedCacheItemPoolInterface $itemPool, ExtendedCacheItemInterface $item){ $item->set('[HACKED BY EVENT] ' . $item->get()); }, 'myCallbackName'); /** * Unbind the event callback */ EventManager::getInstance()->unbindEventCallback('onCacheGetItem', 'myCallbackName'); ``` More information about the implementation and the events are available on the [Wiki](https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV6%5D-Introducing-to-events) --------------------------- Introducing new helpers --------------------------- :books: As of the V6, PhpFastCache provides some helpers to make your code easier. - The [ActOnAll Helper](https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV6%CB%96%5D-Act-on-all-instances) to help you to act on multiple instance at once. - The [CacheConditional Helper](https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV6%CB%96%5D-Cache-Conditional) to help you to make the basic conditional statement more easier. - The [Psr16 adapter](https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV6%CB%96%5D-Psr16-adapter) to keep it simple as explained above. May more will come in the future, feel free to contribute ! --------------------------- As Fast To Implement As Opening a Beer --------------------------- #### :thumbsup: Step 1: Include phpFastCache in your project with composer: ```bash composer require phpfastcache/phpfastcache ``` #### :construction: Step 2: Setup your website code to implement the phpFastCache calls (with Composer) ```php '/var/www/phpfastcache.com/dev/tmp', // or in windows "C:/tmp/" ])); // In your class, function, you can call the Cache $InstanceCache = CacheManager::getInstance('files'); /** * Try to get $products from Caching First * product_page is "identity keyword"; */ $key = "product_page"; $CachedString = $InstanceCache->getItem($key); $your_product_data = [ 'First product', 'Second product', 'Third product' /* ... */ ]; if (!$CachedString->isHit()) { $CachedString->set($your_product_data)->expiresAfter(5);//in seconds, also accepts Datetime $InstanceCache->save($CachedString); // Save the cache item just like you do with doctrine and entities echo 'FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // '; echo $CachedString->get(); } else { echo 'READ FROM CACHE // '; echo $CachedString->get()[0];// Will print 'First product' } /** * use your products here or return them; */ echo implode('
', $CachedString->get());// Will echo your product list ``` ##### :floppy_disk: Legacy support (Without Composer) * See the file examples/withoutComposer.php for more information. #### :zap: Step 3: Enjoy ! Your website is now faster than lightning ! For curious developers, there is a lot of other examples available [here](./docs/examples). #### :boom: phpFastCache support Found an issue or have an idea ? Come **[here](https://github.com/PHPSocialNetwork/phpfastcache/issues)** and let us know !