Redis 輔助工具類(laravel)

Laravel 獲取redis信息輔助類

 

1. redis 服務信息示例

2. redis 內存信息示例

3. redis cpu信息示例

4. db數量獲取示例

 

代碼示例如下:

<?php
namespace App\Utils;


/**
 * redis 服務信息
 * @author	田小濤
 * @date	2019年6月29日
 * @comment
 *
 */
class RedisServerUtils
{
    
    private $redis;
    private $config;
    private static $_instance;
    private function __construct()
    {
        $rhost = env( 'REDIS_HOST' );
        $rpass = env( 'REDIS_PASSWORD' );
        $rport = env( 'REDIS_PORT' );
        
        $config = array(
            'scheme' => '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;
    }
    
}

 

閒來無事整理整理, 歡迎大家補充指正,

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章