[李景山php]每天laravel-20160921|Redis Database.php

<?php

namespace 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 Contract
class 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::pull($servers, ‘cluster‘);// cluster is lots of servers

        $options = (array) Arr::pull($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,
}
时间: 2024-07-31 03:01:06

[李景山php]每天laravel-20160921|Redis Database.php的相关文章

laravel 操作 redis

laravel框架中本身已经存在相应的redis的配置我们在使用的时候只需要更改配置即可,但是在使用的时候一定要注意命名空间的问题,具体可查看config/app.php下面的aliases数组中具体的路径 1.安装启动Redis及依赖包 如果使用的是Homestead虚拟机作为本地开发环境的话,Homestead已经为我们安装好了Redis并开机启动,如果是使用Windows开发环境的话,可参考 这篇文章 安装启动Redis. Redis启动之后,还需要在Laravel项目根目录下运行如下命令

laravel中redis的配置和使用

引入redis composer require predis/predis 会在composer.json中引入最新版本的predis composer update 把下载predis 库加入到vendor,命令执行成功后,如图: 配置redis 说到laravel 中redis 的配置,其实默认项目中已经有了相关配置,只是默认没有使用.默认使用的是: 项目 使用类型 CACHE_DRIVER file SESSION_DRIVER file 添加redis数据库使用 'redis' =>

Redis Persistence 之 redis database

1.关于redis持久化问题,看看官网文档: 注:redis提供了多种不同方式的持久化选项: RDB(即 redis database)持久化表现在特定的时间间隔内某一个时间点的快照.可以理解为,在指定时间间隔内将内存中的数据集快照写入磁盘,也就是常说的snapshot快照,它恢复时是将快照文件直接读入内存中. AOF持久性地记录在服务器中的所有写操作命令,并且在服务器重新启动时执行这些操作命令.(关于AOF会在下一篇博文中介绍.) 补充说明:(redis设置连接数据库密码的操作命令) 注:使用

laravel中redis队列的使用

一.配置文件 首先我们需要在配置文件中配置默认队列驱动为Redis,队列配置文件是config/queue.php: return [ 'default' => env('QUEUE_DRIVER', 'sync'), 'connections' => [ 'sync' => [ 'driver' => 'sync', ], 'database' => [ 'driver' => 'database', 'table' => 'jobs', 'queue' =&g

laravel session redis 设置

Laravel 在使用 Redis 作为 Session 驱动之前, 需要通过 Composer 安装 predis/predis 扩展包 (~1.0). 当然也可以用原生自带的,具体使用见 https://laravel-china.org/docs/laravel/5.6/redis/1402#phpredis 操作即可. 然后在database 配置文件中配置 Redis 连接信息. 在 session 配置文件中,connection 选项可用于指定 Session 使用哪个 Redis

Laravel中Redis的使用

安装 laravel中使用redis首先需要你通过 Composer 安装 predis/predis 包: composer require predis/predis 配置 redis的配置文件是:config/database.php 'redis' => [ 'client' => 'predis', 'default' => [ 'host' => env('REDIS_HOST', '127.0.0.1'), 'password' => env('REDIS_PA

[原创]Laravel 基于redis队列的解析

目录 参考链接 本文环境 为什么使用队列 Laravel 中的队列 分发任务 任务队列 Worker Last-Modified: 2019年5月10日11:44:18 参考链接 使用 Laravel Queue 不得不明白的知识 Laravel 队列文档 本文环境 Laravel 5.5 队列 Redis 为什么使用队列 使用队列的目的一般是: 异步执行 出错重试 解释一下: 异步执行: 部分代码执行很耗时, 为了提高响应速度及避免占用过多连接资源, 可以将这部分代码放到队列中异步执行. Eg

[李景山php]每天laravel-20160922|RedisServicProvider.php

<?php namespace Illuminate\Redis; use Illuminate\Support\ServiceProvider; // this namespace class RedisServiceProvider extends ServiceProvider {     /**      * Indicates if loading of the provider is deferred.      *      * @var bool      */     prot

laravel中redis个方法的使用

在laravel中使用redis自带方法的时候会发现许多原生的方法都不存在了,laravel对其进行了重新的封装但是在文档中并没有找到相关的资料最后在 \vendor\predis\predis\src\Profile\RedisProfile.php 该文件的createCommand方法重打印出 $this->commands 发现许多方法名是被重写的,以下为所有重新定义的方法名 array(151) { ["EXISTS"]=> string(24) "Pr