### Extending phpFastCache: V4 vs V5
Source: https://github.com/phpsocialnetwork/phpfastcache/wiki/[V4]-Migrating-your-code-to-the-V5
Demonstrates how to extend the phpFastCache library. The 'Then' example shows the V4 approach using `phpFastCache`, while the 'Now' example illustrates the V5 approach using `phpFastCacheAbstractProxy`.
```php
namespace My\Custom\Project;
use phpFastCache\Core\phpFastCache;
/**
* Class Cache
*/
class Cache extends phpFastCache
{
}
```
```php
namespace My\Custom\Project;
use phpFastCache\Proxy\phpFastCacheAbstractProxy;
/**
* Class Cache
*/
class Cache extends phpFastCacheAbstractProxy
{
}
```
--------------------------------
### Install Couchbasev4 Extension
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/docs/migration/MigratingFromV9.1ToV9.2.md
Installs the Couchbasev4 extension for phpfastcache, which has been added as a new extension.
```bash
composer install phpfastcache/couchbasev4-extension
```
--------------------------------
### CacheManager Driver Instantiation
Source: https://github.com/phpsocialnetwork/phpfastcache/wiki/[V8˖]-Migrating-your-code-to-the-V9
Demonstrates the new way to get a cache driver instance, replacing the old magic method syntax.
```php
use Phpfastcache\CacheManager;
// Old way (removed in V9)
// $cache = CacheManager::Files();
// New way
$cache = CacheManager::getInstance('Files');
```
--------------------------------
### Install Arangodb Extension
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/docs/migration/MigratingFromV9.1ToV9.2.md
Installs the Arangodb extension for phpfastcache, required if you were previously using the Arangodb driver.
```bash
composer install phpfastcache/arangodb-extension
```
--------------------------------
### Get/Set Cache Operations
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/docs/migration/MigratingFromV4ToV5.md
Illustrates the differences in getting and setting cache items between older and newer versions of phpFastCache. The 'Then' example uses direct get/set methods, while the 'Now' example utilizes getItem, set, and save methods with a configuration array.
```php
namespace My\Custom\Project;
use phpFastCache\Core\phpFastCache;
$cache = phpFastCache();
// or
$cache = __c();
$myCacheItem = $cache->get("myKey");
if($myCacheItem === null){
$myCacheItem = database_operation();
$cache->set("myKey", $myCacheItem, 600);
}
$template['myCacheItemData'] = $myCacheItem;
```
```php
namespace My\Custom\Project;
$config = [
'path' => 'An\absolute\path',
];
$cache = CacheManager::getInstance('Files', $config);
// or
$cache = CacheManager::Files($config);
$myCacheItem = $cache->getItem("myKey");
if(!$myCacheItem->isHit()){
$myCacheItem->set(database_operation());
$cache->save($myCacheItem);
}
$template['myCacheItemData'] = $myCacheItem->get();
```
--------------------------------
### Composer Installation
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/README.md
Command to install the phpFastCache library using Composer.
```bash
composer require phpfastcache/phpfastcache
```
--------------------------------
### README.md Link
Source: https://github.com/phpsocialnetwork/phpfastcache/wiki/_Footer
Provides a link to the main README.md file on GitHub for comprehensive project information and setup instructions.
```markdown
[README.md](https://github.com/PHPSocialNetwork/phpfastcache/blob/master/README.md)
```
--------------------------------
### PHP Cache Implementation with Files
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/README.md
Demonstrates how to configure phpFastCache to use the file system for caching. It shows how to set the cache path, get a cache instance, retrieve an item, and handle cache hits and misses. The example includes setting an expiration time for the cache item.
```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
?>
```
--------------------------------
### Extending phpFastCache Class
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/docs/migration/MigratingFromV4ToV5.md
Demonstrates how to extend the phpFastCache class in older and newer versions. The 'Then' example shows extending the core phpFastCache class, while the 'Now' example illustrates extending the phpFastCacheAbstractProxy class for V5 compatibility.
```php
namespace My\Custom\Project;
use phpFastCache\Core\phpFastCache;
/**
* Class Cache
*/
class Cache extends phpFastCache
{
}
```
```php
namespace My\Custom\Project;
use phpFastCache\Proxy\phpFastCacheAbstractProxy;
/**
* Class Cache
*/
class Cache extends phpFastCacheAbstractProxy
{
}
```
--------------------------------
### Cache Clearing
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/docs/migration/MigratingFromV4ToV5.md
Shows the evolution of cache clearing methods in phpFastCache. The 'Then' example uses a simple `clean()` method, whereas the 'Now' example demonstrates clearing cache items using `clear()` after obtaining an instance via `CacheManager`.
```php
namespace My\Custom\Project;
use phpFastCache\Core\phpFastCache;
$cache = phpFastCache();
// or
$cache = __c();
$cache->clean();
```
```php
namespace My\Custom\Project;
$config = [
'path' => 'An\absolute\path',
];
$cache = CacheManager::getInstance('Files', $config);
// or
$cache = CacheManager::Files($config);
$myCacheItem = $cache->clear();
```
--------------------------------
### Phpfastcache Migration Guides
Source: https://github.com/phpsocialnetwork/phpfastcache/wiki/Home
Guides for migrating your Phpfastcache implementation between different major versions (V4 to V5, V5 to V6, etc.). These guides detail the necessary code changes and potential breaking changes.
```PHP
// Example of a potential change when migrating from V4 to V5
// Old V4 syntax (hypothetical):
// Phpfastcache::setup(array('storage' => 'auto'));
// New V5 syntax:
// Phpfastcache::setup('auto');
// Example of a potential change when migrating from V5 to V6
// Old V5 syntax (hypothetical):
// Phpfastcache::setup(array('storage' => 'files', 'path' => '/tmp/cache'));
// New V6 syntax:
// Phpfastcache::setup('files', '/tmp/cache');
```
--------------------------------
### PSR-16 Adapter Basic Usage
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/README.md
Demonstrates the basic usage of the PSR-16 adapter for cache operations like setting, getting, and checking for keys.
```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
*/
```
--------------------------------
### Get/Set Cache Items: V4 vs V5
Source: https://github.com/phpsocialnetwork/phpfastcache/wiki/[V4]-Migrating-your-code-to-the-V5
Illustrates the differences in getting and setting cache items between phpFastCache V4 and V5. V4 used a direct method on the cache instance, while V5 utilizes `getItem`, `isHit`, `set`, and `save` on a cache item object.
```php
namespace My\Custom\Project;
use phpFastCache\Core\phpFastCache;
$cache = phpFastCache();
// or
$cache = __c();
$myCacheItem = $cache->get("myKey");
if($myCacheItem === null){
$myCacheItem = database_operation();
$cache->set("myKey", $myCacheItem, 600);
}
$template['myCacheItemData'] = $myCacheItem;
```
```php
namespace My\Custom\Project;
$config = [
'path' => 'An\absolute\path',
];
$cache = CacheManager::getInstance('Files', $config);
// or
$cache = CacheManager::Files($config);
$myCacheItem = $cache->getItem("myKey");
if(!$myCacheItem->isHit()){
$myCacheItem->set(database_operation());
$cache->save($myCacheItem);
}
$template['myCacheItemData'] = $myCacheItem->get();
```
--------------------------------
### Adding Standalone Extensions
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/README.md
Demonstrates how to add new driver extensions to phpfastcache using Composer, referencing the Couchbasev4 extension as an example.
```bash
composer install phpfastcache/couchbasev4-extension
```
--------------------------------
### ConfigurationOption Usage
Source: https://github.com/phpsocialnetwork/phpfastcache/wiki/[V8˖]-Migrating-your-code-to-the-V9
Illustrates the change in ConfigurationOption syntax from array-based access to object-notation, while showing valid constructor usage.
```php
use Phpfastcache\Configuration\ConfigurationOption;
use Phpfastcache\Drivers\Files\Config as FilesConfig;
// Array-compatible syntax no longer valid for setting values after instantiation
// $config = new ConfigurationOption();
// $config['yourKey'] = 'value'; // Throws an exception
// Object-notation syntax is now required for setting values
$config = new ConfigurationOption();
$config->setYourKey('value');
// However, array syntax is still valid within the constructor
$configFromArray = new ConfigurationOption(['yourKey' => 'yourValue']);
$filesConfigFromArray = new FilesConfig(['yourKey' => 'yourValue']);
// Setting a config value after instantiation requires the correct method
$config->setYourKey('newValue');
```
--------------------------------
### Catching LogicException
Source: https://github.com/phpsocialnetwork/phpfastcache/wiki/[V5˖]-Migrating-your-code-to-the-V6
In V6, `LogicException` must be caught as `phpFastCache\Exceptions\phpFastCacheLogicException`.
```php
namespace My\Custom\Project;
$instance = CacheManager::getInstance('Files');
try{
$item = $instance->getItem(array());
}catch(\phpFastCache\Exceptions\phpFastCacheLogicException $e){
//Catched exception code
}
```
--------------------------------
### Configuring Files Driver
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/docs/migration/MigratingFromV6ToV7.md
Demonstrates how to configure the Files driver for phpfastcache using a dedicated Config object or an array.
```php
// The recommended way is to use an alias to not confuse yourself
use Phpfastcache\Drivers\Files\Config as FilesConfig;
$config = new FilesConfig();
$config->setPath('/an/absolute/path');
$config->setSecureFileManipulation(true);
$cacheInstace = CacheManager::getInstance('Files', $config);
// This also works well:
$cacheInstace = CacheManager::getInstance('Files', new FilesConfig([
'path' => '/an/absolute/path'
]));
```
--------------------------------
### Removal of ActOnAll helper
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/docs/migration/MigratingFromV7ToV8.md
The `ActOnAll` helper, previously used to act on all instances, has been removed in V8 in favor of aggregated cluster support.
```php
/* Then: */
// The helper ActOnAll used to be useful to act on all instance
/* Now: */
// The "ActOnAll Helper" have been removed in profit of aggregated cluster support
```
--------------------------------
### Configuration Object Handling
Source: https://github.com/phpsocialnetwork/phpfastcache/wiki/[V6˖]-Migrating-your-code-to-the-V7
Explains how V7 now accepts a `PhpfastcacheConfigConfigurationOption` object (or its alias `PhpfastcacheConfigConfig`) for configuration, supporting fluent setters and ArrayAccess.
```php
use Phpfastcache\CacheManager;
use Phpfastcache\Config\Config;
// Using Config object with fluent setters
$config = new Config();
$config->setPath('/an/absolute/path');
$cacheInstance = CacheManager::getInstance('Files', $config);
// Using Config object with array access
$config = new Config();
$config['path'] = '/an/absolute/path';
$cacheInstance = CacheManager::getInstance('Files', $config);
// Using Config object with an array during instantiation
$cacheInstance = CacheManager::getInstance('Files', new Config([
'path' => '/an/absolute/path'
]));
```
--------------------------------
### phpFastcache API Version
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/docs/migration/MigratingFromV7ToV8.md
The phpFastcache API has been upgraded to version 3.0.0. Refer to the CHANGELOG_API.md for detailed changes.
```APIDOC
Phpfastcache API:
Version: 3.0.0
Changelog: ./../../CHANGELOG_API.md
```
--------------------------------
### Migrating CacheManager::getInstance() driver
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/docs/migration/MigratingFromV7ToV8.md
In phpFastCache V8, the 'Auto' driver for CacheManager::getInstance() has been removed. You must now explicitly specify the driver you wish to use.
```php
/* Then: */
// CacheManager::getInstance('Auto') or CacheManager::getInstance() expecting automatic driver chosen
/* Now: */
// Use CacheManager::getInstance('Files') or CacheManager::getInstance('Redis') or something else.
```
--------------------------------
### Composer Dependency Suggestion
Source: https://github.com/phpsocialnetwork/phpfastcache/wiki/[V5˖]-Migrating-your-code-to-the-V6
In V6, third-party libraries are no longer required but suggested. Developers must now explicitly require suggested dependencies using composer.
```php
composer require phpfastcache/phpssdb
```
--------------------------------
### Phpfastcache V5+ Fetching All Keys
Source: https://github.com/phpsocialnetwork/phpfastcache/wiki/Home
This guide explains how to retrieve all keys currently stored in the Phpfastcache. Note that this operation might be resource-intensive depending on the cache backend.
```PHP
getAllKeys();
print_r($allKeys);
} catch (Exception $e) {
echo "Error fetching all keys: " . $e->getMessage();
}
?>
```
--------------------------------
### Removal of fallback feature
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/docs/migration/MigratingFromV7ToV8.md
The `fallback` feature, useful when a backend failed to initialize, has been removed in V8. Users should now use aggregated cluster Master/Slave configurations.
```php
/* Then: */
// The fallback features used to be useful when a backend failed to initialize
/* Now: */
// Use aggregated cluster Master/Slave instead
```
--------------------------------
### Custom Driver Registration
Source: https://github.com/phpsocialnetwork/phpfastcache/wiki/[V8˖]-Migrating-your-code-to-the-V9
Shows how to register a custom cache driver using the `addCustomDriver` method, as demonstrated with the Cookie driver.
```php
use Phpfastcache\CacheManager;
// Example of registering a custom driver (like the removed Cookie driver)
// Assume CustomCookieDriver is a class implementing the necessary interfaces
// CacheManager::addCustomDriver(CustomCookieDriver::class);
// $cache = CacheManager::getInstance('CustomCookieDriver');
```
--------------------------------
### Couchbase Driver Update
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/docs/migration/MigratingFromV8ToV9.md
Highlights the change from the `Couchbase` driver to `Couchbasev3`. The configuration remains similar, with the addition of `scopeName` and `collectionName` as configurable options.
```php
use Phpfastcache\CacheManager;
// Configuration for Couchbasev3 driver:
$config = [
'host' => '127.0.0.1',
'port' => 11210,
'username' => 'user',
'password' => 'pass',
'bucketName' => 'my_bucket',
'scopeName' => 'my_scope', // New configurable option
'collectionName' => 'my_collection' // New configurable option
];
$cacheInstance = CacheManager::getInstance('Couchbasev3', $config);
```
--------------------------------
### Devrandom Driver Addition
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/docs/migration/MigratingFromV8ToV9.md
Introduces the new `Devrandom` driver, which offers configurable factor chance and data length, replacing the removed `Devtrue` and `Devfalse` drivers.
```php
use Phpfastcache\CacheManager;
$config = [
'factorChance' => 50, // Chance in percent (0-100)
'dataLength' => 1024 // Length of random data in bytes
];
$cacheInstance = CacheManager::getInstance('Devrandom', $config);
```
--------------------------------
### Phpfastcache V7+ Custom Driver Implementation
Source: https://github.com/phpsocialnetwork/phpfastcache/wiki/Home
This guide details how to implement your own cache drivers (backends) for Phpfastcache version 7 and above. This allows integration with custom storage solutions.
```PHP
'value']);
?>
```
--------------------------------
### PHPFastCache Configuration and Usage
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/README.md
Demonstrates how to configure and use PHPFastCache, including setting up the cache instance and performing basic cache operations like get, set, and delete. Highlights the new object-based configuration system in V9.
```php
set('path', '/path/to/cache/files'); // Set cache path
$cache = new Phpfastcache('file', $config);
// Set a value
$cache->set('mykey', 'myvalue', 300); // Key, Value, TTL (seconds)
// Get a value
$value = $cache->get('mykey');
if ($value !== null) {
echo 'Value: ' . $value;
} else {
echo 'Key not found or expired.';
}
// Delete a value
$cache->delete('mykey');
?>
```
--------------------------------
### Constants Namespace Change
Source: https://github.com/phpsocialnetwork/phpfastcache/wiki/[V6˖]-Migrating-your-code-to-the-V7
All constants starting with `_PFC_` have been moved to the `Phpfastcache\Autoload` namespace in V7 to avoid polluting the global namespace.
```php
// Previously:
// define('_PFC_SOME_CONSTANT', 'value');
// Now:
// use Phpfastcache\Autoload;
// Autoload::_PFC_SOME_CONSTANT;
```
--------------------------------
### Running PhpFastCache Tests and Quality Checks
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/CONTRIBUTING.md
This snippet details the commands required to set up dependencies, run code quality checks (PHPCS, PHPMD, PHPStan), and execute unit tests for the PhpFastCache project. It also mentions a batch file for Windows users to streamline quality checks.
```bash
#!/bin/bash
# Install dependencies
./bin/ci/scripts/install_dependencies.sh
# Run code sniffer
./vendor/bin/phpcs lib/ --report=summary
# Run mess detector
./vendor/bin/phpmd lib/ ansi phpmd.xml
# Run static analysis
./vendor/bin/phpstan analyse lib/ -c phpstan_lite.neon 2>&1
# Run all unit tests
php -f ./bin/ci/run_tests.php
```
```batch
@echo off
REM Run code quality checks on Windows
call quality.bat
```
--------------------------------
### Basic Usage of phpfastcache
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/bin/stubs/leveldb/README.md
This snippet demonstrates the basic usage of phpfastcache, including setting and retrieving cache items. It shows how to initialize the cache and perform common operations.
```php
'files',
'path' => '/tmp/phpfastcache_test',
'defaultTtl'=> 300
];
Phpfastcache::setup($config);
// Get cache instance
$cache = Phpfastcache::get('myCache');
// Set item
$cache->set('mykey', 'myvalue', 60); // value, ttl in seconds
// Get item
$value = $cache->get('mykey');
if ($value !== null) {
echo "Value from cache: " . $value;
} else {
echo "Cache miss.";
}
// Delete item
$cache->delete('mykey');
?>
```
--------------------------------
### API Version Upgrade and Renamed Methods
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/docs/migration/MigratingFromV8ToV9.md
Details the API version upgrade to 4.0.0 and the renaming of specific methods within the `Api` class for better consistency. For example, `getPhpFastCacheVersion()` is now `getPhpfastcacheVersion()`.
```php
use Phpfastcache\Api;
$api = new Api();
// Renamed methods:
// $version = $api->getPhpFastCacheVersion(); // Old
$version = $api->getPhpfastcacheVersion(); // New
// $changelog = $api->getPhpFastCacheChangelog(); // Old
$changelog = $api->getPhpfastcacheChangelog(); // New
// $gitHash = $api->getPhpFastCacheGitHeadHash(); // Old
$gitHash = $api->getPhpfastcacheGitHeadHash(); // New
```
--------------------------------
### Cache Manager Configuration Updates
Source: https://github.com/phpsocialnetwork/phpfastcache/wiki/[V6˖]-Migrating-your-code-to-the-V7
Explains the changes in cache manager configuration methods. The deprecated `CacheManager::setup()` method has been removed, and `CacheManager::setDefaultConfig()` is now the recommended approach.
```php
// Deprecated in V6, removed in V7:
// CacheManager::setup();
// Recommended method:
CacheManager::setDefaultConfig();
```
--------------------------------
### Correct Cache Driver Instantiation
Source: https://github.com/phpsocialnetwork/phpfastcache/wiki/[V5˖]-Why-calling-getInstance()-each-time-is-a-bad-practice-?
Illustrates the recommended approach of obtaining a single driver instance and reusing it for multiple cache operations, enhancing performance.
```php
$driverInstance = CacheManager::getInstance('driverName');
$cacheItem = $driverInstance->getItem('myKey')->get();
$cacheItem2 = $driverInstance->getItem('myKey2')->get();
$cacheItem3 = $driverInstance->getItem('myKey3')->get();
```
--------------------------------
### CacheContract Callable Syntax
Source: https://github.com/phpsocialnetwork/phpfastcache/wiki/[V9˖]-Cache-contract
Demonstrates the new callable syntax for the CacheContract class in phpfastcache V9, allowing direct invocation instead of using the get() method. This syntax is functionally equivalent to the get() method.
```php
$value = (new CacheContract($cacheInstance))->get($cacheKey, static function() use ($testHelper){
$testHelper->assertPass('The CacheContract class is callable via __invoke()');
return null;
});
$value = (new CacheContract($cacheInstance))($cacheKey, static function() use ($testHelper){
$testHelper->assertPass('The CacheContract class is callable via __invoke()');
return null;
});
```
--------------------------------
### PHPfastcache PSR-16 Adapter Usage
Source: https://github.com/phpsocialnetwork/phpfastcache/wiki/[V6˖]-Psr16-adapter
This snippet demonstrates how to initialize and use the PSR-16 adapter from PHPfastcache. It shows how to set a value with a given key and retrieve it, including checking if the key exists. The adapter is configured with a default driver and an optional configuration array.
```php
/**
*
* This file is part of Phpfastcache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
*
* @author Georges.L (Geolim4)
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
*/
use Phpfastcache\Helper\Psr16Adapter;
chdir(__DIR__);
require_once __DIR__ . '/../vendor/autoload.php';
$defaultDriver = (!empty($argv[1]) ? ucfirst($argv[1]) : 'Files');
$Psr16Adapter = new Psr16Adapter($defaultDriver, [/* Config array */]);
if(!$Psr16Adapter->has('test-key')){
$nastyStuff = 'Database/webservice/etc operations';
$Psr16Adapter->set('test-key', $nastyStuff);
}else{
$value = $Psr16Adapter->get('test-key');
}
```
--------------------------------
### Aggregatable Pool Interface
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/CHANGELOG_API.md
Introduces `AggregatablePoolInterface::isAggregatedBy()` to get the aggregator object and `AggregatablePoolInterface::setAggregatedBy()` to set it.
```php
public function isAggregatedBy(): ?ClusterPoolInterface;
public function setAggregatedBy(ClusterPoolInterface $clusterPool): static;
```
--------------------------------
### Catching InvalidArgumentException
Source: https://github.com/phpsocialnetwork/phpfastcache/wiki/[V5˖]-Migrating-your-code-to-the-V6
In V6, `InvalidArgumentException` must be caught as `phpFastCache\Exceptions\phpFastCacheInvalidArgumentException`, which implements `Psr\Cache\InvalidArgumentException`.
```php
namespace My\Custom\Project;
$instance = CacheManager::getInstance('Files');
try{
$item = $instance->getItem(array());
}catch(\phpFastCache\Exceptions\phpFastCacheInvalidArgumentException $e){
//Catched exception code
}
```
--------------------------------
### phpfastcache Driver Overview
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/README.md
Lists the caching drivers supported by phpfastcache, categorized by type. Includes core drivers and extensions, with notes on deprecation and new additions.
```markdown
| Regular drivers | High performances drivers | Development drivers _(Core)_ | Cluster-Aggregated drivers _(Core)_ |
|--------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------|--------------------------------------------|---------------------------------------|
| `Apcu` _(Core)_
_(APC support removed)_ | `Arangodb` _([Extension](https://github.com/PHPSocialNetwork/arangodb-extension))_ | `Devnull` | `FullReplicationCluster` |
| `Dynamodb` _([Extension](https://github.com/PHPSocialNetwork/dynamodb-extension))_ | `Cassandra`
_(PHP extension is no more maintained by Datastax, might be deprecated in v10)_ | `Devrandom` | `SemiReplicationCluster` |
| `Files` _(Core)_ | `CouchBasev3` _(Core)_
_(Will be deprecated as of v10)_ | `Memory`
(Previously named `Memstatic`) | `MasterSlaveReplicationCluster` |
| `Firestore` _([Extension](https://github.com/PHPSocialNetwork/firestore-extension))_ | `Couchbasev4` _([Extension](https://github.com/PHPSocialNetwork/couchbasev4-extension))_ | | `RandomReplicationCluster` |
| `Leveldb` _(Core)_ | `Couchdb` _([Extension](https://github.com/PHPSocialNetwork/couchdb-extension))_ | | |
| `Memcache(d)` _(Core)_ | `Mongodb` _([Extension](https://github.com/PHPSocialNetwork/mongodb-extension))_ | | |
| `Solr` _([Extension](https://github.com/PHPSocialNetwork/solr-extension))_ | `Predis` _(Core)_ | | |
| `Sqlite` _(Core)_ | `Ravendb` _([Extension](https://github.com/PHPSocialNetwork/ravendb-extension)) | | |
| ` Wincache` _(Core)_
(**Deprecated** as of v9.2, will be removed as of v10) | `Relay` ([By end of 2024](https://relay.so/)) | |
| `Zend Disk Cache` _(Core)_ | `Redis`/`RedisCluster` _(Core)_ | | |
| | `Ssdb` _(Core)_ | | |
| | `Zend Memory Cache` _(Core)_ | | |
```
--------------------------------
### Migrating from APC to APCu driver
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/docs/migration/MigratingFromV7ToV8.md
The 'APC' driver has been removed in phpFastCache V8. Use 'APCu' instead.
```php
/* Then: */
// CacheManager::getInstance('Apc')
/* Now: */
// CacheManager::getInstance('Apcu')
```
--------------------------------
### phpfastcache Documentation Relocation
Source: https://github.com/phpsocialnetwork/phpfastcache/wiki/[V6˖]-Introducing-to-events
The main documentation for phpfastcache has been moved to a GitHub repository for better management and collaboration. Please refer to the linked document for the most up-to-date information.
```Markdown
This Wiki page has been moved [here](https://github.com/PHPSocialNetwork/phpfastcache/blob/master/docs/EVENTS.md)
```
--------------------------------
### Phpfastcache V5+ Good Practices
Source: https://github.com/phpsocialnetwork/phpfastcache/wiki/Home
This section covers essential good practices for using Phpfastcache version 5 and above. It addresses common pitfalls like repeatedly calling getInstance() and handling unsupported characters in cache keys.
```PHP
set('key', 'value');
// $cache = Phpfastcache::getInstance();
// $cache->get('key');
// Use:
$cache = Phpfastcache::getInstance();
$cache->set('key', 'value');
$value = $cache->get('key');
?>
```
--------------------------------
### PSR-16 Adapter Methods
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/README.md
Lists the core methods available in the PSR-16 adapter for cache management.
```APIDOC
Psr16Adapter:
get(key, default = null): mixed
Retrieves a value from the cache.
set(key, value, ttl = null): bool
Stores a value in the cache.
delete(key): bool
Removes a value from the cache.
clear(): bool
Clears the entire cache.
getMultiple(keys, default = null): array
Retrieves multiple values from the cache.
setMultiple(values, ttl = null): bool
Stores multiple values in the cache.
deleteMultiple(keys): bool
Removes multiple values from the cache.
has(key): bool
Checks if a key exists in the cache.
```
--------------------------------
### Using CacheContract instead of CacheConditionalHelper
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/docs/migration/MigratingFromV8ToV9.md
Shows the recommended way to handle cache conditions. The deprecated `PhpfastcacheHelperCacheConditionalHelper` should be replaced with `PhpfastcacheCacheContract`.
```php
use Phpfastcache\CacheContract;
// Deprecated:
// use Phpfastcache\Helper\CacheConditionalHelper;
// $conditionalHelper = new CacheConditionalHelper($cacheInstance);
// Recommended:
$cacheContract = $cacheInstance; // Assuming $cacheInstance implements CacheContract
```
--------------------------------
### Deprecated method CacheManager::getStaticSystemDrivers() removal
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/docs/migration/MigratingFromV7ToV8.md
The deprecated method `CacheManager::getStaticSystemDrivers()` has been removed in V8. It is replaced by `CacheManager::getDriverList()`.
```php
/* Then: */
// Calling CacheManager::getStaticSystemDrivers()
/* Now: */
// Replaced by CacheManager::getDriverList()
```
--------------------------------
### Solr Driver Configuration and Initialization
Source: https://github.com/phpsocialnetwork/phpfastcache/wiki/[V9.1˖]-Configuring-a-Solr-driver
Demonstrates how to configure and initialize the Solr driver for phpfastcache. It includes setting core name, port, host, path, scheme, and mapping schema. Requires Solarium 6.x or greater.
```php
use Phpfastcache\Drivers\Solr\Driver as SolrDriver;
use Phpfastcache\Drivers\Solr\Config as SolrConfig;
/** @var SolrConfig $solrConfig */
$solrConfig = new SolrConfig();
$solrConfig->setCoreName('phpfastcache'); // Optional: Default value
$solrConfig->setPort(8983); // Optional: Default value
$solrConfig->setHost('127.0.0.1'); // Optional: Default value
$solrConfig->setPath('/'); // Optional: Default value
$solrConfig->setScheme('http'); // Optional: Default value
/**
* Optional:
*
* You can change the mapping schema used by Phpfastcache.
* The keys are the Phpfastcache internal index. All required.
* The values are the name of your Solr schema.
*/
$solrConfig->setMappingSchema([
SolrDriver::SOLR_DEFAULT_ID_FIELD => Driver::SOLR_DEFAULT_ID_FIELD,
SolrDriver::SOLR_DISCRIMINATOR_FIELD => Driver::SOLR_DISCRIMINATOR_FIELD . '_s',
ExtendedCacheItemPoolInterface::DRIVER_KEY_WRAPPER_INDEX => ExtendedCacheItemPoolInterface::DRIVER_KEY_WRAPPER_INDEX . '_s',
ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX => ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX . '_s',
ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX => ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX . '_s',
ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX => ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX . '_s',
ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX => ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX . '_s',
TaggableCacheItemPoolInterface::DRIVER_TAGS_WRAPPER_INDEX => TaggableCacheItemPoolInterface::DRIVER_TAGS_WRAPPER_INDEX . '_ss',
]);
/**
* Optional:
*
* You can change the PSR-14 event dispatcher service used (and required) by solarium, by your own one.
*/
// $solrConfig->setEventDispatcher($yourEventDispatcher);
$cacheInstance = CacheManager::getInstance('Solr', $solrConfig);
```
--------------------------------
### Cache Item JSON Serialization
Source: https://github.com/phpsocialnetwork/phpfastcache/blob/master/CHANGELOG_API.md
Implements the JsonSerializable interface for cache items and provides methods to get cache data as JSON strings.
```APIDOC
ExtendedCacheItemInterface::getDataAsJsonString(): string
- Returns the data of the cache item as a JSON string.
ExtendedCacheItemPoolInterface::getItemsAsJsonString(): string
- Returns all items in the pool as a JSON string.
ExtendedCacheItemPoolInterface::getItemsByTagsAsJsonString(array $tags): string
- Returns items filtered by tags as a JSON string.
```
--------------------------------
### Namespace and Class Name Changes (V6 vs V7)
Source: https://github.com/phpsocialnetwork/phpfastcache/wiki/[V6˖]-Migrating-your-code-to-the-V7
Illustrates the change in namespace and class naming conventions from V6 to V7 to comply with modern standards. This includes capitalization of namespaces and class names.
```php
// V6 Example:
namespace phpFastCache;
class phpFastCacheAbstractProxy {}
// V7 Example:
namespace Phpfastcache;
class PhpfastcacheAbstractProxy {}
```
--------------------------------
### Standalone Autoloader Location
Source: https://github.com/phpsocialnetwork/phpfastcache/wiki/[V6˖]-Migrating-your-code-to-the-V7
While using Composer is recommended, a standalone autoloader is still available and is now located in the `lib/autoload` directory.
```php
// To use the standalone autoloader:
// require_once 'lib/autoload/autoload.php';
```
--------------------------------
### Item Instance Type Hint Migration
Source: https://github.com/phpsocialnetwork/phpfastcache/wiki/[V5˖]-Migrating-your-code-to-the-V6
The interface implemented by item instances has changed from `phpFastCache\Cache\ExtendedCacheItemInterface` to `phpFastCache\Core\Item\ExtendedCacheItemInterface` in V6.
```php
namespace My\Custom\Project;
$instance = CacheManager::getInstance('Files');
$item = $instance->getItem('key');
if($item instanceof \phpFastCache\Core\Item\ExtendedCacheItemInterface)
{
// Some code
}
```
--------------------------------
### Driver Instance Type Hint Migration
Source: https://github.com/phpsocialnetwork/phpfastcache/wiki/[V5˖]-Migrating-your-code-to-the-V6
The interface implemented by driver instances has changed from `phpFastCache\Cache\ExtendedCacheItemPoolInterface` to `phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface` in V6.
```php
namespace My\Custom\Project;
$instance = CacheManager::getInstance('Files');
if($instance instanceof \phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface)
{
// Some code
}
```