+ [refac] add method to get memory usage.

This commit is contained in:
liugang
2024-11-20 15:42:49 +08:00
committed by 朱金勇
parent ece01c01ae
commit b0e0090809
3 changed files with 48 additions and 0 deletions
+12
View File
@@ -889,4 +889,16 @@ class cache
{
$this->cache->clear();
}
/**
* 获取内存使用情况。
* Get memory usage.
*
* @param string $type
* @return string
*/
public function memory(string $type)
{
return $this->cache->memory($type);
}
}
+18
View File
@@ -81,4 +81,22 @@ class ApcuDriver implements CacheInterface
{
return (bool) apcu_exists($key);
}
/**
* 获取内存使用情况。
* Get memory usage.
*
* @param string $type
* @return string
*/
public function memory($type)
{
$info = apcu_sma_info(true);
if($type == 'total') return \helper::formatKB($info['seg_size']);
if($type == 'free') return \helper::formatKB($info['avail_mem']);
if($type == 'used') return \helper::formatKB($info['seg_size'] - $info['avail_mem']);
if($type == 'rate') return round(($info['seg_size'] - $info['avail_mem']) / $info['seg_size'] * 100, 2);
return '';
}
}
+18
View File
@@ -230,4 +230,22 @@ class RedisDriver implements CacheInterface
{
return (bool) $this->redis->exists($key);
}
/**
* 获取内存使用情况。
* Get memory usage.
*
* @param string $type
* @return string
*/
public function memory($type)
{
$info = $this->redis->info();
if($type == 'total') return $info['total_system_memory_human'];
if($type == 'free') return \helper::formatKB($info['total_system_memory'] - $info['used_memory']);
if($type == 'used') return $info['used_memory_human'];
if($type == 'rate') return round(($info['used_memory'] / $info['total_system_memory']) * 100, 2);
return '';
}
}