Ejemplo n.º 1
0
 * Cache on predis usage example
 * @author alxmsl
 */
include '../vendor/autoload.php';
use alxmsl\Connection\Predis\PredisFactory;
use alxmsl\Primitives\Cache\Cache;
use alxmsl\Primitives\Cache\Item;
use alxmsl\Primitives\Cache\Exception\ExpiredException;
use alxmsl\Primitives\Cache\Exception\MissingException;
use alxmsl\Primitives\CacheFactory;
$Client = PredisFactory::createPredisByConfig(['host' => 'localhost', 'port' => 6379]);
$Cache = CacheFactory::createPredisCache('key_01', Cache::class, $Client);
// Cache missing example
$key = 'value_' . mt_rand(100, 500);
try {
    $Cache->get($key);
    printf("error: key %s found\n", $key);
} catch (MissingException $Ex) {
}
// Cached value expiration example
$Cache->set($key, 7, Item::TYPE_NUMBER, time() + 1);
sleep(2);
try {
    $Cache->get($key);
    printf("error: key %s not expired\n", $key);
} catch (ExpiredException $Ex) {
}
$Cache->set('some_key', 7);
unset($Cache);
$Cache = CacheFactory::createPredisCache('key_01', Cache::class, $Client);
var_dump($Cache->get('some_key')->getValue() == 7);
Ejemplo n.º 2
0
use alxmsl\Primitives\Cache\Example\Level3Cache;
use alxmsl\Primitives\Cache\Exception\MissingException;
$Connection = PredisFactory::createPredisByConfig(['host' => 'localhost', 'port' => 6379]);
$RootCache = CacheFactory::createPredisCache('key_03', Cache::class, $Connection);
$Level2Cache = CacheFactory::createPredisCache('key_03', Level2Cache::class, $Connection);
$Level3Cache = CacheFactory::createPredisCache('key_03', Level3Cache::class, $Connection);
// Leveled value write and read
$Level3Cache->set('some_level3_key', 5, Item::TYPE_NUMBER);
unset($Level3Cache);
$Level3Cache = CacheFactory::createPredisCache('key_03', Level3Cache::class, $Connection);
var_dump($Level3Cache->get('some_level3_key')->getValue() == 5);
// Check cached level 3 value from level 2
$Level2Value = $Level2Cache->get('level3')->getValue();
var_dump($Level2Value->some_level3_key->getValue() == $Level3Cache->get('some_level3_key')->getValue());
// Check cached level 3 value from root level
$RootLevelValue = $RootCache->get('level2')->getValue();
var_dump($RootLevelValue->level3->getValue()->some_level3_key->getValue() == $Level3Cache->get('some_level3_key')->getValue());
// Set another value on level 2 and invalidate level 3
$Level2Cache->set('another_level2_key', 7);
$Level3Cache->invalidate();
unset($Level2Cache);
// Then check level 2 cached value
$Level2Cache = CacheFactory::createPredisCache('key_03', Level2Cache::class, $Connection);
var_dump($Level2Cache->get('another_level2_key')->getValue() == 7);
// Check what level 3 is empty
try {
    $Level2Cache->get('level3');
    printf("error: level3 was not delete\n");
} catch (MissingException $Ex) {
    var_dump(true);
}