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;
}
}
闲来无事整理整理, 欢迎大家补充指正,