Project

General

Profile

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
	private $log;
10
	private $cache = null;
11
	public function __construct(){
12
		
13
		global $redis_host;
14
		global $redis_port;
15
		global $redis_scheme;
16
		global $host;
17

    
18
	$this->log = Logger::getLogger(__CLASS__);
19
      	Logger::configure('./js/log4php/log4php.xml');
20

    
21
              if(class_exists("Predis\Client")){
22
		
23
			try {
24
//todo changed timeout from 0 to -1 
25
			$this->cache = new Predis\Client(array("scheme" => $redis_scheme,"host" => $redis_host,"port" => $redis_port,"read_write_timeout" => -1));
26
				$this->log->info("redis host: ".$redis_host." and redis port: ".$redis_port);
27
			} catch(Exception $e) {
28
				$this->log->error("Error connecting to Redis server: ".$e->getMessage());
29
				$this->cache = null; 
30
			}
31
		}
32
    	else{
33
            $this->log->info("cache does not exist"); //predis
34
			exit;
35
    	}
36
	}
37

    
38

    
39

    
40

    
41

    
42
function refresh() {
43
		if($this->cache != null){ 
44
			$this->log->info("cache exists");			
45
			$database = new MYDB();
46
			$database->doConnect($GLOBALS['schema_file']);
47
			$keys = $this->cache->keys("*");
48

    
49
 $this->log->info(" ******************REFRESHING CACHE*************************");
50

    
51
//TODO REFRESH CHARTS AND DATA
52

    
53
for($x=0;$x<count($keys);$x++) {
54
   $this->log->info("Looking up for key: ".$keys[$x]);
55

    
56
if (strpos($keys[$x],'STATS')===false)
57
{
58
   try
59
{
60

    
61
$query = $this->cache->hget($keys[$x],'query');
62
$persistent= $this->cache->hget($keys[$x],'persistent');
63
//if($query!=null&&$persistent=='true')
64
if($query!=null)
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 : ".$this->cache->hget($keys[$x],'shadow'));
79
					}
80
else{
81

    
82
$this->log->info("going to delete the key: ".$keys[$x]." for persistence : ".$persistent);
83
                     $this->cache->del($keys[$x]);
84
}
85

    
86
}//catch  
87
catch(Exception $e) {
88
$this->log->error('Error : '.$e->getMessage());
89

    
90
}			
91
	}
92

    
93

    
94
 else 
95
{ 
96
$this->log->info("Ignoring chart key: ".$keys[$x]);  
97
 }
98

    
99

    
100
  			}
101

    
102

    
103
// for eeach key loop
104

    
105
  			$database->doDisconnect();
106
			
107
		} else {
108
			$this->log->info("cache does not exist");
109
		}
110
	}
111
}
112
//$rc = new RefreshCharts();
113
//$rc->refresh();
114

    
115
?>
(22-22/28)