Redis 辅助工具类(laravel)

Laravel 获取redis信息辅助类

 

1. redis 服务信息示例

Redis 辅助工具类(laravel)_第1张图片

2. redis 内存信息示例

Redis 辅助工具类(laravel)_第2张图片

3. redis cpu信息示例

Redis 辅助工具类(laravel)_第3张图片

4. db数量获取示例

Redis 辅助工具类(laravel)_第4张图片

 

代码示例如下:

 'tcp',
            'host'   => $rhost,
            'port'   => $rport,
        );
        $redis = new \Predis\Client( $config );
        if( isset( $rpass ) && !is_null( $rpass ) )
        {
            $redis->auth( $rpass );
        }
        $this->config = $redis->info();
    }
    public static function getInstance()
    {
        if( null == self::$_instance )
        {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
    

    /**
     * redis Server
     * @author	 Administrator
     * @datetime 2019年6月29日  下午2:27:45
     * @comment	
     * 
     * @return array|string[]|NULL[]|mixed[]
     */
    public function server()
    {
        $arrRet = array();
        if( !isset( $this->config[ 'Server' ] ) || empty( $this->config[ 'Server' ] ) )
        {
            return $arrRet;
        }
        
        $arrRet[ 'version' ] = $this->config[ 'Server' ][ 'redis_version' ];
        $arrRet[ 'mode' ] = $this->config[ 'Server' ][ 'redis_mode' ];
        $arrRet[ 'os' ] = $this->config[ 'Server' ][ 'os' ] . '/' . $this->config[ 'Server' ][ 'arch_bits' ];
        $arrRet[ 'clients' ] = $this->config[ 'Clients' ][ 'connected_clients' ];
        $arrRet[ 'role' ] = null;
        if( isset( $this->config[ 'Replication' ][ 'role' ] ) && $this->config[ 'Replication' ][ 'role' ] )
        {
            $arrRet[ 'role' ] = $this->config[ 'Replication' ][ 'role' ];
        }
        
        return $arrRet;
    }
    
    
    /**
     * redis Memory
     * @author	 Administrator
     * @datetime 2019年6月29日  下午2:29:23
     * @comment	
     * 
     * @return array|NULL[]|mixed[]
     */
    public function memory()
    {
        $arrRet = array();
        if( !isset( $this->config[ 'Memory' ] ) || empty( $this->config[ 'Memory' ] ) )
        {
            return $arrRet;
        }
        
        $arrRet[ 'used' ] = $this->config[ 'Memory' ][ 'used_memory' ];
        $arrRet[ 'used_human' ] = $this->config[ 'Memory' ][ 'used_memory_human' ];
        $arrRet[ 'allocator' ] = $this->config[ 'Memory' ][ 'mem_allocator' ];
        
        return $arrRet;
    }
    
    
    /**
     * redis CPU
     * @author	 Administrator
     * @datetime 2019年6月29日  下午2:31:32
     * @comment	
     * 
     * @return array|NULL[]|mixed[]
     */
    public function cpu()
    {
        $arrRet = array();
        if( !isset( $this->config[ 'CPU' ] ) || empty( $this->config[ 'CPU' ] ) )
        {
            return $arrRet;
        }
        
        $arrRet[ 'used_cpu_sys' ] = $this->config[ 'CPU' ][ 'used_cpu_sys' ];
        $arrRet[ 'used_cpu_user' ] = $this->config[ 'CPU' ][ 'used_cpu_user' ];
        $arrRet[ 'used_cpu_sys_children' ] = $this->config[ 'CPU' ][ 'used_cpu_sys_children' ];
        
        return $arrRet;
    }
    
    
    /**
     * redis  Keyspace
     * @author	 Administrator
     * @datetime 2019年6月29日  下午2:32:23
     * @comment	
     * 
     * @return array|mixed[]
     */
    public function keyspace()
    {
        $arrRet = array();
        if( !isset( $this->config[ 'Keyspace' ] ) || empty( $this->config[ 'Keyspace' ] ) )
        {
            return $arrRet;
        }
        
        $arrSpace = array();
        foreach ( $this->config[ 'Keyspace' ] as $db=>$item )
        {
            $data[ 'keys' ] = $item[ 'keys' ];
            $data[ 'expires' ] = $item[ 'expires' ];
            $data[ 'ttl' ] = $item[ 'avg_ttl' ];
            
            $arrSpace[$db] = $data;
            unset( $item );
            unset( $data );
        }
        
        $arrRet[ 'space' ] = $arrSpace;
        unset( $arrSpace );
        
        return $arrRet;
    }
    
}

 

闲来无事整理整理, 欢迎大家补充指正,

你可能感兴趣的:(PHP,laravel,Redis)