예제 #1
7
<?php

require 'vendor/autoload.php';
Predis\Autoloader::register();
$client = new Predis\Client(array('host' => '127.0.0.1', 'port' => 6379), array('prefix' => 'php:'));
$client->set("string:my_key", "Hello World");
$client->get("string:my_key");
# "Hello World"
$client->incr("string:counter");
$client->mget(array("string:my_key", "string:counter"));
# array('Hello World', '2')
$client->rpush("list:my_list", "item1", "item2");
$client->lpop("list:my_list");
# 'item1'
$client->hset("set:redis_book", "title", "Redis Essentials");
$client->hgetall("set:redis_book");
# array('title' => 'Redis Essentials')
$client->sadd("set:users", "alice", "bob");
$client->smembers("set:users");
# array('bob', 'alice')
$client->zadd("sorted_set:programmers", 1940, "Alan Kay");
$client->zadd("sorted_set:programmers", 1912, "Alan Turing");
$client->zrange("sorted_set:programmers", 0, -1, "withscores");
# array('Alan Turing' => 1912, 'Alan Kay' => 1940)
 public function testGetApc()
 {
     $redis = new \Predis\Client(array("scheme" => "tcp", "host" => 'localhost', "port" => 6379));
     $client = new LDClient("BOGUS_API_KEY", array('feature_requester_class' => '\\LaunchDarkly\\ApcLDDFeatureRequester', 'apc_expiration' => 1));
     $builder = new LDUserBuilder(3);
     $user = $builder->build();
     $redis->del("launchdarkly:features");
     $this->assertEquals("jim", $client->toggle('foo', $user, 'jim'));
     $redis->hset("launchdarkly:features", 'foo', $this->gen_feature("foo", "bar"));
     $this->assertEquals("bar", $client->toggle('foo', $user, 'jim'));
     # cached value so not updated
     $redis->hset("launchdarkly:features", 'foo', $this->gen_feature("foo", "baz"));
     $this->assertEquals("bar", $client->toggle('foo', $user, 'jim'));
     apc_delete("launchdarkly:features.foo");
     $this->assertEquals("baz", $client->toggle('foo', $user, 'jim'));
 }
예제 #3
0
        } else {
            if ($loggedin) {
                require './blog/wp-blog-header.php';
                $msg = 'not cached';
                // cache the page
            } else {
                // turn on output buffering
                ob_start();
                require './blog/wp-blog-header.php';
                // get contents of output buffer
                $html = ob_get_contents();
                // clean output buffer
                ob_end_clean();
                echo $html;
                // store html contents to redis cache
                $redis->hset($dkey, $ukey, $html);
                $msg = 'cache is set';
            }
        }
    }
}
$end = microtime();
// get end execution time
// show messages if debug is enabled
if ($debug) {
    echo $msg . ': ';
    echo t_exec($start, $end);
}
// time diff
function t_exec($start, $end)
{
//   - ZSCAN (iterates over members and ranks of a sorted set)
//   - HSCAN (iterates over fields and values of an hash).
// Predis provides a specialized abstraction for each command based on standard
// SPL iterators making it possible to easily consume SCAN-based iterations in
// your PHP code.
//
// See http://redis.io/commands/scan for more details.
//
// Create a client using `2.8` as a server profile (needs Redis 2.8!)
$client = new Predis\Client($single_server, array('profile' => '2.8'));
// Prepare some keys for our example
$client->del('predis:set', 'predis:zset', 'predis:hash');
for ($i = 0; $i < 5; $i++) {
    $client->sadd('predis:set', "member:{$i}");
    $client->zadd('predis:zset', -$i, "member:{$i}");
    $client->hset('predis:hash', "field:{$i}", "value:{$i}");
}
// === Keyspace iterator based on SCAN ===
echo 'Scan the keyspace matching only our prefixed keys:', PHP_EOL;
foreach (new Iterator\Keyspace($client, 'predis:*') as $key) {
    echo " - {$key}", PHP_EOL;
}
/* OUTPUT
Scan the keyspace matching only our prefixed keys:
 - predis:zset
 - predis:set
 - predis:hash
*/
// === Set iterator based on SSCAN ===
echo 'Scan members of `predis:set`:', PHP_EOL;
foreach (new Iterator\SetKey($client, 'predis:set') as $member) {
$start_time = time();
// Continue looping as long as we don't go past the time limit
while (time() < $start_time + $time_limit) {
    /* ... perorm BLPOP command ... */
    /* ... process jobs when received ... */
}
/* ... will quit once the time limit has been reached ... */
/*
 * Assigning Worker IDs & Monitoring
 * 
 * Usage: php worker.php 1
 */
// Gets the worker ID from the command line argument
$worker_id = $argv[1];
// Setting the Worker's Status
$predis->hset('worker.status', $worker_id, 'Started');
// Set the last time this worker checked in, use this to
// help determine when scripts die
$predis->hset('worker.status.last_time', $worker_id, time());
/* 
 * Using Versions to Check for Reloads
 */
$version = $predis->get('worker.version');
// i.e. number: 6
while (time() < $start_time + $time_limit) {
    /* ... check for jobs and process them ... */
    /* ... then, at the very end of the while ... */
    if ($predis->get('worker.version') != $version) {
        echo "New Version Detected... \n";
        echo "Reloading... \n";
        exit;
<?php

/*
 * Worker That will Process "Jobs"
 */
$worker_id = $argv[1];
if (!$worker_id) {
    $worker_id = rand(100, 999);
}
echo "Worker [{$worker_id}] Starting...\n";
require 'config.php';
echo "Connected to Redis Server \n";
$predis = new Predis\Client(array('scheme' => 'tcp', 'host' => REDIS_HOST, 'port' => REDIS_PORT));
// Setting the Worker's Status
$predis->hset('worker.status', $worker_id, 'Started');
$predis->hset('worker.status.last_time', $worker_id, time());
// Set the time limit for php to 0 seconds
set_time_limit(0);
/*
 * We'll set our base time, which is one hour (in seconds).
 * Once we have our base time, we'll add anywhere between 0 
 * to 10 minutes randomly, so all workers won't quick at the
 * same time.
 */
$time_limit = 60 * 60 * 1;
// Minimum of 1 hour
$time_limit += rand(0, 60 * 10);
// Adding additional time
// Set the start time
$start_time = time();
echo "\tWaiting for a Job .";