예제 #1
0
<?php

require __DIR__ . '/vendor/autoload.php';
try {
    $client = new Predis\Client();
    echo $client->ping();
} catch (\Exception $ex) {
    header('HTTP/1.1 503 Service Unavailable');
    echo $ex->getMessage();
}
 /**
  * Instantiate the Redis class.
  *
  * Instantiates the Redis class.
  *
  * @param null $persistent_id To create an instance that persists between requests, use persistent_id to specify a unique ID for the instance.
  */
 public function __construct()
 {
     global $blog_id, $table_prefix;
     $redis = array('scheme' => 'tcp', 'host' => '127.0.0.1', 'port' => 6379);
     if (defined('WP_REDIS_SCHEME')) {
         $redis['scheme'] = WP_REDIS_SCHEME;
     }
     if (defined('WP_REDIS_HOST')) {
         $redis['host'] = WP_REDIS_HOST;
     }
     if (defined('WP_REDIS_PORT')) {
         $redis['port'] = WP_REDIS_PORT;
     }
     if (defined('WP_REDIS_PATH')) {
         $redis['path'] = WP_REDIS_PATH;
     }
     if (defined('WP_REDIS_PASSWORD')) {
         $redis['password'] = WP_REDIS_PASSWORD;
     }
     if (defined('WP_REDIS_DATABASE')) {
         $redis['database'] = WP_REDIS_DATABASE;
     }
     $redis_client = defined('WP_REDIS_CLIENT') ? WP_REDIS_CLIENT : null;
     if (class_exists('Redis') && strcasecmp('predis', $redis_client) !== 0) {
         $redis_client = defined('HHVM_VERSION') ? 'hhvm' : 'pecl';
     } else {
         $redis_client = 'predis';
     }
     try {
         if (strcasecmp('hhvm', $redis_client) === 0) {
             $this->redis_client = sprintf('HHVM %s Extension', HHVM_VERSION);
             $this->redis = new Redis();
             // adjust host and port, if the scheme is `unix`
             if (strcasecmp('unix', $redis['scheme']) === 0) {
                 $redis['host'] = 'unix://' . $redis['path'];
                 $redis['port'] = 0;
             }
             $this->redis->connect($redis['host'], $redis['port']);
             if (isset($redis['password'])) {
                 $this->redis->auth($redis['password']);
             }
             if (isset($redis['database'])) {
                 $this->redis->select($redis['database']);
             }
         } elseif (strcasecmp('pecl', $redis_client) === 0) {
             $this->redis_client = 'PCEL Extension';
             $this->redis = new Redis();
             if (strcasecmp('unix', $redis['scheme']) === 0) {
                 $this->redis->connect($redis['path']);
             } else {
                 $this->redis->connect($redis['host'], $redis['port']);
             }
             if (isset($redis['password'])) {
                 $this->redis->auth($redis['password']);
             }
             if (isset($redis['database'])) {
                 $this->redis->select($redis['database']);
             }
         } else {
             $this->redis_client = 'Predis';
             // require PHP 5.4 or greater
             if (version_compare(PHP_VERSION, '5.4.0', '<')) {
                 throw new Exception();
             }
             // check if bundled Predis library exists
             if (!realpath(WP_CONTENT_DIR . '/plugins/redis-cache/includes/predis.php')) {
                 throw new Exception();
             }
             require_once WP_CONTENT_DIR . '/plugins/redis-cache/includes/predis.php';
             Predis\Autoloader::register();
             $this->redis = new Predis\Client($redis);
             $this->redis->connect();
             $this->redis_client .= ' v' . Predis\Client::VERSION;
         }
         /* Test the connection before setting connected flag */
         $this->redis->ping();
         $this->redis_connected = true;
     } catch (Exception $exception) {
         // When Redis is unavailable, fall back to the internal back by forcing all groups to be "no redis" groups
         $this->no_redis_groups = array_unique(array_merge($this->no_redis_groups, $this->global_groups));
         $this->redis_connected = false;
     }
     /**
      * This approach is borrowed from Sivel and Boren. Use the salt for easy cache invalidation and for
      * multi single WP installs on the same server.
      */
     if (!defined('WP_CACHE_KEY_SALT')) {
         define('WP_CACHE_KEY_SALT', '');
     }
     // Assign global and blog prefixes for use with keys
     if (function_exists('is_multisite')) {
         $this->global_prefix = is_multisite() || defined('CUSTOM_USER_TABLE') && defined('CUSTOM_USER_META_TABLE') ? '' : $table_prefix;
         $this->blog_prefix = (is_multisite() ? $blog_id : $table_prefix) . ':';
     }
 }
예제 #3
0
$view = $app->view();
$view->parserExtensions = array(new \Slim\Views\TwigExtension());
$redis = new Predis\Client(array("scheme" => "tcp", "host" => "127.0.0.1", "port" => 6379));
$app->get('/hello/:name', function ($name) use($app) {
    $logPath = '/tmp/mono.log';
    $logger = new Logger('foo_test');
    $logger->pushHandler(new StreamHandler($logPath, Logger::DEBUG));
    // $logger->info()
    $logger->addInfo('info_bar');
    // $logger->notice()
    $logger->addNotice('notice_bar');
    // $logger->warning(), $logger->warn()
    $logger->addWarning('warning_bar');
    // $logger->error(), $logger->err()
    $logger->addError('error_bar');
    // $logger->critical(), $logger->crit()
    $logger->addCritical('critical_bar');
    // $logger->alert()
    $logger->addAlert('alert_bar');
    // $logger->emergency(), $logger->emerg()
    $logger->addEmergency('emergency_bar');
    $app->render('index.html', array('name' => $name));
});
$app->get('/redis', function () use($redis) {
    // PING
    echo $redis->ping();
    $redis->set('key', 'value');
    $value = $redis->get('key');
    echo "key: " . $value;
});
$app->run();