1
|
<?php
|
2
|
require_once('./js/log4php/Logger.php');
|
3
|
require_once('./paths.php');
|
4
|
//require_once('./MYDB.php');
|
5
|
include_once('./MYDB.php');
|
6
|
//require_once('./controller.php');
|
7
|
|
8
|
class RefreshCharts{
|
9
|
|
10
|
/* TODO Eri: Class for recomputng all the charts present in the cache.
|
11
|
*/
|
12
|
|
13
|
private $log;
|
14
|
private $cache = null;
|
15
|
public function __construct(){
|
16
|
|
17
|
global $redis_host;
|
18
|
global $redis_port;
|
19
|
global $redis_scheme;
|
20
|
global $host;
|
21
|
|
22
|
$this->log = Logger::getLogger(__CLASS__);
|
23
|
Logger::configure('./js/log4php/log4php.xml');
|
24
|
|
25
|
if(class_exists("Predis\Client")){
|
26
|
|
27
|
try {
|
28
|
//todo changed timeout from 0 to -1
|
29
|
$this->cache = new Predis\Client(array("scheme" => $redis_scheme,"host" => $redis_host,"port" => $redis_port,"read_write_timeout" => -1));
|
30
|
$this->log->info("redis host: ".$redis_host." and redis port: ".$redis_port);
|
31
|
} catch(Exception $e) {
|
32
|
$this->log->error("Error connecting to Redis server: ".$e->getMessage());
|
33
|
$this->cache = null;
|
34
|
}
|
35
|
}
|
36
|
else{
|
37
|
$this->log->info("cache does not exist"); //predis
|
38
|
exit;
|
39
|
}
|
40
|
}
|
41
|
|
42
|
|
43
|
function refresh() {
|
44
|
if($this->cache != null){
|
45
|
$this->log->info("cache exists");
|
46
|
$database = new MYDB();
|
47
|
$database->doConnect($GLOBALS['schema_file']);
|
48
|
$keys = $this->cache->keys("*");
|
49
|
|
50
|
$this->log->info(" ******************REFRESHING CACHE*************************");
|
51
|
|
52
|
//TODO REFRESH CHARTS AND DATA
|
53
|
|
54
|
for($x=0;$x<count($keys);$x++) {
|
55
|
$this->log->info("Looking up for key: ".$keys[$x]);
|
56
|
|
57
|
if ((strpos($keys[$x],'chart')===false)&&(strpos($keys[$x],'table')===false)&&(strpos($keys[$x],'STATS')===false))
|
58
|
{
|
59
|
try
|
60
|
{
|
61
|
|
62
|
$query = $this->cache->hget($keys[$x],'query');
|
63
|
$persistent= $this->cache->hget($keys[$x],'persistent');
|
64
|
if(($query!=null)&&($persistent))
|
65
|
{ $this->log->info("going to recalculate the results for query ".$query );
|
66
|
$fetchMode = $this->cache->hget($keys[$x], 'fetchMode');
|
67
|
if($fetchMode==null)
|
68
|
{ $fetchMode=3; }
|
69
|
$shadowResults = $database->doQueryNoCache($query, $fetchMode);
|
70
|
$result = $this->cache->hget($keys[$x], 'results');
|
71
|
// $this->log->info("Currently stored results".$result );
|
72
|
$backup = $this->cache->hget($keys[$x], 'backup');
|
73
|
//$this->log->info("Currently stored backup".$backup);
|
74
|
|
75
|
if(json_encode($shadowResults))
|
76
|
{$shadowResults=json_encode($shadowResults);}
|
77
|
$this->cache->hmset($keys[$x],array("results" => $result, "query" =>$query,"persistent" => $persistent,"shadow" => $shadowResults, "fetchMode" => $fetchMode, "backup" => $backup));
|
78
|
$this->log->info("Stored Entry : ");
|
79
|
$this->log->info("Shadow ".$this->cache->hget($keys[$x],'shadow'));
|
80
|
}
|
81
|
else{
|
82
|
|
83
|
$this->log->info("going to delete the key: ".$keys[$x]);
|
84
|
$this->cache->del($keys[$x]);
|
85
|
}
|
86
|
|
87
|
}//catch
|
88
|
catch(Exception $e) {
|
89
|
$this->log->error('Error : '.$e->getMessage());
|
90
|
|
91
|
}
|
92
|
}
|
93
|
|
94
|
|
95
|
else
|
96
|
{
|
97
|
$this->log->info("Ignoring chart key: ".$keys[$x]);
|
98
|
|
99
|
}
|
100
|
|
101
|
|
102
|
}
|
103
|
|
104
|
|
105
|
// for eeach key loop
|
106
|
|
107
|
$database->doDisconnect();
|
108
|
|
109
|
} else {
|
110
|
$this->log->info("cache does not exist");
|
111
|
}
|
112
|
}
|
113
|
}
|
114
|
//$rc = new RefreshCharts();
|
115
|
//$rc->refresh();
|
116
|
|
117
|
?>
|