예제 #1
0
/**
 * Safe update json in Redis storage
 * @param Redis $Redis
 * @param string $key
 * @param array $array
 * @throws Exception
 */
function updateJsonInRedis(RedisClient $Redis, $key, array $array)
{
    // Create new Lock instance
    $Lock = new RedisLock($Redis, 'Lock_' . $key, RedisLock::FLAG_CATCH_EXCEPTIONS);
    // Acquire lock for 2 sec.
    // If lock has acquired in another thread then we will wait 3 second,
    // until another thread release the lock. Otherwise it throws a exception.
    if (!$Lock->acquire(2, 3)) {
        throw new Exception('Can\'t get a Lock');
    }
    // Get value from storage
    $json = $Redis->get($key);
    if (!$json) {
        $jsonArray = [];
    } else {
        $jsonArray = json_decode($json, true);
    }
    // Some operations with json
    $jsonArray = array_merge($jsonArray, $array);
    $json = json_encode($jsonArray);
    // Update key in storage
    $Redis->set($key, $json);
    // Release the lock
    // After $lock->release() another waiting thread (Lock) will be able to update json in storage
    $Lock->release();
}
예제 #2
0
 public function test_RedisLock_Exceptions()
 {
     $key = static::TEST_KEY;
     $RedisLock = new RedisLock(static::$Redis, $key);
     $this->assertSame(true, $RedisLock->acquire(2));
     $this->assertSame(true, $RedisLock->isLocked());
     static::$Redis->del($key);
     try {
         $RedisLock->release();
         $this->assertFalse('Expect LostLockException');
     } catch (\Exception $Ex) {
         $this->assertInstanceOf(LostLockException::class, $Ex);
     }
     $this->assertSame(false, $RedisLock->isLocked());
     $this->assertSame(true, $RedisLock->acquire(2));
     $this->assertSame(true, $RedisLock->isLocked());
     static::$Redis->del($key);
     $this->assertSame(false, $RedisLock->isExists());
     try {
         $RedisLock->isLocked();
         $this->assertFalse('Expect LostLockException');
     } catch (\Exception $Ex) {
         $this->assertInstanceOf(LostLockException::class, $Ex);
     }
 }
 * Create a new instance of RedisClient
 */
namespace Examples;

require dirname(__DIR__) . '/vendor/autoload.php';
// or require (dirname(__DIR__).'/src/autoloader.php');
use RedisClient\RedisClient;
use RedisClient\Client\Version\RedisClient2x6;
use RedisClient\ClientFactory;
// Example 1. Create new Instance with default config
$Redis = new RedisClient();
// By default, the client works with the latest stable version of Redis.
echo 'RedisClient: ' . $Redis->getSupportedVersion() . PHP_EOL;
echo 'Redis: ' . $Redis->info('Server')['redis_version'] . PHP_EOL;
// RedisClient: 3.2
// Redis: 3.0.3
// Example 2. Create new Instance with config
// By default, the client works with the latest stable version of Redis.
$Redis = new RedisClient(['server' => 'tcp://127.0.0.1:6387', 'timeout' => 2]);
echo 'RedisClient: ' . $Redis->getSupportedVersion() . PHP_EOL;
echo 'Redis: ' . $Redis->info('Server')['redis_version'] . PHP_EOL;
// RedisClient: 3.2
// Redis: 3.2.0
// Example 3. Create new Instance for Redis version 2.6.x with config
$Redis = new RedisClient2x6(['server' => 'tcp://127.0.0.1:6379', 'timeout' => 2]);
echo 'RedisClient: ' . $Redis->getSupportedVersion() . PHP_EOL;
// RedisClient: 2.6
// Example 4. Create new Instance for Redis version 2.8.x with config via factory
$Redis = ClientFactory::create(['server' => 'tcp://127.0.0.1:6379', 'timeout' => 2, 'version' => '2.8.24']);
echo 'RedisClient: ' . $Redis->getSupportedVersion() . PHP_EOL;
// RedisClient: 2.8
예제 #4
0
 * git: https://github.com/cheprasov/php-redis-client
 *
 * (C) Alexander Cheprasov <*****@*****.**>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
/**
 * Monitor
 */
namespace Examples;

require dirname(__DIR__) . '/vendor/autoload.php';
// or require (dirname(__DIR__).'/src/autoloader.php');
use RedisClient\RedisClient;
// Example 1. monitor with timeout
$Redis = new RedisClient(['timeout' => 10]);
$Redis->monitor(function ($message) {
    // This function will be called on message and on timeout
    if (!isset($message)) {
        echo 'No any message for 10 second... exit' . PHP_EOL;
        // return <false> for stop monitoring and exit
        return false;
    }
    echo $message, PHP_EOL;
    // return <true> for to wait next message
    return true;
});
// NOTE! You can not use $Redis after monitor,
// because connection with redis will be closed,
// it is correct way to stop monitor.
예제 #5
0
 * (C) Alexander Cheprasov <*****@*****.**>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
/**
 * Transactions
 */
namespace Examples;

require dirname(__DIR__) . '/vendor/autoload.php';
// or require (dirname(__DIR__).'/src/autoloader.php');
use RedisClient\Pipeline\Pipeline;
use RedisClient\Pipeline\PipelineInterface;
use RedisClient\RedisClient;
$Redis = new RedisClient();
// Example 1. Transactions
$Redis->watch(['foo', 'bar']);
$Redis->multi();
$Redis->set('foo', 'bar');
$Redis->set('bar', 'foo');
$result = $Redis->exec();
var_dump($result);
//    array(2) {
//        [0]=> bool(true)
//        [1]=> bool(true)
//    }
// Example 2. True way to use transactions via Pipeline
$result = $Redis->pipeline(function (PipelineInterface $Pipeline) {
    /** @var Pipeline $Pipeline */
    $Pipeline->watch(['foo', 'bar']);
예제 #6
0
 /**
  * @inheritdoc
  */
 public function isExists()
 {
     return $this->Redis->get($this->key) ? true : false;
 }
예제 #7
0
 * (C) Alexander Cheprasov <*****@*****.**>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
/**
 * Pipeline
 */
namespace Examples;

require dirname(__DIR__) . '/vendor/autoload.php';
// or require (dirname(__DIR__).'/src/autoloader.php');
use RedisClient\Pipeline\Pipeline;
use RedisClient\Pipeline\PipelineInterface;
use RedisClient\RedisClient;
$Redis = new RedisClient();
// Example 1. via new Pipeline
// Method 'pipeline' without params returns new Pipeline object;
$Pipeline = $Redis->pipeline();
// or $Pipeline = new Pipeline();
$Pipeline->set('foo', 'bar')->get('foo')->flushall();
var_dump($Redis->pipeline($Pipeline));
// result:
//    array(3) {
//        [0]=> bool(true)
//        [1]=> string(3) "bar"
//        [2]=> bool(true)
//    }
// Example 2. via Closure
// Method 'pipeline' without params returns new Pipeline object;
$result = $Redis->pipeline(function (PipelineInterface $Pipeline) {
예제 #8
0
        }
    }
    // This function will be called on subscribe and on message
    if ($type === 'subscribe') {
        // Note, if $type === 'subscribe'
        // then $channel = <channel-name>
        // and $message = <count of subsribers>
        echo 'Subscribed to channel <', $channel, '>', PHP_EOL;
    } elseif ($type === 'message') {
        echo 'Message <', $message, '> from channel <', $channel, '>', PHP_EOL;
    }
    // return <true> for to wait next message
    return true;
});
// Example 4. psubscribe
$Redis = new RedisClient(['timeout' => 0]);
$Redis->psubscribe('channel.*', function ($type, $pattern, $channel, $message) {
    // This function will be called on subscribe and on message
    if ($type === 'psubscribe') {
        // Note, if $type === 'psubscribe'
        // then $pattern = <channel-name>
        // and $channel = <count of subsribers>
        echo 'Subscribed to channel <', $pattern, '>', PHP_EOL;
    } elseif ($type === 'pmessage') {
        echo 'Message <', $message, '> from channel <', $channel, '> by pattern <', $pattern, '>', PHP_EOL;
        if ($message === 'quit') {
            // return <false> for unsubscribe and exit
            return false;
        }
    }
    // return <true> for to wait next message
예제 #9
0
 * This file is part of RedisClient.
 * git: https://github.com/cheprasov/php-redis-client
 *
 * (C) Alexander Cheprasov <*****@*****.**>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
/**
 * RAW commands
 */
namespace Examples;

require dirname(__DIR__) . '/vendor/autoload.php';
// or require (dirname(__DIR__).'/src/autoloader.php');
use RedisClient\RedisClient;
$Redis = new RedisClient();
// Example 1. As string[] by <executeRaw>
// Every part of command must be a separate string
// <executeRaw> is better way to use raw commands than <executeRawString>
$Redis->executeRaw(['SET', 'foo', 'bar']);
echo 'result: ' . $Redis->executeRaw(['GET', 'foo']) . PHP_EOL;
// bar
// Example s. As string by <executeRawString>
$Redis->executeRawString('SET foo bar');
echo 'result: ' . $Redis->executeRawString('GET foo') . PHP_EOL;
// bar
// You can use quotes for keys and arguments
$Redis->executeRawString('SET "key with spaces" "or value with spaces"');
echo 'result: ' . $Redis->executeRawString('GET "key with spaces"') . PHP_EOL;
// or value with spaces