Skip to content

Commit acb3990

Browse files
committed
ADDED setCacheStatus() which permits to disable and enable the cache & getCacheStatus() which permits to get the status (true or false) of the cache (Fixes #2)
1 parent c8250df commit acb3990

File tree

3 files changed

+131
-21
lines changed

3 files changed

+131
-21
lines changed

PHPGlances/PHPGlances.php

+123-18
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class PHPGlances{
66
private $_url;
77
private $_port = 80;
88
private $_error = '';
9+
private $_useCache = false;
10+
private $_arrCache = array();
911

1012
private $_oCurl;
1113
private $_extPHPCurl;
@@ -14,13 +16,12 @@ class PHPGlances{
1416
private $_extPHPSimpleXML;
1517

1618
public function __construct($psURL, $piPort){
17-
$this->_url = $psURL;
18-
$this->_port = $piPort;
19+
$this->_url = $psURL;
20+
$this->_port = $piPort;
1921

20-
21-
$this->_extPHPCurl = extension_loaded('curl');
22-
$this->_extPHPJson = extension_loaded('json');
23-
$this->_extPHPXMLRPC = extension_loaded('xmlrpc');
22+
$this->_extPHPCurl = extension_loaded('curl');
23+
$this->_extPHPJson = extension_loaded('json');
24+
$this->_extPHPXMLRPC = extension_loaded('xmlrpc');
2425
$this->_extPHPSimpleXML = extension_loaded('simplexml');
2526
if($this->_extPHPCurl == true){
2627
$this->_oCurl = curl_init();
@@ -347,6 +348,26 @@ public function getError(){
347348
return $this->_error;
348349
}
349350

351+
/**
352+
* Enable or disable the cache
353+
* @param boolean $bUseCache
354+
* @return $this
355+
* @author Progi1984
356+
*/
357+
public function setCacheStatus($bUseCache){
358+
$this->_useCache = $bUseCache;
359+
return $this;
360+
}
361+
362+
/**
363+
* Get the cache status
364+
* @return bool
365+
* @author Progi1984
366+
*/
367+
public function getCacheStatus(){
368+
return $this->_useCache;
369+
}
370+
350371
/**
351372
* @return array
352373
* @author Progi1984
@@ -360,7 +381,14 @@ public function getCore(){
360381
}
361382

362383
private function getCpu(){
363-
return $this->fn_json_decode($this->_api('getCpu'), true);
384+
if($this->_useCache){
385+
if(!isset($this->_arrCache['getCpu'])){
386+
$this->_arrCache['getCpu'] = $this->fn_json_decode($this->_api('getCpu'), true);
387+
}
388+
return $this->_arrCache['getCpu'];
389+
} else {
390+
return $this->fn_json_decode($this->_api('getCpu'), true);
391+
}
364392
}
365393
public function cpu_getIOWait(){
366394
$res = $this->getCpu();
@@ -436,7 +464,14 @@ public function cpu_getNice(){
436464
}
437465

438466
private function getDiskIO(){
439-
return $this->fn_json_decode($this->_api('getDiskIO'), true);
467+
if($this->_useCache){
468+
if(!isset($this->_arrCache['getDiskIO'])){
469+
$this->_arrCache['getDiskIO'] = $this->fn_json_decode($this->_api('getDiskIO'), true);
470+
}
471+
return $this->_arrCache['getDiskIO'];
472+
} else {
473+
return $this->fn_json_decode($this->_api('getDiskIO'), true);
474+
}
440475
}
441476
public function diskIO_getCount(){
442477
$res = $this->getDiskIO();
@@ -484,7 +519,14 @@ public function diskIO_getWriteBytes($piIdx){
484519
}
485520

486521
private function getFs(){
487-
return $this->fn_json_decode($this->_api('getFs'), true);
522+
if($this->_useCache){
523+
if(!isset($this->_arrCache['getFs'])){
524+
$this->_arrCache['getFs'] = $this->fn_json_decode($this->_api('getFs'), true);
525+
}
526+
return $this->_arrCache['getFs'];
527+
} else {
528+
return $this->fn_json_decode($this->_api('getFs'), true);
529+
}
488530
}
489531
public function fs_getCount(){
490532
$res = $this->getFs();
@@ -568,7 +610,14 @@ public function fs_getSize($piIdx){
568610
}
569611

570612
private function getLoad(){
571-
return $this->fn_json_decode($this->_api('getLoad'), true);
613+
if($this->_useCache){
614+
if(!isset($this->_arrCache['getLoad'])){
615+
$this->_arrCache['getLoad'] = $this->fn_json_decode($this->_api('getLoad'), true);
616+
}
617+
return $this->_arrCache['getLoad'];
618+
} else {
619+
return $this->fn_json_decode($this->_api('getLoad'), true);
620+
}
572621
}
573622
public function load_getMin1(){
574623
$res = $this->getLoad();
@@ -608,7 +657,14 @@ public function load_getMin15(){
608657
}
609658

610659
private function getLimits(){
611-
return $this->fn_json_decode($this->_api('getAllLimits'), true);
660+
if($this->_useCache){
661+
if(!isset($this->_arrCache['getAllLimits'])){
662+
$this->_arrCache['getAllLimits'] = $this->fn_json_decode($this->_api('getAllLimits'), true);
663+
}
664+
return $this->_arrCache['getAllLimits'];
665+
} else {
666+
return $this->fn_json_decode($this->_api('getAllLimits'), true);
667+
}
612668
}
613669
public function limit_getSTD(){
614670
$res = $this->getLimits();
@@ -744,7 +800,14 @@ public function limit_getSWAP(){
744800
}
745801

746802
private function getMem(){
747-
return $this->fn_json_decode($this->_api('getMem'), true);
803+
if($this->_useCache){
804+
if(!isset($this->_arrCache['getMem'])){
805+
$this->_arrCache['getMem'] = $this->fn_json_decode($this->_api('getMem'), true);
806+
}
807+
return $this->_arrCache['getMem'];
808+
} else {
809+
return $this->fn_json_decode($this->_api('getMem'), true);
810+
}
748811
}
749812
public function mem_getInactive(){
750813
$res = $this->getMem();
@@ -844,7 +907,14 @@ public function mem_getFree(){
844907
}
845908

846909
private function getMemSwap(){
847-
return $this->fn_json_decode($this->_api('getMemSwap'), true);
910+
if($this->_useCache){
911+
if(!isset($this->_arrCache['getMemSwap'])){
912+
$this->_arrCache['getMemSwap'] = $this->fn_json_decode($this->_api('getMemSwap'), true);
913+
}
914+
return $this->_arrCache['getMemSwap'];
915+
} else {
916+
return $this->fn_json_decode($this->_api('getMemSwap'), true);
917+
}
848918
}
849919
public function memswap_getTotal(){
850920
$res = $this->getMemSwap();
@@ -884,7 +954,14 @@ public function memswap_getUsed(){
884954
}
885955

886956
private function getNetwork(){
887-
return $this->fn_json_decode($this->_api('getNetwork'), true);
957+
if($this->_useCache){
958+
if(!isset($this->_arrCache['getNetwork'])){
959+
$this->_arrCache['getNetwork'] = $this->fn_json_decode($this->_api('getNetwork'), true);
960+
}
961+
return $this->_arrCache['getNetwork'];
962+
} else {
963+
return $this->fn_json_decode($this->_api('getNetwork'), true);
964+
}
888965
}
889966
public function network_getCount(){
890967
$res = $this->getNetwork();
@@ -936,7 +1013,14 @@ public function getNow(){
9361013
}
9371014

9381015
private function getProcessCount(){
939-
return $this->fn_json_decode($this->_api('getProcessCount'), true);
1016+
if($this->_useCache){
1017+
if(!isset($this->_arrCache['getProcessCount'])){
1018+
$this->_arrCache['getProcessCount'] = $this->fn_json_decode($this->_api('getProcessCount'), true);
1019+
}
1020+
return $this->_arrCache['getProcessCount'];
1021+
} else {
1022+
return $this->fn_json_decode($this->_api('getProcessCount'), true);
1023+
}
9401024
}
9411025
public function processcount_getZombie(){
9421026
$res = $this->getProcessCount();
@@ -988,7 +1072,14 @@ public function processcount_getSleeping(){
9881072
}
9891073

9901074
private function getProcessList(){
991-
return $this->fn_json_decode($this->_api('getProcessList'), true);
1075+
if($this->_useCache){
1076+
if(!isset($this->_arrCache['getProcessList'])){
1077+
$this->_arrCache['getProcessList'] = $this->fn_json_decode($this->_api('getProcessList'), true);
1078+
}
1079+
return $this->_arrCache['getProcessList'];
1080+
} else {
1081+
return $this->fn_json_decode($this->_api('getProcessList'), true);
1082+
}
9921083
}
9931084
public function processlist_getCount(){
9941085
$res = $this->getProcessList();
@@ -1132,7 +1223,14 @@ public function processlist_getNice($piIdx){
11321223
}
11331224

11341225
private function getSensors(){
1135-
return $this->fn_json_decode($this->_api('getSensors'), true);
1226+
if($this->_useCache){
1227+
if(!isset($this->_arrCache['getSensors'])){
1228+
$this->_arrCache['getSensors'] = $this->fn_json_decode($this->_api('getSensors'), true);
1229+
}
1230+
return $this->_arrCache['getSensors'];
1231+
} else {
1232+
return $this->fn_json_decode($this->_api('getSensors'), true);
1233+
}
11361234
}
11371235
public function sensors_getCount(){
11381236
$res = $this->getSensors();
@@ -1168,7 +1266,14 @@ public function sensors_getLabel($piIdx){
11681266
}
11691267

11701268
private function getSystem(){
1171-
return $this->fn_json_decode($this->_api('getSystem'), true);
1269+
if($this->_useCache){
1270+
if(!isset($this->_arrCache['getSystem'])){
1271+
$this->_arrCache['getSystem'] = $this->fn_json_decode($this->_api('getSystem'), true);
1272+
}
1273+
return $this->_arrCache['getSystem'];
1274+
} else {
1275+
return $this->fn_json_decode($this->_api('getSystem'), true);
1276+
}
11721277
}
11731278
public function system_getLinuxDistro(){
11741279
$res = $this->getSystem();

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ Example usage:
4242

4343
Changelog
4444
---------
45-
**Version 0.10**
45+
**Version 0.1**
4646
- Initial Release
4747

48-
**Version 0.11** __(current)__
48+
**Version 0.2** __(current)__
4949
- ADDED pingServer() which return a boolean to check if Glances server is available
5050
- ADDED Replacement for functions used in Curl / JSON / SimpleXML / XmlRPC (Issue [#3](https://github.com/Progi1984/PHPGlances/issues/3))
51-
- ADDED getError() which return a string with the intercepted error when a function (like listMethods()) return false
51+
- ADDED getError() which return a string with the intercepted error when a function (like listMethods()) return false
52+
- ADDED setCacheStatus() which permits to disable and enable the cache & getCacheStatus() which permits to get the status (true or false) of the cache

samples/sample00.php

+4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
include_once '../PHPGlances/PHPGlances.php';
44

5+
define('USE_CACHE', true);
6+
57
$oGlances = new PHPGlances('http://127.0.0.1', 61209);
68
$bAlive = $oGlances->pingServer();
79
if(!$bAlive){
810
echo 'Can\'t connect to the server';
911
} else {
12+
$oGlances->setCacheStatus(USE_CACHE);
13+
1014
$res = $oGlances->listMethods();
1115
echo 'listMethods : ';
1216
echo '<ul>';

0 commit comments

Comments
 (0)