Exemplo n.º 1
0
 * the extent permitted by applicable law. You can redistribute it
 * and/or modify it under the terms of the Do What The F**k You Want
 * To Public License, Version 2, as published by Sam Hocevar. See
 * http://www.wtfpl.net/ for more details.
 *
 * 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) {
Exemplo n.º 2
0
<?php

/**
 * Copyright 2015-2016 Alexey Maslov <*****@*****.**>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Predis usage example
 * @author alxmsl
 */
// Firstly include base class
include '../vendor/autoload.php';
use alxmsl\Connection\Predis\PredisFactory;
// Create Redis Client instance with you configuration settings
$Redis = PredisFactory::createPredisByConfig(array('host' => 'localhost', 'port' => 6379));
// Use Redis commands
$Redis->set('test', '7');
var_dump($Redis->get('test'));
Exemplo n.º 3
0
 * To Public License, Version 2, as published by Sam Hocevar. See
 * http://www.wtfpl.net/ for more details.
 *
 * Hierarchical cache usage example
 * @author alxmsl
 * @date 8/28/14
 */
include '../vendor/autoload.php';
use alxmsl\Connection\Predis\PredisFactory;
use alxmsl\Primitives\Cache\Cache;
use alxmsl\Primitives\Cache\Item;
use alxmsl\Primitives\CacheFactory;
use alxmsl\Primitives\Cache\Example\Level2Cache;
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