Skip to content

Commit 4fb638e

Browse files
committed
Added fetchAllKeys example as per #494
1 parent 1447dfa commit 4fb638e

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

docs/examples/fetchAllKeys.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
*
4+
* This file is part of phpFastCache.
5+
*
6+
* @license MIT License (MIT)
7+
*
8+
* For full copyright and license information, please see the docs/CREDITS.txt file.
9+
*
10+
* @author Khoa Bui (khoaofgod) <[email protected]> http://www.phpfastcache.com
11+
* @author Georges.L (Geolim4) <[email protected]>
12+
*
13+
*/
14+
use phpFastCache\CacheManager;
15+
16+
// Include composer autoloader
17+
require __DIR__ . '/../../vendor/autoload.php';
18+
19+
$InstanceCache = CacheManager::getInstance('files');
20+
$InstanceCache->clear();
21+
22+
/**
23+
* @var $keys \phpFastCache\Core\Item\ExtendedCacheItemInterface[]
24+
*/
25+
$keyPrefix = "product_page_";
26+
$keys = [];
27+
28+
for ($i=1;$i<=10;$i++)
29+
{
30+
$keys[$keyPrefix . $i] = $InstanceCache->getItem($keyPrefix . $i);
31+
if(!$keys[$keyPrefix . $i]->isHit()){
32+
$keys[$keyPrefix . $i]
33+
->set(uniqid('pfc', true))
34+
->addTag('pfc');
35+
}
36+
}
37+
38+
$InstanceCache->saveMultiple($keys);
39+
40+
/**
41+
* Remove items references from memory
42+
*/
43+
unset($keys);
44+
$InstanceCache->detachAllItems();
45+
gc_collect_cycles();
46+
47+
48+
/**
49+
* Now get the items by a specific tag
50+
*/
51+
$keys = $InstanceCache->getItemsByTag('pfc');
52+
foreach ($keys as $key) {
53+
echo "Key: {$key->getKey()} =&gt; {$key->get()}<br />";
54+
}
55+
56+
echo '<br /><br /><a href="/">Back to index</a>&nbsp;--&nbsp;<a href="./' . basename(__FILE__) . '">Reload</a>';

0 commit comments

Comments
 (0)