diff --git a/lib/cache/cache.class.php b/lib/cache/cache.class.php index 03f1c17d70..841debf0d9 100644 --- a/lib/cache/cache.class.php +++ b/lib/cache/cache.class.php @@ -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); + } } diff --git a/lib/cache/driver/ApcuDriver.php b/lib/cache/driver/ApcuDriver.php index 7adeda2b41..6e712c16ec 100644 --- a/lib/cache/driver/ApcuDriver.php +++ b/lib/cache/driver/ApcuDriver.php @@ -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 ''; + } } diff --git a/lib/cache/driver/RedisDriver.php b/lib/cache/driver/RedisDriver.php index 1297c71959..ecb6cdf776 100644 --- a/lib/cache/driver/RedisDriver.php +++ b/lib/cache/driver/RedisDriver.php @@ -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 ''; + } }