-
-
Notifications
You must be signed in to change notification settings - Fork 453
[V5˖] Fetching all keys
As of the v9.2 there's a limited support to retrieve all keys natively with an optional $pattern
parameter which is limited to some drivers.
The method ExtendedCacheItemInterface::getAllItems(string $pattern = '')
allows you to retrieve all the cache items from the backend with an internal hard-limitation of 9999 elements.
The following table will let you see the support of this feature as of v9.2:
Driver name |
getAllItems() Support |
$pattern parameter support |
Information |
---|---|---|---|
Apcu | 🔴 | 🔴 | |
Arangodb | 🔴 | 🔴 | |
Cassandra | 🔴 | 🔴 | |
Couchbasev3 | 🔴 | 🔴 | |
Couchdb | ✅ | 🔴 | No native pattern support. |
Devnull | 🔴 | 🔴 | |
Devrandom | 🔴 | 🔴 | |
Dynamodb | 🔴 | 🔴 | |
Files | 🔴 | 🔴 | |
Firestore | ✅ | 🔴 | No native pattern support. |
Leveldb | 🔴 | 🔴 | |
Memcache | 🔴 | 🔴 | |
Memcached | 🟧 | 🔴 | No native pattern support. The internal getAllKeys() method of Memcached is broken on some versions. |
Memstatic | ✅ | ✅ | Pattern support in development. |
Mongodb | ✅ | ✅ | |
Predis | ✅ | ✅ | Planned |
Redis | ✅ | ✅ | Pattern documentation |
Solr | ✅ | ✅ | Pattern documentation |
Sqlite | 🔴 | 🔴 | |
Ssdb | 🔴 | 🔴 | |
Wincache | 🔴 | 🔴 | |
Zenddisk | 🔴 | 🔴 | |
Zendshm | 🔴 | 🔴 |
Legend: ✅ Full support, 🟧 Partial support, 🔴 No support, 🚧 Planned/In development
Before the v9.2 was released, there's a tweak to allow you to "fetch all keys" from the cache: You can achieve this using tags for a reasonable amount of cache items (less than 10k, except if you have a consequent server memory setting up).
<?php
/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*
*/
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()}<br />";
}
By this way you can retrieve every keys in cache tagged "pfc".
❓ Finally, if you need help, always check out the inevitable README.md