<?phpnamespace illuminate\redis;use closure;use predis\client;use illuminate\support\ Arr;use illuminate\contracts\redis\database as databasecontract;// this is redis namespace , father is database contractclass database implements DatabaseContract{// class Database implements Database Contract /** * the host address of the database. * * @var array */ protected $clients;// the host address of the database. // host not hot , ok // host address is like 127.0.0.1 /** * Create a new redis connection instance. * * @param array $servers * @return void */ public function __construct (array $servers = [] ) { $cluster = arr::p ull ($ servers, ' cluster ');// cluster is lots of servers $options = (array) arr::p ull ($servers, ' options ');// pull servers // get options if ($cluster) {// if it is a cluster ,use first way to make it $this- >clients = $This->createaggregateclient ($servers, $options); } else {// other way $this->clients = $this->createsingleclients ($servers, $options); } }// Create a new Redis Connection instance /** * create a new aggregate client supporting sharding. * * @param array $servers * @param array $options * @return array */ protected function createaggregateclient (array $servers, array $options = []) { return [' default ' => new client (Array_values ($servers), $options)]; // set the with server and options }//Create a new aggregate client supporting sharding /** * Create an array of single connection clients. * * @param array $servers * @param array $options * @return array */ protected function Createsingleclients (array $servers, array $options = []) { $clients = [];// create Single Clients make clients foreach ($servers as $key => $server) {// a single client $ clients[$key] = new client ($server, $options); } return $clients; }// Make a single clients /** * get a specific Redis connection instance. * * @param string $name * @return \predis\clientinterface|null */ public function Connection ($name = ‘ Default ') { return arr::get ($this- >clients, $name ?: ' default '); }//get a specific redis connection instance. // Get a instance about you self want /** * run a command against the redis database. * * @param string $method * @param array $parameters * @return mixed */ public function command ($method, array $parameters = []) { return call_user_func_ Array ([$this->clients[' default '], $method], $parameters); }// run a command against the redis database // use this method call_ user_func_array /** * subscribe to a set of given channels for messages. * * @param array|string $channels * @ param \closure $callback * @param string $connection * @param string $method * @return void */ public function subscribe ($channels, closure $callback, $connection = null, $method = ' suBscribe ') { $loop = $this Connection ($connection)->pubsubloop ();// loop call_user _func_array ([$loop, $method], (array) $channels);// call user func array foreach ($loop as $message) { if ($message->kind === ' message ' | | $message->kind === ' pmessage ') { call_user_func ($callback, $message->payload, $message- >channel);// call_user_func } }// loop like message unset ($loop); }//subscribe /** * subscribe to a set of given channels with wildcards . * * @param array|string $channels * @param \Closure $callback * @param string $connection * @return void */ public function Psubscribe ($channels, closure $callback, $connection = null) { return $this->subscribe ($channels, $callback, $connection, __function__); }// subscribe to a set of given channels with wildcards /** * dynamically make a redis command. * * @param string $method * @param array $ parameters * @return mixed */ public function __call ($method, $parameters) { return $this->command ($method, $parameters); }// very good method,}
This article is from the "Focus on PHP" blog, please be sure to keep this source http://jingshanls.blog.51cto.com/3357095/1828664
[Li Jingshan php] every day laravel-20160921| Redis database.php