当前位置: 首页>>代码示例>>PHP>>正文


PHP Redis类代码示例

本文整理汇总了PHP中Redis的典型用法代码示例。如果您正苦于以下问题:PHP Redis类的具体用法?PHP Redis怎么用?PHP Redis使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Redis类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: createClients

 /**
  * 创建 Redis.io client
  * @param  array  $servers
  * @param  array  $options
  * @return array
  * @throw exception
  */
 protected function createClients(array $servers, array $options = [])
 {
     $clients = [];
     try {
         foreach ($servers as $key_s => $server) {
             $redis = new \Redis();
             //长连接为pconnect,长连接要注意执行close关闭
             $func = Arr::get($server, 'persistent', false) ? 'pconnect' : 'connect';
             $redis->connect(Arr::get($server, 'host', ''), Arr::get($server, 'port'), $this->timeOut);
             //有配置密码的,进行auth操作
             if ($pwd = Arr::get($server, 'password', '')) {
                 $redis->auth($pwd);
             }
             $redis->select(Arr::get($server, 'database'));
             //设置redis的option,如Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE
             foreach ($options as $key => $val) {
                 $redis->setOption($key, $val);
             }
             $clients[$key_s] = $redis;
         }
     } catch (\Exception $e) {
         throw new \Exception("connect redis error:" . var_export($e->getMessage(), 1));
     }
     return $clients;
 }
开发者ID:TeamOfMalaysia,项目名称:TOM-BLOG,代码行数:32,代码来源:RedisService.php

示例2: _overview_all

 /**
  * 查看全部服务器
  */
 private function _overview_all()
 {
     $info = array();
     foreach ($this->server_list as $i => $server) {
         if (isset($server['cluster_list'])) {
             $info[$i]['error'] = FALSE;
             $info[$i]['cluster_list'] = $server['cluster_list'];
             continue;
         }
         $redis = new Redis();
         $can_connect = FALSE;
         try {
             $can_connect = $redis->connect($server['host'], $server['port'], 0.5);
         } catch (Exception $e) {
             $can_connect = TRUE;
         }
         $info[$i] = array('error' => FALSE);
         if (!$can_connect) {
             $info[$i]['error'] = TRUE;
         } else {
             $info[$i] = array_merge($info[$i], $redis->info());
             $info[$i]['size'] = $redis->dbSize();
         }
     }
     $page_data['info'] = $info;
     $page_data['server_list'] = $this->server_list;
     $page_data['title'] = '服务器一览表';
     $this->load->view('overview', $page_data);
 }
开发者ID:RyeZhu,项目名称:RedisMyAdmin,代码行数:32,代码来源:overview.php

示例3: getAdapter

 /**
  * {@inheritdoc}
  */
 public function getAdapter(array $config)
 {
     $client = new \Redis();
     $dsn = $this->getDsn();
     if (empty($dsn)) {
         if (false === $client->connect($config['host'], $config['port'])) {
             throw new ConnectException(sprintf('Could not connect to Redis database on "%s:%s".', $config['host'], $config['port']));
         }
     } else {
         if (false === $client->connect($dsn->getFirstHost(), $dsn->getFirstPort())) {
             throw new ConnectException(sprintf('Could not connect to Redis database on "%s:%s".', $dsn->getFirstHost(), $dsn->getFirstPort()));
         }
         if (!empty($dsn->getPassword())) {
             if (false === $client->auth($dsn->getPassword())) {
                 throw new ConnectException('Could not connect authenticate connection to Redis database.');
             }
         }
         if ($dsn->getDatabase() !== null) {
             if (false === $client->select($dsn->getDatabase())) {
                 throw new ConnectException(sprintf('Could not select Redis database with index "%s".', $dsn->getDatabase()));
             }
         }
     }
     $pool = new RedisCachePool($client);
     if (null !== $config['pool_namespace']) {
         $pool = new NamespacedCachePool($pool, $config['pool_namespace']);
     }
     return $pool;
 }
开发者ID:php-cache,项目名称:adapter-bundle,代码行数:32,代码来源:RedisFactory.php

示例4: wait

 /**
  * {@inheritdoc}
  */
 public function wait(Closure $callback)
 {
     while (true) {
         if (!$this->shouldProcessNext()) {
             break;
         }
         while (true) {
             $messageString = false;
             if ($this->redis instanceof \Predis\Client) {
                 $messageString = $this->redis->spop($this->key);
             }
             if ($this->redis instanceof \Redis) {
                 $messageString = $this->redis->sPop($this->key);
             }
             if (!$messageString) {
                 break;
             }
             $callback($this->serializer->unserialize($messageString));
             $this->incrementProcessedItems();
         }
         if ($this->refreshInterval) {
             sleep($this->refreshInterval);
         }
     }
 }
开发者ID:tomaj,项目名称:hermes,代码行数:28,代码来源:RedisSetDriver.php

示例5: __callStatic

 /**
  *
  * @param string $name
  * @param array $arguments
  * @return RedisFactory
  * @throws Exception
  */
 public static function __callStatic($name, $arguments)
 {
     switch ($name) {
         case 'get':
             list($redis_name, ) = $arguments ? $arguments : array('normal');
             if (!isset(self::$redis_list[$redis_name])) {
                 $redis_list = Application::$configs['redis'];
                 if (isset($redis_list[$redis_name])) {
                     try {
                         $redis_handle = new Redis();
                         $connected = $redis_handle->pconnect($redis_list[$redis_name]['host'], $redis_list[$redis_name]['port'], 30, sprintf('%s_%s_%s', $redis_list[$redis_name]['host'], $redis_list[$redis_name]['port'], $redis_list[$redis_name]['db']));
                         if (false == $connected) {
                             throw new Exception(sprintf('can\'t connect %s redis %s', $redis_name, json_encode($redis_list[$redis_name])));
                         }
                         $selected = $redis_handle->select((int) $redis_list[$redis_name]['db']);
                         if (false == $selected) {
                             throw new Exception(sprintf('connect %s redis %s select db failed', $redis_name, json_encode($redis_list[$redis_name])));
                         }
                         self::$redis_list[$redis_name] = new self($redis_handle);
                     } catch (RedisException $e) {
                         throw new Exception($e->getMessage());
                     }
                 } else {
                     throw new Exception('no config data key `' . $redis_name . '`');
                 }
             }
             return self::$redis_list[$redis_name];
             break;
             //其他case 省略
         //其他case 省略
         default:
             throw new Exception('RedisFactory unknown static method `' . $name . '`');
     }
 }
开发者ID:GitHubSi,项目名称:data-push,代码行数:41,代码来源:redis.php

示例6: get_redis

 /**
  * 获取redis对象
  *
  * @return Redis
  */
 public static function get_redis()
 {
     $redis_conf = $GLOBALS['config']['redis'];
     $redis = new Redis();
     $res = $redis->connect($redis_conf['host'], $redis_conf['port']);
     return $redis;
 }
开发者ID:codergma,项目名称:myspider2,代码行数:12,代码来源:CG_Redis.php

示例7: cache

 protected function cache()
 {
     $redis = new Redis();
     $redis->connect('127.0.0.1', 6379);
     $redis->auth('123456');
     return $redis;
 }
开发者ID:kl0428,项目名称:urtime,代码行数:7,代码来源:Controller.php

示例8: testIfCacheInitsWithOpenConnection

 public function testIfCacheInitsWithOpenConnection()
 {
     $redis = new \Redis();
     $redis->connect(REDIS_HOST, REDIS_PORT, REDIS_TIMEOUT_SECONDS);
     $this->cache = new RedisCache($redis);
     $this->assertKeysCanBeWrittenAndRead();
 }
开发者ID:mcustiel,项目名称:php-simple-cache,代码行数:7,代码来源:RedisCacheFunctionalTest.php

示例9: testGetMessageWithNoExistsMessage

 /**
  * Get message with message not found
  */
 public function testGetMessageWithNoExistsMessage()
 {
     $this->redis->expects($this->once())->method('lPop')->with('foo.bar')->will($this->returnValue(false));
     $adapter = new RedisAdapter();
     $adapter->setRedis($this->redis)->setListKey('foo.bar');
     $this->assertNull($adapter->getMessage());
 }
开发者ID:integer,项目名称:AppleApnPush,代码行数:10,代码来源:RedisTest.php

示例10: getCacheAction

 public function getCacheAction()
 {
     $redis = new Redis();
     $redis->connect('127.0.0.1');
     $page = $redis->get('page');
     print_r($page);
 }
开发者ID:sunrain520,项目名称:yaf_base,代码行数:7,代码来源:index.php

示例11: class_init

 public static function class_init()
 {
     /** @var Settings $settings */
     $settings = resource(Settings::class);
     self::$redis = new \Redis();
     self::$redis->connect($settings->get("redis", "hostname"), $settings->get("redis", "connect_port"));
 }
开发者ID:pldin601,项目名称:HomeMusic,代码行数:7,代码来源:RedisBackend.php

示例12: o_indirect_tsf

/**
*@author: JJyy
*@todo: get the indirect transform number for the user_id 
*		less than 30 days
*@param: 
*
**/
function o_indirect_tsf($user_id, $channel_name, $timeset, $db)
{
    $sql = "select count(order_id) as cn from order_info where order_status=1 and pay_status=2 and confirm_time>" . $timeset . " and confirm_time<" . ($timeset + 3600 * 30);
    $row = $db->query($sql);
    $cn = $row['cn'];
    if ($cn == 0) {
        //if within 30 days no order
        return 0;
    } else {
        $rds = new Redis();
        //get the keys for the user_id
        $keys = $rds->keys("*:" . $user_id);
        //string like: li9t209jm7mc6m4vmn88o5a7j0:1454035403.8093:10.10.10.29:baidu:0
        $num = 0;
        foreach ($keys as $k => $v) {
            //the time must after timeset
            //same user_id and different channel_name and time after than timeset
            if ($v[1] > $timeset && $v[3] != $channel_name && $v[4] == $user_id) {
                $sql = "select count(order_id) as cn from order_info where order_status=1 and pay_status=2 and confirm_time>" . $timeset . " and confirm_time<" . ($timeset + 3600 * 2);
                $row = $db->query($sql);
                $cn = $row['cn'];
                $num += $cn;
            }
        }
        return $num;
    }
}
开发者ID:r00tjimmy,项目名称:golang-website_data_statis,代码行数:34,代码来源:transform_order.php

示例13: testBasics

 public function testBasics()
 {
     if (extension_loaded('Redis')) {
         $redis = new \Redis();
         try {
             $ok = @$redis->connect('127.0.0.1', 6379);
         } catch (\Exception $e) {
             $ok = false;
         }
         if (!$ok) {
             $this->markTestSkipped('The ' . __CLASS__ . ' cannot connect to redis');
         }
     } else {
         $this->markTestSkipped('The ' . __CLASS__ . ' requires the use of redis');
     }
     $cache = new RedisCache();
     $cache->setRedis($redis);
     // Test save
     $cache->save('test_key', 'testing this out');
     // Test contains to test that save() worked
     $this->assertTrue($cache->contains('test_key'));
     $cache->save('test_key1', 'testing this out', 20);
     // Test contains to test that save() worked
     $this->assertTrue($cache->contains('test_key1'));
     // Test fetch
     $this->assertEquals('testing this out', $cache->fetch('test_key'));
     // Test delete
     $cache->save('test_key2', 'test2');
     $cache->delete('test_key2');
     $this->assertFalse($cache->contains('test_key2'));
     $this->assertEquals($redis, $cache->getRedis());
 }
开发者ID:nlegoff,项目名称:Phraseanet,代码行数:32,代码来源:RedisCacheTest.php

示例14: close

 /**
  * Отключение от Redis.
  * 
  * @return void
  * @access public
  * @static
  */
 public static function close()
 {
     if (static::$redis) {
         static::$redis->close();
         static::$redis = null;
     }
 }
开发者ID:Ganzal,项目名称:php-pinger-service,代码行数:14,代码来源:RKS.php

示例15: createRedis

 public function createRedis()
 {
     $redis = new \Redis();
     $redis->connect($this->getOption('host'), $this->getOption('port'));
     $redis->setOption(\Redis::OPT_PREFIX, $this->getOption('prefix'));
     return $redis;
 }
开发者ID:rybakit,项目名称:phive-queue,代码行数:7,代码来源:RedisHandler.php


注:本文中的Redis类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。