8000 Rates cache fix · etherscan-io/Ethplorer@b3b907f · GitHub
[go: up one dir, main page]

Skip to content

Commit b3b907f

Browse files
committed
Rates cache fix
1 parent e7a9236 commit b3b907f

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

service/lib/ethplorer.php

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ class Ethplorer {
145145

146146
protected $memUsage = [];
147147

148+
/**
149+
* Token current prices in-memory cache
150+
*
151+
* @var array
152+
*/
153+
protected $aRates = [];
154+
148155
/**
149156
* Constructor.
150157
*
@@ -2729,6 +2736,19 @@ public function getTokenPrice30d($address){
27292736
return $result;
27302737
}
27312738

2739+
/**
2740+
* Reads rates cache to local variable.
2741+
*/
2742+
protected function getRatesCached($update = FALSE){
2743+
if(empty($this->aRates) || $update){
2744+
$this->aRates = $this->oCache->get('rates', false, true);
2745+
if(!is_array($this->aRates)){
2746+
$this->aRates = [];
2747+
}
2748+
}
2749+
return $this->aRates;
2750+
}
2751+
27322752
public function getTokenPrice($address, $updateCache = FALSE){
27332753
// evxProfiler::checkpoint('getTokenPrice', 'START', 'address=' . $address . ', updateCache=' . ($updateCache ? 'TRUE' : 'FALSE'));
27342754
$result = FALSE;
@@ -2746,12 +2766,8 @@ public function getTokenPrice($address, $updateCache = FALSE){
27462766
$knownPrice = isset($this->aSettings['updateRates']) && in_array($address, $this->aSettings['updateRates']);
27472767

27482768
if(!$isHidden && $knownPrice){
2749-
$cache = 'rates';
2750-
$rates = $this->oCache->get($cache, false, true);
2769+
$this->getRatesCached();
27512770
if($updateCache){
2752-
if(!is_array($rates)){
2753-
$rates = array();
2754-
}
27552771
if(isset($this->aSettings['currency'])){
27562772
$method = 'getCurrencyCurrent';
27572773
$params = array($address, 'USD');
@@ -2773,25 +2789,21 @@ public function getTokenPrice($address, $updateCache = FALSE){
27732789
$result['diff30d'] = $pdiff;
27742790
}
27752791
}
2776-
$rates[$address] = $result;
2777-
$this->oCache->save($cache, $rates);
2792+
$this->aRates[$address] = $result;
2793+
$this->oCache->save('rates', $this->aRates);
27782794
}
27792795
}
27802796
}
2781-
if(is_array($rates) && isset($rates[$address])){
2782-
$result = $rates[$address];
2797+
if(is_array($this->aRates) && isset($this->aRates[$address])){
2798+
$result = $this->aRates[$address];
27832799
}
27842800
}
27852801
// evxProfiler::checkpoint('getTokenPrice', 'FINISH');
27862802
return $result;
27872803
}
27882804

27892805
public function getAllTokenPrice(){
2790-
$cache = 'rates';
2791-
$rates = $this->oCache->get($cache, false, true);
2792-
if(!is_array($rates)){
2793-
$rates = array();
2794-
}
2806+
$this->getRatesCached();
27952807
if(isset($this->aSettings['currency'])){
27962808
$result = $this->_jsonrpcall($this->aSettings['currency'], 'getCurrentPrices', array());
27972809
if($result && is_array($result)){
@@ -2813,16 +2825,16 @@ public function getAllTokenPrice(){
28132825
}
28142826
}
28152827
unset($aPriceData['address']);
2816-
$rates[$address] = $aPriceData;
2828+
$this->aRates[$address] = $aPriceData;
28172829
}
28182830
}
28192831
}
2820-
if(is_array($rates) && sizeof($rates)){
2821-
$this->oCache->save($cache, $rates);
2832+
if(is_array($this->aRates) && sizeof($this->aRates)){
2833+
$this->oCache->save('rates', $this->aRates);
28222834
}
28232835
}
28242836
}
2825-
return $rates;
2837+
return $this->aRates;
28262838
}
28272839

28282840
public function getTokenPriceHistory($address, $period = 0, $type = 'daily', $updateCache = FALSE, $updateFullHistory = FALSE){

0 commit comments

Comments
 (0)
0