示例#1
0
use alxmsl\Primitives\Cache\Example\Level3Cache;
use alxmsl\Primitives\Cache\Exception\MissingException;
$Connection = RedisFactory::createRedisByConfig(['host' => 'localhost', 'port' => 6379]);
$RootCache = CacheFactory::createRedisCache('key_03', Cache::class, $Connection);
$Level2Cache = CacheFactory::createRedisCache('key_03', Level2Cache::class, $Connection);
$Level3Cache = CacheFactory::createRedisCache('key_03', Level3Cache::class, $Connection);
// Leveled value write and read
$Level3Cache->set('some_level3_key', 5, Item::TYPE_NUMBER);
unset($Level3Cache);
$Level3Cache = CacheFactory::createRedisCache('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::createRedisCache('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);
}
示例#2
0
include '../vendor/autoload.php';
use alxmsl\Connection\Redis\RedisFactory;
use alxmsl\Primitives\Cache\Cache;
use alxmsl\Primitives\Cache\Item;
use alxmsl\Primitives\CacheFactory;
$Connection = RedisFactory::createRedisByConfig(['host' => 'localhost', 'port' => 6379]);
$Cache = CacheFactory::createRedisCache('key_02', Cache::class, $Connection);
$Cache->invalidate();
unset($Cache);
// Append array example
$Cache = CacheFactory::createRedisCache('key_02', Cache::class, $Connection);
$Cache->invalidate();
$Cache->append('some_array', 7, Item::TYPE_ARRAY);
unset($Cache);
$Cache = CacheFactory::createRedisCache('key_02', Cache::class, $Connection);
$Cache->append('some_array', 1);
var_dump($Cache->get('some_array')->getValue() == [7, 1]);
// Increment example
$Cache = CacheFactory::createRedisCache('key_02', Cache::class, $Connection);
$Cache->append('some_number', 7, Item::TYPE_NUMBER);
unset($Cache);
$Cache = CacheFactory::createRedisCache('key_02', Cache::class, $Connection);
$Cache->append('some_number', 2);
var_dump($Cache->get('some_number')->getValue() == 9);
// Strings concatenation example
$Cache = CacheFactory::createRedisCache('key_02', Cache::class, $Connection);
$Cache->append('some_string', 7, Item::TYPE_STRING);
unset($Cache);
$Cache = CacheFactory::createRedisCache('key_02', Cache::class, $Connection);
$Cache->append('some_string', 2);
var_dump($Cache->get('some_string')->getValue() == '72');
示例#3
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);
示例#4
0
 * @author alxmsl
 * @date 8/28/14
 */
include '../vendor/autoload.php';
use alxmsl\Primitives\Cache\Cache;
use alxmsl\Primitives\Cache\Exception\ExpiredException;
use alxmsl\Primitives\Cache\Exception\MissingException;
use alxmsl\Primitives\Cache\Item;
use alxmsl\Primitives\CacheFactory;
$Connection = new Memcached('cache');
$Connection->addServer('localhost', 11211);
$Cache = CacheFactory::createMemcachedCache('key_01', Cache::class, $Connection);
// 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::createMemcachedCache('key_01', Cache::class, $Connection);
var_dump($Cache->get('some_key')->getValue() == 7);
use alxmsl\Primitives\Cache\Exception\MissingException;
$Connection = new Memcached('cache');
$Connection->addServer('localhost', 11211);
$RootCache = CacheFactory::createMemcachedCache('key_03', Cache::class, $Connection);
$Level2Cache = CacheFactory::createMemcachedCache('key_03', Level2Cache::class, $Connection);
$Level3Cache = CacheFactory::createMemcachedCache('key_03', Level3Cache::class, $Connection);
// Leveled value write and read
$Level3Cache->set('some_level3_key', 5, Item::TYPE_NUMBER);
unset($Level3Cache);
$Level3Cache = CacheFactory::createMemcachedCache('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::createMemcachedCache('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);
}