Redis 辅助工具类(laravel)

10 篇文章 0 订阅
2 篇文章 0 订阅

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;
    }
    
}

 

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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值