diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ec566fab..4fda31f9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,12 +1,20 @@ +## 9.0.1 +##### 14 november 2021 +- __Core__ + - Added `\Phpfastcache\Event\Event` class for centralizing event name with reusable constants. +- __Item__ + - `\Psr\Cache\CacheItemInterface::set` will also no longer accepts resource object anymore as method unique parameter +- __Misc__ + - Fixed typos in [README.md](./README.md) ## 9.0.0 -##### 1 october 2021 +##### 31 october 2021 - __Migration guide__ - Read the [migration guide](./docs/migration/MigratingFromV8ToV9.md) to upgrade from V8 to V9 - __PSR-6__ - Upgraded `psr/cache` dependency to `^2.0||^3.0` (for PHP-8 types) - `\Psr\Cache\CacheItemInterface::get()` slightly changed to fully comply with missing PSR-6 specification: If the cache item is **NOT** hit, this method will return `NULL`. - __PSR-16__ - - _To be written when the PSR-16 will be upgraded for PHP-8_ + - Upgraded `psr/simple-cache` dependency to `^2.0||^3.0` (for PHP-8 types) - __API__ - Upgraded Phpfastcache API `4.0.0` ([see changes](CHANGELOG_API.md)) - Renamed `Api::getPhpFastCacheVersion()` to `Api::getPhpfastcacheVersion()` diff --git a/README.md b/README.md index a8f4f1fa7..2a869b1f0 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ [![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) [![Cache Interface](https://img.shields.io/badge/CI-PSR6-orange.svg?maxAge=86400)](https://github.com/php-fig/cache) [![Extended Coding Style](https://img.shields.io/badge/ECS-PSR12-orange.svg?maxAge=86400)](https://www.php-fig.org/psr/psr-12/) [![Simple Cache](https://img.shields.io/badge/SC-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.com/PHPSocialNetwork/phpfastcache.svg?branch=master)](https://travis-ci.com/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) +[![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.com/PHPSocialNetwork/phpfastcache.svg?branch=master)](https://app.travis-ci.com/github/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 V9 is mostly a PHP 8 type aware update of Phpfastcache with some significant changes ! > As the V9 is **relatively** not compatible with previous versions, please read carefully the [migration guide](./docs/migration/MigratingFromV8ToV9.md) to ensure you the smoothest migration possible. @@ -19,19 +19,16 @@ The simplicity of abstraction: One class for many backend cache. You don't need |---------------------------------|---------------------------------------------------------------|-------------------------------|-----------------------------------| | `Apcu` *(APC support removed)* | `Arangodb` | `Devnull` | `FullReplicationCluster` | | `Dynamodb` (AWS) | `Cassandra` | `Devrandom` | `SemiReplicationCluster` | -| `Files` | `CouchBasev3`
_(`Couchbase` for SDK 2 support removed)_ | `Devrandom` | `SemiReplicationCluster` | -| `Firestore` (GCP) | `Couchdb` | `Devrandom` | `SemiReplicationCluster` | -| `Leveldb` | `Mongodb` | `Memstatic` | `MasterSlaveReplicationCluster` | -| `Memcache(d)` | `Predis` | | `RandomReplicationCluster` | +| `Files` | `CouchBasev3`
_(`Couchbase` for SDK 2 support removed)_ | `Memstatic` | `MasterSlaveReplicationCluster` | +| `Firestore` (GCP) | `Couchdb` | | `RandomReplicationCluster` | +| `Leveldb` | `Mongodb` | | | +| `Memcache(d)` | `Predis` | | | | `Sqlite` | `Redis` | | | | `Wincache` | `Ssdb` | | | | `Zend Disk Cache` | `Zend Memory Cache` | | | | | | | | -| | | | | -| | | | | -| | | | | -\* Driver descriptions available in DOCS/DRIVERS.md +\* Driver descriptions available in [DOCS/DRIVERS.md](./docs/DRIVERS.md) --------------------------- Because caching does not mean weaken your code @@ -41,6 +38,7 @@ Phpfastcache has been developed over the years with 3 main goals: - Performance: We optimized and still optimize the code to provide you the lightest library as possible - Security: Because caching strategies can sometimes comes with unwanted vulnerabilities, we do our best to provide you a sage & strong library as possible - Portability: No matter what operating system you're working on, we did our best to provide you the most cross-platform code as possible + --------------------------- Rich Development API --------------------------- diff --git a/docs/examples/README.md b/docs/examples/README.md new file mode 100644 index 000000000..eb18c7a2d --- /dev/null +++ b/docs/examples/README.md @@ -0,0 +1,6 @@ +### Important notice + +Example files were removed due to maintainability problem.\ +Instead you can take a look at the `/tests` directory which contains a lot of code example your can inspire yourself to. + +[Bring me to `/tests` directory](../../tests) diff --git a/docs/examples/SaveMultiple.php b/docs/examples/SaveMultiple.php deleted file mode 100644 index ba0d720c7..000000000 --- a/docs/examples/SaveMultiple.php +++ /dev/null @@ -1,57 +0,0 @@ - - * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors - */ -// Include composer autoloader -require __DIR__ . '/../../vendor/autoload.php'; -// OR require_once("../src/phpFastCache/phpFastCache.php"); - -use Phpfastcache\CacheManager; -use Phpfastcache\Config\Config; - -// Setup File Path on your config files -CacheManager::setDefaultConfig(new Config([ - "path" => sys_get_temp_dir() -])); - -// In your class, function, you can call the Cache -$InstanceCache = CacheManager::getInstance('files'); - -$key = "product_page"; -$key2 = "product_page2"; - -$cacheItem = $InstanceCache->getItem($key); -$cacheItem2 = $InstanceCache->getItem($key2); - -$cacheItem->set('test')->expiresAfter(300); -$cacheItem2->set('test')->expiresAfter(300); - -/** - * Old way, but still working, to persist multiple items - */ -$InstanceCache->save($cacheItem); -$InstanceCache->save($cacheItem2); - -/** - * New way to persist multiple items - * (Unlimited arguments) - */ -$InstanceCache->saveMultiple($cacheItem, $cacheItem2); - -/** - * New way to persist multiple items - * Alternative for automated mass persisting - */ -/** - * New way to persist multiple items - * (Only first argument will be interpreted) - */ -$InstanceCache->saveMultiple([$cacheItem, $cacheItem2]); diff --git a/docs/examples/apc.php b/docs/examples/apc.php deleted file mode 100644 index 5ff72bd22..000000000 --- a/docs/examples/apc.php +++ /dev/null @@ -1,41 +0,0 @@ - - * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors - */ -use Phpfastcache\CacheManager; - -// Include composer autoloader -require __DIR__ . '/../../vendor/autoload.php'; - -$InstanceCache = CacheManager::getInstance('apc'); - -/** - * Try to get $products from Caching First - * product_page is "identity keyword"; - */ -$key = "product_page"; -$CachedString = $InstanceCache->getItem($key); - -if (is_null($CachedString->get())) { - //$CachedString = "APC Cache --> Cache Enabled --> Well done !"; - // Write products to Cache in 10 minutes with same keyword - $CachedString->set("APC Cache --> Cache Enabled --> Well done !")->expiresAfter(5); - $InstanceCache->save($CachedString); - - echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // "; - echo $CachedString->get(); - -} else { - echo "READ FROM CACHE // "; - echo $CachedString->get(); -} - -echo '

Back to index -- Reload'; diff --git a/docs/examples/cassandra.php b/docs/examples/cassandra.php deleted file mode 100644 index 1de218841..000000000 --- a/docs/examples/cassandra.php +++ /dev/null @@ -1,52 +0,0 @@ - - * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors - */ - -use Phpfastcache\CacheManager; -use Phpfastcache\Drivers\Cassandra\Config; - -// Include composer autoloader -require __DIR__ . '/../../vendor/autoload.php'; - -$config = []; -$config['host'] = '127.0.0.1'; -$config['port'] = 9042; -$config['timeout'] = 2; -$config['username'] = ''; -$config['password'] = ''; -$config['sslEnabled'] = false; -$config['sslVerify'] = false; - -$InstanceCache = CacheManager::getInstance('cassandra', new Config($config)); - -/** - * Try to get $products from Caching First - * product_page is "identity keyword"; - */ -$key = "product_page"; -$CachedString = $InstanceCache->getItem($key); - -if (is_null($CachedString->get())) { - //$CachedString = "APC Cache --> Cache Enabled --> Well done !"; - // Write products to Cache in 10 minutes with same keyword - $CachedString->set("Redis Cache --> Cache Enabled --> Well done !")->expiresAfter(5); - $InstanceCache->save($CachedString); - - echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // "; - echo $CachedString->get(); - -} else { - echo "READ FROM CACHE // "; - echo $CachedString->get(); -} - -echo '

Back to index -- Reload'; diff --git a/docs/examples/cookie.php b/docs/examples/cookie.php deleted file mode 100644 index 94db51564..000000000 --- a/docs/examples/cookie.php +++ /dev/null @@ -1,41 +0,0 @@ - - * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors - */ -use Phpfastcache\CacheManager; - -// Include composer autoloader -require __DIR__ . '/../../vendor/autoload.php'; - -$InstanceCache = CacheManager::getInstance('cookie'); - -/** - * Try to get $products from Caching First - * product_page is "identity keyword"; - */ -$key = "product_page"; -$CachedString = $InstanceCache->getItem($key); - -if (is_null($CachedString->get())) { - //$CachedString = "Cookie Cache --> Cache Enabled --> Well done !"; - // Write products to Cache in 10 minutes with same keyword - $CachedString->set("Cookie Cache --> Cache Enabled --> Well done !")->expiresAfter(5); - $InstanceCache->save($CachedString); - - echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // "; - echo $CachedString->get(); - -} else { - echo "READ FROM CACHE // "; - echo $CachedString->get(); -} - -echo '

Back to index -- Reload'; diff --git a/docs/examples/couchbase.php b/docs/examples/couchbase.php deleted file mode 100644 index 4ce5a2bd4..000000000 --- a/docs/examples/couchbase.php +++ /dev/null @@ -1,49 +0,0 @@ - - * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors - */ - -use Phpfastcache\CacheManager; -use Phpfastcache\Drivers\Couchbase\Config; - -// Include composer autoloader -require __DIR__ . '/../../vendor/autoload.php'; - -$InstanceCache = CacheManager::getInstance('couchbase', new Config([ - 'host' => 'your-couchbase-host', - 'port' => '8091', - 'username' => 'your-couchbase-username', - 'password' => 'your-couchbase-password', - 'bucketName' => 'default' // The bucket name, generally "default" by default -])); - -/** - * Try to get $products from Caching First - * product_page is "identity keyword"; - */ -$key = "product_page"; -$CachedString = $InstanceCache->getItem($key); - -if (is_null($CachedString->get())) { - //$CachedString = "APC Cache --> Cache Enabled --> Well done !"; - // Write products to Cache in 10 minutes with same keyword - $CachedString->set("Couchbase Cache --> Cache Enabled --> Well done !")->expiresAfter(5); - $InstanceCache->save($CachedString); - - echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // "; - echo $CachedString->get(); - -} else { - echo "READ FROM CACHE // "; - echo $CachedString->get(); -} - -echo '

Back to index -- Reload'; diff --git a/docs/examples/couchdb.php b/docs/examples/couchdb.php deleted file mode 100644 index 3a0816fd8..000000000 --- a/docs/examples/couchdb.php +++ /dev/null @@ -1,51 +0,0 @@ - - * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors - */ - -use Phpfastcache\CacheManager; -use Phpfastcache\Drivers\Couchdb\Config; - -// Include composer autoloader -require __DIR__ . '/../../vendor/autoload.php'; - -$InstanceCache = CacheManager::getInstance('couchdb', new Config([ - 'host' => '127.0.0.1', - 'port' => 5984, - 'path' => '/', - 'username' => 'your-couchdb-username', - 'password' => 'your-couchdb-password', - 'ssl' => true, - 'timeout' => 10, -])); - -/** - * Try to get $products from Caching First - * product_page is "identity keyword"; - */ -$key = "product_page"; -$CachedString = $InstanceCache->getItem($key); - -if (is_null($CachedString->get())) { - //$CachedString = "APC Cache --> Cache Enabled --> Well done !"; - // Write products to Cache in 10 minutes with same keyword - $CachedString->set("Couchdb Cache --> Cache Enabled --> Well done !")->expiresAfter(5); - $InstanceCache->save($CachedString); - - echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // "; - echo $CachedString->get(); - -} else { - echo "READ FROM CACHE // "; - echo $CachedString->get(); -} - -echo '

Back to index -- Reload'; diff --git a/docs/examples/decrement.php b/docs/examples/decrement.php deleted file mode 100644 index 9a0c8f78a..000000000 --- a/docs/examples/decrement.php +++ /dev/null @@ -1,50 +0,0 @@ - - * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors - */ - -// Include composer autoloader -require __DIR__ . '/../../vendor/autoload.php'; -// OR require_once("../src/phpFastCache/phpFastCache.php"); -date_default_timezone_set("Europe/Paris"); - - -use Phpfastcache\CacheManager; -use Phpfastcache\Core\phpFastCache; - -// Setup File Path on your config files -CacheManager::setDefaultConfig([ - "path" => '/var/www/phpfastcache.dev.geolim4.com/geolim4/tmp', // or in windows "C:/tmp/" -]); - -// In your class, function, you can call the Cache -$InstanceCache = CacheManager::getInstance('redis'); - -/** - * Try to get $products from Caching First - * product_page is "identity keyword"; - */ -$key = "product_decrement"; -$CachedString = $InstanceCache->getItem($key); -if (is_null($CachedString->get())) { - $CachedString->set(1000)->expiresAfter(10); - - echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // DECREMENT // "; - echo $CachedString->decrement()->get(); - -} else { - echo "READ FROM CACHE // decrement // "; - echo $CachedString->decrement()->get(); -} - -$InstanceCache->save($CachedString); - -echo '

Back to index -- Reload'; diff --git a/docs/examples/extendedPhpFastCache.php b/docs/examples/extendedPhpFastCache.php deleted file mode 100644 index 777feac0c..000000000 --- a/docs/examples/extendedPhpFastCache.php +++ /dev/null @@ -1,53 +0,0 @@ - - * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors - */ - -namespace MyCustom\Project; - -use Phpfastcache\Drivers\Files\Driver as FilesDriver; -use Phpfastcache\Proxy\PhpfastcacheAbstractProxy; - -/** - * Specific driver-based example - * Class extendsPhpFastCache - * @package MyCustom\Project - */ -class extendedPhpFastCache extends FilesDriver -{ - public function __construct(array $config = []) - { - $config[ 'path' ] = 'your/custom/path/where/files/will/be/written'; - parent::__construct($config); - /** - * That's all !! Your cache class is ready to use - */ - } -} - - -/** - * Dynamic driver-based example - * Class myCustomCacheClass - * @package MyCustom\Project - */ -class myCustomCacheClass extends PhpfastcacheAbstractProxy -{ - public function __construct($driver = '', array $config = []) - { - $config[ 'path' ] = 'your/custom/path/where/files/will/be/written'; - $driver = 'files'; - parent::__construct($driver, $config); - /** - * That's all !! Your cache class is ready to use - */ - } -} diff --git a/docs/examples/fetchAllKeys.php b/docs/examples/fetchAllKeys.php deleted file mode 100644 index a1199f656..000000000 --- a/docs/examples/fetchAllKeys.php +++ /dev/null @@ -1,55 +0,0 @@ - - * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors - */ -use Phpfastcache\CacheManager; - -// Include composer autoloader -require __DIR__ . '/../../vendor/autoload.php'; - -$InstanceCache = CacheManager::getInstance('files'); -$InstanceCache->clear(); - -/** - * @var $keys \Phpfastcache\Core\Item\ExtendedCacheItemInterface[] - */ -$keyPrefix = "product_page_"; -$keys = []; - -for ($i=1;$i<=10;$i++) -{ - $keys[$keyPrefix . $i] = $InstanceCache->getItem($keyPrefix . $i); - if(!$keys[$keyPrefix . $i]->isHit()){ - $keys[$keyPrefix . $i] - ->set(uniqid('pfc', true)) - ->addTag('pfc'); - } -} - -$InstanceCache->saveMultiple($keys); - -/** - * Remove items references from memory - */ -unset($keys); -$InstanceCache->detachAllItems(); -gc_collect_cycles(); - - -/** - * Now get the items by a specific tag - */ -$keys = $InstanceCache->getItemsByTag('pfc'); -foreach ($keys as $key) { - echo "Key: {$key->getKey()} => {$key->get()}
"; -} - -echo '

Back to index -- Reload'; diff --git a/docs/examples/files.php b/docs/examples/files.php deleted file mode 100644 index 5c75a3b39..000000000 --- a/docs/examples/files.php +++ /dev/null @@ -1,55 +0,0 @@ - - * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors - */ -// Include composer autoloader -require __DIR__ . '/../../vendor/autoload.php'; -// OR require_once("../src/phpFastCache/phpFastCache.php"); -date_default_timezone_set("Europe/Paris"); - - -use Phpfastcache\CacheManager; -use Phpfastcache\Config\Config; -use Phpfastcache\Core\phpFastCache; - -// Setup File Path on your config files -CacheManager::setDefaultConfig(new Config([ - "path" => sys_get_temp_dir(), - "itemDetailedDate" => false -])); - -// In your class, function, you can call the Cache -$InstanceCache = CacheManager::getInstance('files'); -// OR $InstanceCache = CacheManager::getInstance() <-- open examples/global.setup.php to see more - -/** - * Try to get $products from Caching First - * product_page is "identity keyword"; - */ -$key = "product_page"; -$CachedString = $InstanceCache->getItem($key); - -if (is_null($CachedString->get())) { - //$CachedString = "Files Cache --> Cache Enabled --> Well done !"; - // Write products to Cache in 1 minutes with same keyword - $CachedString->set("Files Cache --> Cache Enabled --> Well done !")->expiresAfter(60); - $InstanceCache->save($CachedString); - - echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // "; - echo $CachedString->get(); - -} else { - echo "READ FROM CACHE // "; - echo "\n EXPIRATION DATE: " . $CachedString->getExpirationDate()->format(DateTime::W3C); - echo $CachedString->get(); -} - -echo '

Back to index -- Reload'; diff --git a/docs/examples/increment.php b/docs/examples/increment.php deleted file mode 100644 index 9f7030f22..000000000 --- a/docs/examples/increment.php +++ /dev/null @@ -1,49 +0,0 @@ - - * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors - */ -// Include composer autoloader -require __DIR__ . '/../../vendor/autoload.php'; -// OR require_once("../src/phpFastCache/phpFastCache.php"); -date_default_timezone_set("Europe/Paris"); - - -use Phpfastcache\CacheManager; -use Phpfastcache\Core\phpFastCache; - -// Setup File Path on your config files -CacheManager::setDefaultConfig([ - "path" => '/var/www/phpfastcache.dev.geolim4.com/geolim4/tmp', // or in windows "C:/tmp/" -]); - -// In your class, function, you can call the Cache -$InstanceCache = CacheManager::getInstance('redis'); - -/** - * Try to get $products from Caching First - * product_page is "identity keyword"; - */ -$key = "product_increment"; -$CachedString = $InstanceCache->getItem($key); -if (is_null($CachedString->get())) { - $CachedString->set(1000)->expiresAfter(10); - - echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // INCREMENT // "; - echo $CachedString->increment()->get(); - -} else { - echo "READ FROM CACHE // INCREMENT // "; - echo $CachedString->increment()->get(); -} - -$InstanceCache->save($CachedString); - -echo '

Back to index -- Reload'; diff --git a/docs/examples/index.php b/docs/examples/index.php deleted file mode 100644 index 74e020b7f..000000000 --- a/docs/examples/index.php +++ /dev/null @@ -1,43 +0,0 @@ - - * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors - */ - -echo '

Drivers available:

'; -echo ''; - -// Comment this "exit;" to display extensions -// informations about missing/misconfigured drivers -// exit; - -echo '

PHP extensions loaded:

'; -echo ''; - -echo '

PHP info:

'; - -phpinfo(); diff --git a/docs/examples/itemDetailedDate.php b/docs/examples/itemDetailedDate.php deleted file mode 100644 index 3408c54cb..000000000 --- a/docs/examples/itemDetailedDate.php +++ /dev/null @@ -1,58 +0,0 @@ - - * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors - */ -// Include composer autoloader -require __DIR__ . '/../../vendor/autoload.php'; -// OR require_once("../src/phpFastCache/phpFastCache.php"); -date_default_timezone_set("Europe/Paris"); - - -use Phpfastcache\CacheManager; - -// Setup File Path on your config files -CacheManager::setDefaultConfig([ - "path" => sys_get_temp_dir(), - "itemDetailedDate" => true -]); - -// In your class, function, you can call the Cache -$InstanceCache = CacheManager::getInstance('files'); -// OR $InstanceCache = CacheManager::getInstance() <-- open examples/global.setup.php to see more - -/** - * Try to get $products from Caching First - * product_page is "identity keyword"; - */ -$key = "product_page"; -$CachedString = $InstanceCache->getItem($key); - -if (is_null($CachedString->get())) { - //$CachedString = "Files Cache --> Cache Enabled --> Well done !"; - // Write products to Cache in 10 minutes with same keyword - $CachedString->set("Files Cache --> Cache Enabled --> Well done !")->expiresAfter(60); - $InstanceCache->save($CachedString); - - echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // "; - echo $CachedString->get(); - -} else { - $CachedString->set("Files Cache --> Cache Enabled --> Well done !"); - $InstanceCache->save($CachedString); - - echo "READ FROM CACHE // "; - echo "\n CREATION DATE: " . $CachedString->getCreationDate()->format(DateTime::W3C); - echo "\n MODIFICATION DATE: " . $CachedString->getModificationDate()->format(DateTime::W3C); - echo "\n EXPIRATION DATE: " . $CachedString->getExpirationDate()->format(DateTime::W3C); - echo $CachedString->get(); -} - -echo '

Back to index -- Reload'; diff --git a/docs/examples/leveldb.php b/docs/examples/leveldb.php deleted file mode 100644 index 58b194393..000000000 --- a/docs/examples/leveldb.php +++ /dev/null @@ -1,54 +0,0 @@ - - * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors - */ -// Include composer autoloader -require __DIR__ . '/../../vendor/autoload.php'; -// OR require_once("../src/phpFastCache/phpFastCache.php"); -date_default_timezone_set("Europe/Paris"); - - -use Phpfastcache\CacheManager; -use Phpfastcache\Config\Config; -use Phpfastcache\Core\phpFastCache; - -// Setup File Path on your config files -CacheManager::setDefaultConfig(new Config([ - "path" => sys_get_temp_dir(), -])); - -// In your class, function, you can call the Cache -$InstanceCache = CacheManager::getInstance('leveldb'); -// OR $InstanceCache = CacheManager::getInstance() <-- open examples/global.setup.php to see more - -/** - * Try to get $products from Caching First - * product_page is "identity keyword"; - */ -$key = "product_page"; -$CachedString = $InstanceCache->getItem($key); - -if (is_null($CachedString->get())) { - //$CachedString = "Files Cache --> Cache Enabled --> Well done !"; - // Write products to Cache in 10 minutes with same keyword - $CachedString->set("Files Cache --> Cache Enabled --> Well done !"); - $InstanceCache->save($CachedString); - - echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // "; - echo $CachedString->get(); - -} else { - echo "READ FROM CACHE // "; - echo $CachedString->getExpirationDate()->format(Datetime::W3C); - echo $CachedString->get(); -} - -echo '

Back to index -- Reload'; diff --git a/docs/examples/memcache.php b/docs/examples/memcache.php deleted file mode 100644 index 284be03bc..000000000 --- a/docs/examples/memcache.php +++ /dev/null @@ -1,58 +0,0 @@ - - * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors - */ - -use Phpfastcache\CacheManager; -use Phpfastcache\Drivers\Memcache\Config; - -// Include composer autoloader -require __DIR__ . '/../../src/autoload.php'; - -$InstanceCache = CacheManager::getInstance('memcache',new Config([ - 'host' =>'127.0.0.1', - 'port' => 11211, - // 'sasl_user' => false, // optional - // 'sasl_password' => false // optional -])); - -/** - * In case you need to enable compress_data option: - * $InstanceCache = CacheManager::getInstance('memcache', ['compress_data' => true]); - * - * In case you need SASL authentication: - * $InstanceCache = CacheManager::getInstance('memcache', ['sasl_user' => 'hackerman', 'sasl_password' => '12345']); - * Warning: Memcache needs to be compiled with a specific option (--enable-memcached-sasl) to use sasl authentication, see: - * http://php.net/manual/fr/memcached.installation.php - */ - -/** - * Try to get $products from Caching First - * product_page is "identity keyword"; - */ -$key = "product_page"; -$CachedString = $InstanceCache->getItem($key); - -if (is_null($CachedString->get())) { - //$CachedString = "APC Cache --> Cache Enabled --> Well done !"; - // Write products to Cache in 10 minutes with same keyword - $CachedString->set("Memcache Cache --> Cache Enabled --> Well done !")->expiresAfter(5); - $InstanceCache->save($CachedString); - - echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // "; - echo $CachedString->get(); - -} else { - echo "READ FROM CACHE // "; - echo $CachedString->get(); -} - -echo '

Back to index -- Reload'; diff --git a/docs/examples/memcached.php b/docs/examples/memcached.php deleted file mode 100644 index 6054499d5..000000000 --- a/docs/examples/memcached.php +++ /dev/null @@ -1,55 +0,0 @@ - - * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors - */ - -use Phpfastcache\CacheManager; -use Phpfastcache\Drivers\Memcached\Config; - -// Include composer autoloader -require __DIR__ . '/../../vendor/autoload.php'; - -$InstanceCache = CacheManager::getInstance('memcached', new Config([ - 'host' =>'127.0.0.1', - 'port' => 11211, - // 'sasl_user' => false, // optional - // 'sasl_password' => false // optional -])); - -/** - * In case you need SASL authentication: - * $InstanceCache = CacheManager::getInstance('memcache', ['sasl_user' => 'hackerman', 'sasl_password' => '12345']); - * Warning: Memcache needs to be compiled with a specific option (--enable-memcached-sasl) to use sasl authentication, see: - * http://php.net/manual/fr/memcached.installation.php - */ - -/** - * Try to get $products from Caching First - * product_page is "identity keyword"; - */ -$key = "product_page"; -$CachedString = $InstanceCache->getItem($key); - -if (is_null($CachedString->get())) { - //$CachedString = "APC Cache --> Cache Enabled --> Well done !"; - // Write products to Cache in 10 minutes with same keyword - $CachedString->set("Memcached Cache --> Cache Enabled --> Well done !")->expiresAfter(5); - $InstanceCache->save($CachedString); - - echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // "; - echo $CachedString->get(); - -} else { - echo "READ FROM CACHE // "; - echo $CachedString->get(); -} - -echo '

Back to index -- Reload'; diff --git a/docs/examples/mongodb.php b/docs/examples/mongodb.php deleted file mode 100644 index 264bd475d..000000000 --- a/docs/examples/mongodb.php +++ /dev/null @@ -1,57 +0,0 @@ - - * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors - */ - -use Phpfastcache\CacheManager; -use Phpfastcache\Drivers\Mongodb\Config; - -// Include composer autoloader -require __DIR__ . '/../../vendor/autoload.php'; - -$InstanceCache = CacheManager::getInstance('mongodb', new Config([ - 'host' => '127.0.0.1', - 'port' => 27017, - 'username' => '', - 'password' => '', - 'timeout' => 1, - /** - * These ones are - * totally optional - */ - // 'collectionName' => 'Cache', - // 'databaseName' => 'phpFastCache' - -])); - - -/** - * Try to get $products from Caching First - * product_page is "identity keyword"; - */ -$key = "product_page"; -$CachedString = $InstanceCache->getItem($key); -/* var_dump($CachedString->get()); -exit; */ -if (is_null($CachedString->get())) { - // Write products to Cache in 10 minutes with same keyword - $CachedString->set("Mongodb Cache --> Cache Enabled --> Well done !")->expiresAfter(5); - $InstanceCache->save($CachedString); - - echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // "; - echo $CachedString->get(); - -} else { - echo "READ FROM CACHE // "; - echo $CachedString->get(); -} - -echo '

Back to index -- Reload'; diff --git a/docs/examples/predis.php b/docs/examples/predis.php deleted file mode 100644 index ba804772a..000000000 --- a/docs/examples/predis.php +++ /dev/null @@ -1,48 +0,0 @@ - - * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors - */ - -use Phpfastcache\CacheManager; -use Phpfastcache\Drivers\Predis\Config; - -// Include composer autoloader -require __DIR__ . '/../../vendor/autoload.php'; - -$InstanceCache = CacheManager::getInstance('predis', new Config([ - 'host' => '127.0.0.1', //Default value - 'port' => 6379, //Default value - 'password' => null, //Default value - 'database' => null, //Default value -])); - -/** - * Try to get $products from Caching First - * product_page is "identity keyword"; - */ -$key = "product_page"; -$CachedString = $InstanceCache->getItem($key); - -if (is_null($CachedString->get())) { - //$CachedString = "APC Cache --> Cache Enabled --> Well done !"; - // Write products to Cache in 10 minutes with same keyword - $CachedString->set("Predis Cache --> Cache Enabled --> Well done !")->expiresAfter(5); - $InstanceCache->save($CachedString); - - echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // "; - echo $CachedString->get(); - -} else { - echo "READ FROM CACHE // "; - echo $CachedString->get(); -} - -echo '

Back to index -- Reload'; diff --git a/docs/examples/redis.php b/docs/examples/redis.php deleted file mode 100644 index bdaa8eb78..000000000 --- a/docs/examples/redis.php +++ /dev/null @@ -1,48 +0,0 @@ - - * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors - */ - -use Phpfastcache\CacheManager; -use Phpfastcache\Drivers\Redis\Config; - -// Include composer autoloader -require __DIR__ . '/../../vendor/autoload.php'; - -$InstanceCache = CacheManager::getInstance('redis', new Config([ - 'host' => '127.0.0.1', //Default value - 'port' => 6379, //Default value - 'password' => null, //Default value - 'database' => null, //Default value -])); - -/** - * Try to get $products from Caching First - * product_page is "identity keyword"; - */ -$key = "product_page"; -$CachedString = $InstanceCache->getItem($key); - -if (is_null($CachedString->get())) { - //$CachedString = "APC Cache --> Cache Enabled --> Well done !"; - // Write products to Cache in 10 minutes with same keyword - $CachedString->set("Redis Cache --> Cache Enabled --> Well done !")->expiresAfter(5); - $InstanceCache->save($CachedString); - - echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // "; - echo $CachedString->get(); - -} else { - echo "READ FROM CACHE // "; - echo $CachedString->get(); -} - -echo '

Back to index -- Reload'; diff --git a/docs/examples/sqlite.php b/docs/examples/sqlite.php deleted file mode 100644 index 04939450e..000000000 --- a/docs/examples/sqlite.php +++ /dev/null @@ -1,55 +0,0 @@ - - * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors - */ - -// Include composer autoloader -require __DIR__ . '/../../vendor/autoload.php'; -// OR require_once("../src/phpFastCache/phpFastCache.php"); -date_default_timezone_set("Europe/Paris"); - - -use Phpfastcache\CacheManager; -use Phpfastcache\Config\Config; -use Phpfastcache\Core\phpFastCache; - -// Setup File Path on your config files -CacheManager::setDefaultConfig(new Config([ - "path" => sys_get_temp_dir(), -])); - -// In your class, function, you can call the Cache -$InstanceCache = CacheManager::getInstance('sqlite'); -// OR $InstanceCache = CacheManager::getInstance() <-- open examples/global.setup.php to see more - -/** - * Try to get $products from Caching First - * product_page is "identity keyword"; - */ -$key = "product_page"; -$CachedString = $InstanceCache->getItem($key); - -if (is_null($CachedString->get())) { - //$CachedString = "Files Cache --> Cache Enabled --> Well done !"; - // Write products to Cache in 10 minutes with same keyword - $CachedString->set("Files Cache --> Cache Enabled --> Well done !")->expiresAfter(5); - $InstanceCache->save($CachedString); - - echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // "; - echo $CachedString->get(); - -} else { - echo "READ FROM CACHE // "; - echo $CachedString->getExpirationDate()->format(Datetime::W3C); - echo $CachedString->get(); -} - -echo '

Back to index -- Reload'; diff --git a/docs/examples/ssdb.php b/docs/examples/ssdb.php deleted file mode 100644 index a90696677..000000000 --- a/docs/examples/ssdb.php +++ /dev/null @@ -1,48 +0,0 @@ - - * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors - */ - -use Phpfastcache\CacheManager; - -// Include composer autoloader -require __DIR__ . '/../../vendor/autoload.php'; - -// OR without composer: -// require __DIR__ . '/../src/autoload.php'; -// require __DIR__ . 'YOUR_PROJECT/phpfastcache/phpssdb/src/autoload.php'; -// require __DIR__ . 'YOUR_PROJECT/fig_cache_interfaces.php'; - - -$InstanceCache = CacheManager::getInstance('ssdb'); - -/** - * Try to get $products from Caching First - * product_page is "identity keyword"; - */ -$key = "product_page"; -$CachedString = $InstanceCache->getItem($key); - -if (is_null($CachedString->get())) { - //$CachedString = "APC Cache --> Cache Enabled --> Well done !"; - // Write products to Cache in 10 minutes with same keyword - $CachedString->set("Ssdb Cache --> Cache Enabled --> Well done !")->expiresAfter(5); - $InstanceCache->save($CachedString); - - echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // "; - echo $CachedString->get(); - -} else { - echo "READ FROM CACHE // "; - echo $CachedString->get(); -} - -echo '

Back to index -- Reload'; diff --git a/docs/examples/tagsMethods.php b/docs/examples/tagsMethods.php deleted file mode 100644 index aac939705..000000000 --- a/docs/examples/tagsMethods.php +++ /dev/null @@ -1,75 +0,0 @@ - - * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors - */ - -use Phpfastcache\CacheManager; - -// Include composer autoloader -require __DIR__ . '/../../vendor/autoload.php'; - -$InstanceCache = CacheManager::getInstance('apc'); - -/** - * Try to get $products from Caching First - * product_page is "identity keyword"; - */ -$key = "product_page"; -$key2 = "product_page2"; -$CachedString = $InstanceCache->getItem($key); -$CachedString2 = $InstanceCache->getItem($key2); - -if (is_null($CachedString->get()) || is_null($CachedString2->get())) { - // Write products to Cache in 10 minutes with same keyword - $CachedString->set("My beautifull Ios product") - ->expiresAfter(600) - ->addTag('Mobile') - ->addTag('Ios'); - - $CachedString2->set("My beautifull Android product") - ->expiresAfter(600) - ->addTag('Mobile') - ->addTag('Android'); - - $InstanceCache->save($CachedString); - $InstanceCache->save($CachedString2); - - echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // "; - echo '
TAGS of product_page item: ' . $CachedString->getTagsAsString(); - echo '
TAGS of product_page2 item: ' . $CachedString2->getTagsAsString(); - - -} else { - echo "
READ FROM CACHE WITH TAGS 'Ios' //
"; - echo '
';
-    var_dump($InstanceCache->getItemsByTag('Ios'));// Will output product_page item
-    echo '
'; - - echo "
READ FROM CACHE WITH TAGS 'Android' //
"; - echo '
';
-    var_dump($InstanceCache->getItemsByTag('Android'));// Will output product_page2 item
-    echo '
'; - - echo "
READ FROM CACHE WITH TAGS 'Ios,Android' //
"; - echo '
';
-    var_dump($InstanceCache->getItemsByTags(['Ios', 'Android']));// Will output product_page and product_page2 item
-    echo '
'; -} - -/** - * Finally you can delete by tags - * - * $InstanceCache->deleteItemsByTag('Ios'); - * $InstanceCache->deleteItemsByTags(['Ios', 'Android']); - **/ - - -echo '

Back to index -- Reload'; diff --git a/docs/examples/withoutComposer.php b/docs/examples/withoutComposer.php deleted file mode 100644 index e1d6e6ecd..000000000 --- a/docs/examples/withoutComposer.php +++ /dev/null @@ -1,46 +0,0 @@ - - * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors - */ - -use Phpfastcache\CacheManager; - -/** - * You don't use composer ? - * Fine! PhpFastCache provides - * his own autoloader. - */ -require __DIR__ . '/../src/autoload.php'; - -$InstanceCache = CacheManager::getInstance('memcache'); - -/** - * Try to get $products from Caching First - * product_page is "identity keyword"; - */ -$key = "product_page"; -$CachedString = $InstanceCache->getItem($key); - -if (is_null($CachedString->get())) { - //$CachedString = "APC Cache --> Cache Enabled --> Well done !"; - // Write products to Cache in 5 seconds with same keyword - $CachedString->set("Memcache Cache --> Cache Enabled --> Well done !")->expiresAfter(5); - $InstanceCache->save($CachedString); - - echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // "; - echo $CachedString->get(); - -} else { - echo "READ FROM CACHE // "; - echo $CachedString->get(); -} - -echo '

Back to index -- Reload'; diff --git a/docs/examples/xcache.php b/docs/examples/xcache.php deleted file mode 100644 index 44e0d641b..000000000 --- a/docs/examples/xcache.php +++ /dev/null @@ -1,42 +0,0 @@ - - * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors - */ - -use Phpfastcache\CacheManager; - -// Include composer autoloader -require __DIR__ . '/../../vendor/autoload.php'; - -$InstanceCache = CacheManager::getInstance('xcache'); - -/** - * Try to get $products from Caching First - * product_page is "identity keyword"; - */ -$key = "product_page"; -$CachedString = $InstanceCache->getItem($key); - -if (is_null($CachedString->get())) { - //$CachedString = "APC Cache --> Cache Enabled --> Well done !"; - // Write products to Cache in 10 minutes with same keyword - $CachedString->set("Xcache Cache --> Cache Enabled --> Well done !")->expiresAfter(5); - $InstanceCache->save($CachedString); - - echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // "; - echo $CachedString->get(); - -} else { - echo "READ FROM CACHE // "; - echo $CachedString->get(); -} - -echo '

Back to index -- Reload'; diff --git a/docs/examples/zend_disk.php b/docs/examples/zend_disk.php deleted file mode 100644 index 43bdaeccd..000000000 --- a/docs/examples/zend_disk.php +++ /dev/null @@ -1,48 +0,0 @@ - - * - */ -// Include composer autoloader -require __DIR__ . '/../../vendor/autoload.php'; -// OR require_once("../src/phpFastCache/phpFastCache.php"); -date_default_timezone_set("Europe/Paris"); - - -use Phpfastcache\CacheManager; -use Phpfastcache\Core\phpFastCache; - -// In your class, function, you can call the Cache -$InstanceCache = CacheManager::getInstance('zenddisk'); -// OR $InstanceCache = CacheManager::getInstance() <-- open examples/global.setup.php to see more - -/** - * Try to get $products from Caching First - * product_page is "identity keyword"; - */ -$key = "product_page"; -$CachedString = $InstanceCache->getItem($key); - -if (is_null($CachedString->get())) { - //$CachedString = "Zend Disk Cache --> Cache Enabled --> Well done !"; - // Write products to Cache in 10 minutes with same keyword - $CachedString->set("Zend Disk Cache --> Cache Enabled --> Well done !"); - $InstanceCache->save($CachedString); - - echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // "; - echo $CachedString->get(); - -} else { - echo "READ FROM CACHE // "; - echo $CachedString->getExpirationDate()->format(Datetime::W3C); - echo $CachedString->get(); -} - -echo '

Back to index -- Reload'; \ No newline at end of file diff --git a/docs/examples/zend_shm.php b/docs/examples/zend_shm.php deleted file mode 100644 index fd48f1f83..000000000 --- a/docs/examples/zend_shm.php +++ /dev/null @@ -1,41 +0,0 @@ - - * - */ -use Phpfastcache\CacheManager; - -// Include composer autoloader -require __DIR__ . '/../../vendor/autoload.php'; - -$InstanceCache = CacheManager::getInstance('zendshm'); - -/** - * Try to get $products from Caching First - * product_page is "identity keyword"; - */ -$key = "product_page"; -$CachedString = $InstanceCache->getItem($key); - -if (is_null($CachedString->get())) { - //$CachedString = "Zend Memory Cache --> Cache Enabled --> Well done !"; - // Write products to Cache in 10 minutes with same keyword - $CachedString->set("Zend Memory Cache --> Cache Enabled --> Well done !")->expiresAfter(5); - $InstanceCache->save($CachedString); - - echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // "; - echo $CachedString->get(); - -} else { - echo "READ FROM CACHE // "; - echo $CachedString->get(); -} - -echo '

Back to index -- Reload'; diff --git a/docs/migration/MigratingFromV8ToV9.md b/docs/migration/MigratingFromV8ToV9.md index f66302231..7fe421f23 100644 --- a/docs/migration/MigratingFromV8ToV9.md +++ b/docs/migration/MigratingFromV8ToV9.md @@ -4,7 +4,7 @@ Because the V9 is **relatively** not backward compatible with the V8, here's a g As of the V9 the mandatory minimum php version has been increased to 8.0+. Once released, the php version 8.1 will be unit-tested -### Embedded autoload has been removed (and therefore, embedded dependencies) +### Embedded autoload has been removed (and therefore, embedded dependencies too) Use [Composer](https://getcomposer.org/doc/03-cli.md#require) to include Phpfastcache in your project ### Removed magics methods from CacheManager `CacheManager::DriverName()` @@ -24,9 +24,9 @@ Finally, the config name you try to set MUST be recognized or an exception will Use `\Phpfastcache\CacheContract` instead. See [Wiki](https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV9%CB%96%5D-Cache-contract). ### Removed `Couchbase` driver (SDK 2 support dropped) -It is now replaced by `Couchbasev3` driver (SDK 3), the configuration options are all the same plus `scopeName` and `collectionName` that are now configurable. +It is now replaced by `Couchbasev3` driver (SDK 3), the configuration options remains the same plus `scopeName` and `collectionName` that are now configurable. -### Updated EventManager instances +### Updated EventManager callback parameters - Updated argument type #2 (`$items`) of `onCacheSaveMultipleItems()` event from `ExtendedCacheItemInterface[]` to `EventReferenceParameter($items)` - Updated argument type #2 (`$items`) of `onCacheCommitItem()` event from `ExtendedCacheItemInterface[]` to `EventReferenceParameter($items)` - Updated argument type #2 (`$value`) of `onCacheItemSet()` event from `mixed` to `EventReferenceParameter(mixed $value)` diff --git a/lib/Phpfastcache/Cluster/ClusterAggregator.php b/lib/Phpfastcache/Cluster/ClusterAggregator.php index 9a39296cc..ba98ed4a5 100644 --- a/lib/Phpfastcache/Cluster/ClusterAggregator.php +++ b/lib/Phpfastcache/Cluster/ClusterAggregator.php @@ -18,6 +18,7 @@ use Exception; use Phpfastcache\CacheManager; use Phpfastcache\Config\ConfigurationOption; +use Phpfastcache\Event\Event; use Phpfastcache\EventManager; use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException; use Phpfastcache\Exceptions\PhpfastcacheDriverException; @@ -149,12 +150,7 @@ public function getCluster(int $strategy = AggregatorInterface::STRATEGY_FULL_RE ...\array_values($this->driverPools) ); - /** - * @eventName CacheClusterBuilt - * @param $clusterAggregator AggregatorInterface - * @param $cluster ClusterPoolInterface - */ - $this->cluster->getEventManager()->dispatch('CacheClusterBuilt', $this, $this->cluster); + $this->cluster->getEventManager()->dispatch(Event::CACHE_CLUSTER_BUILT, $this, $this->cluster); } } else { throw new PhpfastcacheInvalidArgumentException('Unknown cluster strategy'); diff --git a/lib/Phpfastcache/Cluster/Drivers/MasterSlaveReplication/Driver.php b/lib/Phpfastcache/Cluster/Drivers/MasterSlaveReplication/Driver.php index 08f9ae93e..1a3be9325 100644 --- a/lib/Phpfastcache/Cluster/Drivers/MasterSlaveReplication/Driver.php +++ b/lib/Phpfastcache/Cluster/Drivers/MasterSlaveReplication/Driver.php @@ -18,6 +18,7 @@ use Phpfastcache\Cluster\ClusterPoolAbstract; use Phpfastcache\Core\Item\ExtendedCacheItemInterface; use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; +use Phpfastcache\Event\Event; use Phpfastcache\EventManager; use Phpfastcache\Exceptions\PhpfastcacheCoreException; use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException; @@ -77,7 +78,7 @@ protected function makeOperation(callable $operation) } catch (PhpfastcacheExceptionInterface $e) { try { $this->eventManager->dispatch( - 'CacheReplicationSlaveFallback', + Event::CACHE_REPLICATION_SLAVE_FALLBACK, $this, \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'] ); diff --git a/lib/Phpfastcache/Cluster/Drivers/RandomReplication/Driver.php b/lib/Phpfastcache/Cluster/Drivers/RandomReplication/Driver.php index 734b757a8..b70be4555 100644 --- a/lib/Phpfastcache/Cluster/Drivers/RandomReplication/Driver.php +++ b/lib/Phpfastcache/Cluster/Drivers/RandomReplication/Driver.php @@ -17,6 +17,7 @@ use Phpfastcache\Cluster\Drivers\MasterSlaveReplication\Driver as MasterSlaveReplicationDriver; use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; +use Phpfastcache\Event\Event; use Phpfastcache\Event\EventManagerInterface; use ReflectionException; use ReflectionMethod; @@ -36,7 +37,7 @@ public function __construct(string $clusterName, EventManagerInterface $em, Exte $randomPool = $driverPools[\random_int(0, \count($driverPools) - 1)]; $this->eventManager->dispatch( - 'CacheReplicationRandomPoolChosen', + Event::CACHE_REPLICATION_RANDOM_POOL_CHOSEN, $this, $randomPool ); diff --git a/lib/Phpfastcache/Core/Item/CacheItemTrait.php b/lib/Phpfastcache/Core/Item/CacheItemTrait.php index d794a023a..e4241565d 100644 --- a/lib/Phpfastcache/Core/Item/CacheItemTrait.php +++ b/lib/Phpfastcache/Core/Item/CacheItemTrait.php @@ -18,6 +18,7 @@ use DateInterval; use DateTime; use DateTimeInterface; +use Phpfastcache\Event\Event; use Phpfastcache\Event\EventManagerDispatcherTrait; use Phpfastcache\Event\EventReferenceParameter; use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException; @@ -63,13 +64,11 @@ public function set(mixed $value): static throw new PhpfastcacheInvalidArgumentException('The value set cannot be an instance of \\Closure.'); } - /** - * @eventName CacheSaveDeferredItem - * @param ExtendedCacheItemInterface $this - * @param mixed $value - * - */ - $this->eventManager->dispatch('CacheItemSet', $this, new EventReferenceParameter($value, true)); + if (\is_resource($value)) { + throw new PhpfastcacheInvalidArgumentException('The value set cannot be a resource'); + } + + $this->eventManager->dispatch(Event::CACHE_ITEM_SET, $this, new EventReferenceParameter($value, true)); $this->data = $value; @@ -101,12 +100,7 @@ public function setHit(bool $isHit): ExtendedCacheItemInterface public function expiresAt(?\DateTimeInterface $expiration): static { if ($expiration instanceof DateTimeInterface) { - /** - * @eventName CacheItemExpireAt - * @param ExtendedCacheItemInterface $this - * @param DateTimeInterface $expiration - */ - $this->eventManager->dispatch('CacheItemExpireAt', $this, $expiration); + $this->eventManager->dispatch(Event::CACHE_ITEM_EXPIRE_AT, $this, $expiration); $this->expirationDate = $expiration; } else { throw new PhpfastcacheInvalidArgumentException('$expiration must be an object implementing the DateTimeInterface got: ' . \gettype($expiration)); @@ -131,21 +125,11 @@ public function expiresAfter(int|\DateInterval|null $time): static $time = 30 * 24 * 3600 * 5; } - /** - * @eventName CacheItemExpireAt - * @param ExtendedCacheItemInterface $this - * @param DateTimeInterface $expiration - */ - $this->eventManager->dispatch('CacheItemExpireAfter', $this, $time); + $this->eventManager->dispatch(Event::CACHE_ITEM_EXPIRE_AFTER, $this, $time); $this->expirationDate = (new DateTime())->add(new DateInterval(\sprintf('PT%dS', $time))); } elseif ($time instanceof DateInterval) { - /** - * @eventName CacheItemExpireAt - * @param ExtendedCacheItemInterface $this - * @param DateTimeInterface $expiration - */ - $this->eventManager->dispatch('CacheItemExpireAfter', $this, $time); + $this->eventManager->dispatch(Event::CACHE_ITEM_EXPIRE_AFTER, $this, $time); $this->expirationDate = (new DateTime())->add($time); } else { diff --git a/lib/Phpfastcache/Core/Item/ExtendedCacheItemInterface.php b/lib/Phpfastcache/Core/Item/ExtendedCacheItemInterface.php index 62b467888..490908bda 100644 --- a/lib/Phpfastcache/Core/Item/ExtendedCacheItemInterface.php +++ b/lib/Phpfastcache/Core/Item/ExtendedCacheItemInterface.php @@ -20,6 +20,7 @@ use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; use Phpfastcache\Event\EventManagerDispatcherInterface; use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException; +use Phpfastcache\Exceptions\PhpfastcacheInvalidTypeException; use Phpfastcache\Exceptions\PhpfastcacheLogicException; use Phpfastcache\Util\ClassNamespaceResolverInterface; use Psr\Cache\CacheItemInterface; @@ -137,7 +138,7 @@ public function getLength(): int; /** * @param ExtendedCacheItemPoolInterface $driver * - * @return mixed + * @return ExtendedCacheItemInterface */ public function setDriver(ExtendedCacheItemPoolInterface $driver): ExtendedCacheItemInterface; @@ -145,7 +146,6 @@ public function setDriver(ExtendedCacheItemPoolInterface $driver): ExtendedCache * @param bool $isHit * * @return ExtendedCacheItemInterface - * @throws PhpfastcacheInvalidArgumentException */ public function setHit(bool $isHit): ExtendedCacheItemInterface; @@ -153,7 +153,7 @@ public function setHit(bool $isHit): ExtendedCacheItemInterface; * @param int $step * * @return ExtendedCacheItemInterface - * @throws PhpfastcacheInvalidArgumentException + * @throws PhpfastcacheInvalidTypeException */ public function increment(int $step = 1): ExtendedCacheItemInterface; @@ -161,7 +161,7 @@ public function increment(int $step = 1): ExtendedCacheItemInterface; * @param int $step * * @return ExtendedCacheItemInterface - * @throws PhpfastcacheInvalidArgumentException + * @throws PhpfastcacheInvalidTypeException */ public function decrement(int $step = 1): ExtendedCacheItemInterface; @@ -169,7 +169,7 @@ public function decrement(int $step = 1): ExtendedCacheItemInterface; * @param array|string $data * * @return ExtendedCacheItemInterface - * @throws PhpfastcacheInvalidArgumentException + * @throws PhpfastcacheInvalidTypeException */ public function append(array|string $data): ExtendedCacheItemInterface; @@ -177,7 +177,7 @@ public function append(array|string $data): ExtendedCacheItemInterface; * @param array|string $data * * @return ExtendedCacheItemInterface - * @throws PhpfastcacheInvalidArgumentException + * @throws PhpfastcacheInvalidTypeException */ public function prepend(array|string $data): ExtendedCacheItemInterface; diff --git a/lib/Phpfastcache/Core/Item/ExtendedCacheItemTrait.php b/lib/Phpfastcache/Core/Item/ExtendedCacheItemTrait.php index be6dcef05..6421d897a 100644 --- a/lib/Phpfastcache/Core/Item/ExtendedCacheItemTrait.php +++ b/lib/Phpfastcache/Core/Item/ExtendedCacheItemTrait.php @@ -20,6 +20,7 @@ use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; use Phpfastcache\Event\EventManagerInterface; use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException; +use Phpfastcache\Exceptions\PhpfastcacheInvalidTypeException; use Phpfastcache\Exceptions\PhpfastcacheLogicException; trait ExtendedCacheItemTrait @@ -180,12 +181,12 @@ public function isExpired(): bool public function isNull(): bool { - return $this->data === null; + return $this->get() === null; } public function isEmpty(): bool { - return empty($this->data); + return empty($this->get()); } /** @@ -213,22 +214,43 @@ public function getLength(): int return -1; } + /** + * @throws PhpfastcacheInvalidTypeException + */ public function increment(int $step = 1): ExtendedCacheItemInterface { + if ($this->data !== null && !\is_numeric($this->data)) { + throw new PhpfastcacheInvalidTypeException(\sprintf('Cannot increment on a "%s" type.', \gettype($this->data))); + } + $this->data += $step; return $this; } + /** + * @throws PhpfastcacheInvalidTypeException + */ public function decrement(int $step = 1): ExtendedCacheItemInterface { + if ($this->data !== null && !\is_numeric($this->data)) { + throw new PhpfastcacheInvalidTypeException(\sprintf('Cannot decrement on a "%s" type.', \gettype($this->data))); + } + $this->data -= $step; return $this; } + /** + * @throws PhpfastcacheInvalidTypeException + */ public function append(array|string $data): ExtendedCacheItemInterface { + if ($this->data !== null && !\is_string($this->data) && !\is_array($this->data)) { + throw new PhpfastcacheInvalidTypeException(\sprintf('Cannot append on a "%s" type.', \gettype($this->data))); + } + if (\is_array($this->data)) { $this->data[] = $data; } else { @@ -238,8 +260,15 @@ public function append(array|string $data): ExtendedCacheItemInterface return $this; } + /** + * @throws PhpfastcacheInvalidTypeException + */ public function prepend(array|string $data): ExtendedCacheItemInterface { + if ($this->data !== null && !\is_string($this->data) && !\is_array($this->data)) { + throw new PhpfastcacheInvalidTypeException(\sprintf('Cannot prepend on a "%s" type.', \gettype($this->data))); + } + if (\is_array($this->data)) { \array_unshift($this->data, $data); } else { diff --git a/lib/Phpfastcache/Core/Pool/CacheItemPoolTrait.php b/lib/Phpfastcache/Core/Pool/CacheItemPoolTrait.php index bf8659266..f558fdc19 100644 --- a/lib/Phpfastcache/Core/Pool/CacheItemPoolTrait.php +++ b/lib/Phpfastcache/Core/Pool/CacheItemPoolTrait.php @@ -19,6 +19,7 @@ use Phpfastcache\Core\Item\ExtendedCacheItemInterface; use Phpfastcache\Entities\DriverIO; use Phpfastcache\Entities\ItemBatch; +use Phpfastcache\Event\Event; use Phpfastcache\Event\EventManagerDispatcherTrait; use Phpfastcache\Event\EventReferenceParameter; use Phpfastcache\Exceptions\PhpfastcacheCoreException; @@ -151,18 +152,13 @@ public function getItem(string $key): ExtendedCacheItemInterface * The timeout has been reached * Consider that the batch has * failed and serve an empty item - * to avoid to get stuck with a + * to avoid get stuck with a * batch item stored in driver */ goto getItemDriverExpired; } - /** - * @eventName CacheGetItem - * @param $this ExtendedCacheItemPoolInterface - * @param $driverData ItemBatch - * @param $cacheSlamsSpendSeconds int - */ - $this->eventManager->dispatch('CacheGetItemInSlamBatch', $this, $driverData, $cacheSlamsSpendSeconds); + + $this->eventManager->dispatch(Event::CACHE_GET_ITEM_IN_SLAM_BATCH, $this, $driverData, $cacheSlamsSpendSeconds); /** * Wait for a second before @@ -231,12 +227,7 @@ public function getItem(string $key): ExtendedCacheItemInterface if ($item !== null) { - /** - * @eventName CacheGetItem - * @param $this ExtendedCacheItemPoolInterface - * @param $this ExtendedCacheItemInterface - */ - $this->eventManager->dispatch('CacheGetItem', $this, $item); + $this->eventManager->dispatch(Event::CACHE_GET_ITEM, $this, $item); $item->isHit() ? $this->getIO()->incReadHit() : $this->getIO()->incReadMiss(); @@ -267,12 +258,7 @@ public function hasItem(string $key): bool */ public function clear(): bool { - /** - * @eventName CacheClearItem - * @param $this ExtendedCacheItemPoolInterface - * @param $itemInstances ExtendedCacheItemInterface[] - */ - $this->eventManager->dispatch('CacheClearItem', $this, $this->itemInstances); + $this->eventManager->dispatch(Event::CACHE_CLEAR_ITEM, $this, $this->itemInstances); $this->getIO()->incWriteHit(); // Faster than detachAllItems() @@ -317,12 +303,7 @@ public function deleteItem(string $key): bool $item->setHit(false); $this->getIO()->incWriteHit(); - /** - * @eventName CacheCommitItem - * @param $this ExtendedCacheItemPoolInterface - * @param $item ExtendedCacheItemInterface - */ - $this->eventManager->dispatch('CacheDeleteItem', $this, $item); + $this->eventManager->dispatch(Event::CACHE_DELETE_ITEM, $this, $item); /** * De-register the item instance @@ -355,12 +336,7 @@ public function saveDeferred(CacheItemInterface $item): bool throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.'); } - /** - * @eventName CacheSaveDeferredItem - * @param $this ExtendedCacheItemPoolInterface - * @param $this ExtendedCacheItemInterface - */ - $this->eventManager->dispatch('CacheSaveDeferredItem', $this, $item); + $this->eventManager->dispatch(Event::CACHE_SAVE_DEFERRED_ITEM, $this, $item); $this->deferredList[$item->getKey()] = $item; return true; @@ -372,16 +348,10 @@ public function saveDeferred(CacheItemInterface $item): bool * @throws PhpfastcacheDriverException * @throws PhpfastcacheInvalidArgumentException * @throws PhpfastcacheLogicException - * @throws \ReflectionException */ public function commit(): bool { - /** - * @eventName CacheCommitItem - * @param $this ExtendedCacheItemPoolInterface - * @param $deferredList ExtendedCacheItemInterface[] - */ - $this->eventManager->dispatch('CacheCommitItem', $this, new EventReferenceParameter($this->deferredList)); + $this->eventManager->dispatch(Event::CACHE_COMMIT_ITEM, $this, new EventReferenceParameter($this->deferredList)); if (\count($this->deferredList)) { $return = true; @@ -424,13 +394,7 @@ public function save(CacheItemInterface $item): bool throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.'); } - /** - * @eventName CacheSaveItem - * @param $this ExtendedCacheItemPoolInterface - * @param $this ExtendedCacheItemInterface - */ - $this->eventManager->dispatch('CacheSaveItem', $this, $item); - + $this->eventManager->dispatch(Event::CACHE_SAVE_ITEM, $this, $item); if ($this->getConfig()->isPreventCacheSlams()) { /** diff --git a/lib/Phpfastcache/Core/Pool/ExtendedCacheItemPoolTrait.php b/lib/Phpfastcache/Core/Pool/ExtendedCacheItemPoolTrait.php index 6540be4b8..d15b645c0 100644 --- a/lib/Phpfastcache/Core/Pool/ExtendedCacheItemPoolTrait.php +++ b/lib/Phpfastcache/Core/Pool/ExtendedCacheItemPoolTrait.php @@ -16,6 +16,7 @@ namespace Phpfastcache\Core\Pool; use Phpfastcache\Core\Item\ExtendedCacheItemInterface; +use Phpfastcache\Event\Event; use Phpfastcache\Event\EventReferenceParameter; use Phpfastcache\Exceptions\PhpfastcacheCoreException; use Phpfastcache\Exceptions\PhpfastcacheDriverException; @@ -119,16 +120,10 @@ public function isAttached(CacheItemInterface $item): bool * @throws PhpfastcacheDriverException * @throws PhpfastcacheInvalidArgumentException * @throws PhpfastcacheLogicException - * @throws \ReflectionException */ public function saveMultiple(ExtendedCacheItemInterface...$items): bool { - /** - * @eventName CacheSaveItem - * @param $this ExtendedCacheItemPoolInterface - * @param $this ExtendedCacheItemInterface - */ - $this->eventManager->dispatch('CacheSaveMultipleItems', $this, new EventReferenceParameter($items)); + $this->eventManager->dispatch(Event::CACHE_SAVE_MULTIPLE_ITEMS, $this, new EventReferenceParameter($items)); if (\count($items)) { foreach ($items as $item) { diff --git a/lib/Phpfastcache/Core/Pool/IO/IOHelperTrait.php b/lib/Phpfastcache/Core/Pool/IO/IOHelperTrait.php index 80416626f..295d7df58 100644 --- a/lib/Phpfastcache/Core/Pool/IO/IOHelperTrait.php +++ b/lib/Phpfastcache/Core/Pool/IO/IOHelperTrait.php @@ -18,6 +18,7 @@ use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; use Phpfastcache\Core\Pool\TaggableCacheItemPoolTrait; use Phpfastcache\Entities\DriverStatistic; +use Phpfastcache\Event\Event; use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException; use Phpfastcache\Exceptions\PhpfastcacheIOException; use Phpfastcache\Util\Directory; @@ -271,14 +272,7 @@ protected function readFile(string $file): string */ protected function writeFile(string $file, string $data, bool $secureFileManipulation = false): bool { - /** - * @eventName CacheWriteFileOnDisk - * @param ExtendedCacheItemPoolInterface $this - * @param string $file - * @param bool $secureFileManipulation - * - */ - $this->eventManager->dispatch('CacheWriteFileOnDisk', $this, $file, $secureFileManipulation); + $this->eventManager->dispatch(Event::CACHE_WRITE_FILE_ON_DISK, $this, $file, $secureFileManipulation); if ($secureFileManipulation) { $tmpFilename = Directory::getAbsolutePath( diff --git a/lib/Phpfastcache/Drivers/Arangodb/Driver.php b/lib/Phpfastcache/Drivers/Arangodb/Driver.php index 5218fa595..bb319676c 100644 --- a/lib/Phpfastcache/Drivers/Arangodb/Driver.php +++ b/lib/Phpfastcache/Drivers/Arangodb/Driver.php @@ -30,6 +30,7 @@ use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; use Phpfastcache\Core\Pool\TaggableCacheItemPoolTrait; use Phpfastcache\Entities\DriverStatistic; +use Phpfastcache\Event\Event; use Phpfastcache\Event\EventReferenceParameter; use Phpfastcache\Exceptions\PhpfastcacheDriverConnectException; use Phpfastcache\Exceptions\PhpfastcacheDriverException; @@ -101,7 +102,7 @@ protected function driverConnect(): bool $connectionOptions[ArangoConnectionOptions::OPTION_CIPHERS] = $this->getConfig()->getCiphers(); } - $this->eventManager->dispatch('ArangodbConnection', $this, new EventReferenceParameter($connectionOptions)); + $this->eventManager->dispatch(Event::ARANGODB_CONNECTION, $this, new EventReferenceParameter($connectionOptions)); $this->instance = new ArangoConnection($connectionOptions); $this->documentHandler = new ArangoDocumentHandler($this->instance); @@ -214,7 +215,7 @@ protected function createCollection($collectionName): bool 'waitForSync' => false ]; - $this->eventManager->dispatch('ArangodbCollectionParams', $this, new EventReferenceParameter($params)); + $this->eventManager->dispatch(Event::ARANGODB_COLLECTION_PARAMS, $this, new EventReferenceParameter($params)); $this->collectionHandler->create($collection, $params); diff --git a/lib/Phpfastcache/Drivers/Dynamodb/Driver.php b/lib/Phpfastcache/Drivers/Dynamodb/Driver.php index 3ebefebaa..eeb8e9346 100644 --- a/lib/Phpfastcache/Drivers/Dynamodb/Driver.php +++ b/lib/Phpfastcache/Drivers/Dynamodb/Driver.php @@ -25,6 +25,7 @@ use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; use Phpfastcache\Core\Pool\TaggableCacheItemPoolTrait; use Phpfastcache\Entities\DriverStatistic; +use Phpfastcache\Event\Event; use Phpfastcache\Event\EventReferenceParameter; use Phpfastcache\Exceptions\PhpfastcacheDriverConnectException; use Phpfastcache\Exceptions\PhpfastcacheDriverException; @@ -210,7 +211,7 @@ protected function createTable() :void ] ]; - $this->eventManager->dispatch('DynamodbCreateTable', $this, new EventReferenceParameter($params)); + $this->eventManager->dispatch(Event::DYNAMODB_CREATE_TABLE, $this, new EventReferenceParameter($params)); $this->instance->createTable($params); $this->instance->waitUntil('TableExists', $params); diff --git a/lib/Phpfastcache/Event/Event.php b/lib/Phpfastcache/Event/Event.php new file mode 100644 index 000000000..059d0b893 --- /dev/null +++ b/lib/Phpfastcache/Event/Event.php @@ -0,0 +1,38 @@ + + * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors + */ + +declare(strict_types=1); + +namespace Phpfastcache\Event; + +class Event +{ + public const CACHE_GET_ITEM = 'CacheGetItem'; + public const CACHE_DELETE_ITEM = 'CacheDeleteItem'; + public const CACHE_SAVE_ITEM = 'CacheSaveItem'; + public const CACHE_SAVE_MULTIPLE_ITEMS = 'CacheSaveMultipleItems'; + public const CACHE_SAVE_DEFERRED_ITEM = 'CacheSaveDeferredItem'; + public const CACHE_COMMIT_ITEM = 'CacheCommitItem'; + public const CACHE_CLEAR_ITEM = 'CacheClearItem'; + public const CACHE_WRITE_FILE_ON_DISK = 'CacheWriteFileOnDisk'; + public const CACHE_GET_ITEM_IN_SLAM_BATCH = 'CacheGetItemInSlamBatch'; + public const CACHE_REPLICATION_SLAVE_FALLBACK = 'CacheReplicationSlaveFallback'; + public const CACHE_REPLICATION_RANDOM_POOL_CHOSEN = 'CacheReplicationRandomPoolChosen'; + public const CACHE_CLUSTER_BUILT = 'CacheClusterBuilt'; + public const CACHE_ITEM_SET = 'CacheItemSet'; + public const CACHE_ITEM_EXPIRE_AT = 'CacheItemExpireAt'; + public const CACHE_ITEM_EXPIRE_AFTER = 'CacheItemExpireAfter'; + public const ARANGODB_CONNECTION = 'ArangodbConnection'; + public const ARANGODB_COLLECTION_PARAMS = 'ArangodbCollectionParams'; + public const DYNAMODB_CREATE_TABLE = 'DynamodbCreateTable'; +} diff --git a/tests/AtomicOperations.test.php b/tests/AtomicOperations.test.php new file mode 100644 index 000000000..6ff4bd795 --- /dev/null +++ b/tests/AtomicOperations.test.php @@ -0,0 +1,130 @@ + + * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors + */ + +use Phpfastcache\CacheManager; +use Phpfastcache\Tests\Helper\TestHelper; + +chdir(__DIR__); +require_once __DIR__ . '/../vendor/autoload.php'; +require_once __DIR__ . '/mock/Autoload.php'; +$testHelper = new TestHelper('Atomic Operations'); +$pool = CacheManager::getInstance('Memstatic'); +$testHelper->printInfoText('Testing APPEND/PREPEND methods...'); + +{ + $cacheItem = $pool->getItem($testHelper->getRandomKey()); + $cacheItem->set(['alpha', 'bravo']); + $cacheItem->append('charlie'); + $pool->save($cacheItem); + + $target = ['alpha', 'bravo', 'charlie']; + if(count(array_intersect($cacheItem->get(), $target)) === count($target)){ + $testHelper->assertPass('Atomic operation APPEND on ARRAY works as expected'); + } else { + $testHelper->assertFail('Atomic operation APPEND on ARRAY did not worked as expected'); + } +} + +// Reset pool +unset($target, $cacheItem); +$pool->clear(); + + +{ + $cacheItem = $pool->getItem($testHelper->getRandomKey()); + $cacheItem->set('alpha_bravo'); + $cacheItem->append('_charlie'); + $pool->save($cacheItem); + + $target = 'alpha_bravo_charlie'; + if($cacheItem->get() === $target){ + $testHelper->assertPass('Atomic operation APPEND on STRING works as expected'); + } else { + $testHelper->assertFail('Atomic operation APPEND on STRING did not worked as expected'); + } +} + +// Reset pool +unset($target, $cacheItem); +$pool->clear(); + +{ + $cacheItem = $pool->getItem($testHelper->getRandomKey()); + $cacheItem->set(['bravo', 'charlie']); + $cacheItem->prepend('alpha'); + $pool->save($cacheItem); + + $target = ['alpha', 'bravo', 'charlie']; + if(count(array_intersect($cacheItem->get(), $target)) === count($target)){ + $testHelper->assertPass('Atomic operation PREPEND on ARRAY works as expected'); + } else { + $testHelper->assertFail('Atomic operation PREPEND on ARRAY did not worked as expected'); + } +} + +// Reset pool +unset($target, $cacheItem); +$pool->clear(); + +{ + $cacheItem = $pool->getItem($testHelper->getRandomKey()); + $cacheItem->set('bravo_charlie'); + $cacheItem->prepend('alpha_'); + $pool->save($cacheItem); + + $target = 'alpha_bravo_charlie'; + if($cacheItem->get() === $target){ + $testHelper->assertPass('Atomic operation PREPEND on STRING works as expected'); + } else { + $testHelper->assertFail('Atomic operation PREPEND on STRING did not worked as expected'); + } +} + +// Reset pool +unset($target, $cacheItem); +$pool->clear(); + +$testHelper->printInfoText('Testing INCREMENT/DECREMENT methods...'); + +{ + $cacheItem = $pool->getItem($testHelper->getRandomKey()); + $cacheItem->set(1330); + $cacheItem->increment(3 + 4); + $pool->save($cacheItem); + + if($cacheItem->get() === 1337){ + $testHelper->assertPass('Atomic operation INCREMENT on INT works as expected'); + } else { + $testHelper->assertFail('Atomic operation INCREMENT on INT did not worked as expected'); + } +} + +// Reset pool +unset($target, $cacheItem); +$pool->clear(); + +{ + $cacheItem = $pool->getItem($testHelper->getRandomKey()); + $cacheItem->set(1340); + $cacheItem->decrement(4 - 1); + $pool->save($cacheItem); + + if($cacheItem->get() === 1337){ + $testHelper->assertPass('Atomic operation DECREMENT on INT works as expected'); + } else { + $testHelper->assertFail('Atomic operation DECREMENT on INT did not worked as expected'); + } +} + +$testHelper->terminateTest(); diff --git a/tests/lib/Helper/TestHelper.php b/tests/lib/Helper/TestHelper.php index 081fd841c..9b5952182 100644 --- a/tests/lib/Helper/TestHelper.php +++ b/tests/lib/Helper/TestHelper.php @@ -559,4 +559,19 @@ public function exceptionHandler(Throwable $exception): void } $this->terminateTest(); } + + public function getRandomKey(string $prefix = 'test_', int $minBlockLength = 3): string + { + return $prefix . \implode( + '_', + \array_filter( + \array_map( + static function ($str) use ($minBlockLength) { + return \strlen($str) < $minBlockLength ? null : $str; + }, + \str_split(\bin2hex(\random_bytes(\random_int(6, 16))), \random_int($minBlockLength, $minBlockLength + 5)) + ) + ) + ); + } }