-
-
Notifications
You must be signed in to change notification settings - Fork 454
[V5˖] Fetching all keys
Georges.L edited this page Jul 10, 2017
·
7 revisions
Sometimes you may need to fetch all key stored inside your cache. Currently there's no such implementation that allows you to do this. Why ? Simply because not all drivers let us implements an abstract method that could help us to propose this feature.
But there's a good news, 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) <[email protected]> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <[email protected]>
*
*/
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