CrossPHP
  • Namespace
  • Class
  • Download

Namespaces

  • Cross
    • Auth
    • Cache
      • Driver
      • Request
    • Core
    • DB
      • Connecter
      • Drivers
      • SQLAssembler
    • Exception
    • Http
    • I
    • Lib
      • Document
    • Module
    • MVC
    • Runtime
  • None

Classes

  • Cross\Auth\CookieAuth
  • Cross\Auth\SessionAuth
  • Cross\Cache\Driver\FileCacheDriver
  • Cross\Cache\Driver\MemcacheDriver
  • Cross\Cache\Driver\RedisDriver
  • Cross\Cache\Request\FileCache
  • Cross\Cache\Request\Memcache
  • Cross\Cache\Request\RedisCache
  • Cross\Cache\RequestCache
  • Cross\Core\Annotate
  • Cross\Core\Application
  • Cross\Core\ArrayMap
  • Cross\Core\Config
  • Cross\Core\CrossArray
  • Cross\Core\Delegate
  • Cross\Core\FrameBase
  • Cross\Core\Helper
  • Cross\Core\HttpAuth
  • Cross\Core\Loader
  • Cross\Core\Rest
  • Cross\Core\Router
  • Cross\DB\Connecter\BaseConnecter
  • Cross\DB\Connecter\MySQLConnecter
  • Cross\DB\Connecter\PgSQLConnecter
  • Cross\DB\Connecter\SQLiteConnecter
  • Cross\DB\DBFactory
  • Cross\DB\Drivers\CouchDriver
  • Cross\DB\Drivers\MongoDriver
  • Cross\DB\Drivers\PDOSqlDriver
  • Cross\DB\SQLAssembler\MySQLAssembler
  • Cross\DB\SQLAssembler\PgSQLAssembler
  • Cross\DB\SQLAssembler\SQLAssembler
  • Cross\DB\SQLAssembler\SQLiteAssembler
  • Cross\Http\Request
  • Cross\Http\Response
  • Cross\Lib\Array2XML
  • Cross\Lib\Document\CallTree
  • Cross\Lib\Document\CallTreeToHTML
  • Cross\Lib\Document\HTML
  • Cross\Lib\StringToPHPStream
  • Cross\Module\SQLModule
  • Cross\MVC\Controller
  • Cross\MVC\Module
  • Cross\MVC\View
  • Cross\Runtime\ClosureContainer

Interfaces

  • Cross\I\CacheInterface
  • Cross\I\HttpAuthInterface
  • Cross\I\PDOConnecter
  • Cross\I\RequestCacheInterface
  • Cross\I\RouterInterface
  • Cross\I\SqlInterface

Exceptions

  • Cross\Exception\CacheException
  • Cross\Exception\CoreException
  • Cross\Exception\CrossException
  • Cross\Exception\FrontException

Functions

  • ascLogo
  • line
  • tBody
  • th
  • tHead
  1 <?php
  2 /**
  3  * Cross - a micro PHP 5 framework
  4  *
  5  * @link        http://www.crossphp.com
  6  * @license     MIT License
  7  */
  8 
  9 namespace Cross\Cache\Driver;
 10 
 11 use Cross\Exception\CoreException;
 12 use Redis;
 13 
 14 /**
 15  * @author wonli <wonli@live.com>
 16  * Class RedisDriver
 17  * @package Cross\Cache\Driver
 18  */
 19 class RedisDriver
 20 {
 21     /**
 22      * @var string
 23      */
 24     private $id;
 25 
 26     /**
 27      * @var Redis
 28      */
 29     protected $link;
 30 
 31     /**
 32      * @var array
 33      */
 34     protected $option;
 35 
 36     /**
 37      * 连接redis
 38      * <pre>
 39      * unixsocket设置
 40      * unixsocket /tmp/redis.sock
 41      * unixsocketperm 777
 42      * </pre>
 43      *
 44      * @param $option
 45      * @throws \cross\exception\CoreException
 46      */
 47     function __construct(array $option)
 48     {
 49         if (!extension_loaded('redis')) {
 50             throw new CoreException('Not support redis extension !');
 51         }
 52 
 53         if (!isset($option['host'])) {
 54             $option['host'] = '127.0.0.1';
 55         }
 56 
 57         if (!isset($option['port'])) {
 58             $option['port'] = 6379;
 59         }
 60 
 61         if (!isset($option['timeout'])) {
 62             $option['timeout'] = 3;
 63         }
 64 
 65         if (strcasecmp(PHP_OS, 'linux') == 0 && !empty($option['unix_socket'])) {
 66             $id = $option['unix_socket'];
 67             $use_unix_socket = true;
 68         } else {
 69             $id = "{$option['host']}:{$option['port']}:{$option['timeout']}";
 70             $use_unix_socket = false;
 71         }
 72 
 73         static $connects;
 74         if (!isset($connects[$id])) {
 75             $redis = new Redis();
 76             if ($use_unix_socket) {
 77                 $redis->connect($option['unix_socket']);
 78             } else {
 79                 $redis->connect($option['host'], $option['port'], $option['timeout']);
 80             }
 81 
 82             if (!empty($option['pass'])) {
 83                 $authStatus = $redis->auth($option['pass']);
 84                 if (!$authStatus) {
 85                     throw new CoreException('Redis auth failed !');
 86                 }
 87             }
 88 
 89             $connects[$id] = $redis;
 90         } else {
 91             $redis = &$connects[$id];
 92         }
 93 
 94         $this->id = $id;
 95         $this->link = $redis;
 96         $this->option = $option;
 97     }
 98 
 99     /**
100      * 获取连接属性
101      *
102      * @return array
103      */
104     function getLinkOption()
105     {
106         return $this->option;
107     }
108 
109     /**
110      * 调用redis类提供的方法
111      *
112      * @param $method
113      * @param $argv
114      * @return mixed|null
115      * @throws CoreException
116      */
117     public function __call($method, $argv)
118     {
119         $result = null;
120         if (method_exists($this->link, $method)) {
121             $this->selectCurrentDatabase();
122             $result = ($argv == null)
123                 ? $this->link->$method()
124                 : call_user_func_array(array($this->link, $method), $argv);
125         }
126 
127         return $result;
128     }
129 
130     /**
131      * 选择当前数据库
132      *
133      * @throws CoreException
134      */
135     protected function selectCurrentDatabase()
136     {
137         static $selected = null;
138 
139         $db = &$this->option['db'];
140         $current = $this->id . ':' . $db;
141         if ($selected !== $current) {
142             $select_ret = $this->link->select($db);
143             if ($select_ret) {
144                 $selected = $current;
145             } else {
146                 throw new CoreException("Redis select DB($current) failed!");
147             }
148         }
149     }
150 }
151 
CrossPHP API documentation generated by ApiGen