1
|
<?php
|
2
|
require_once('./js/log4php/Logger.php');
|
3
|
require_once('./paths.php');
|
4
|
require_once('./MYDB.php');
|
5
|
|
6
|
class RestoreCache {
|
7
|
/*TODO Eri : Restores the contents of the current cache from its latest backup.*/
|
8
|
|
9
|
private $log;
|
10
|
private $db;
|
11
|
private $cache = null; //predis
|
12
|
public function __construct(){
|
13
|
|
14
|
global $redis_host;
|
15
|
global $redis_port;
|
16
|
global $redis_scheme;
|
17
|
global $host;
|
18
|
|
19
|
$this->log = Logger::getLogger("RestoreCache");
|
20
|
Logger::configure('./js/log4php/log4php.xml');
|
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
|
public function restore()
|
41
|
{
|
42
|
|
43
|
//first restore numbers
|
44
|
|
45
|
$backupKeys=$this->cache->hkeys('STATS_NUMBERS_BACKUP');
|
46
|
$this->log->info("Restoring Stat Numbers...");
|
47
|
|
48
|
for($x=0;$x<count($backupKeys);$x++)
|
49
|
{
|
50
|
$key=$backupKeys[$x];
|
51
|
$value=$this->cache->hget('STATS_NUMBERS_BACKUP',$key);
|
52
|
$this->cache->hset('STATS_NUMBERS',$key,$value); }
|
53
|
|
54
|
//now promote all other entries
|
55
|
$keys = $this->cache->keys("*");
|
56
|
|
57
|
$this->log->info("Restoring data queries...");
|
58
|
$this->log->info("*******************************************");
|
59
|
|
60
|
for($x=0;$x<count($keys);$x++) {
|
61
|
|
62
|
if ((strpos($keys[$x],'chart')===false)&&(strpos($keys[$x],'table')===false)&&(strpos($keys[$x],'STATS')===false) )
|
63
|
{
|
64
|
try{
|
65
|
$query = $this->cache->hget($keys[$x],'query');
|
66
|
$persistent= $this->cache->hget($keys[$x],'persistent');
|
67
|
|
68
|
if(($query!=null)&&($persistent))
|
69
|
{
|
70
|
|
71
|
$fetchMode = $this->cache->hget($keys[$x], 'fetchMode');
|
72
|
$shadowResults = $this->cache->hget($keys[$x],'shadow');
|
73
|
$result = $this->cache->hget($keys[$x], 'results');
|
74
|
$backup = $this->cache->hget($keys[$x], 'backup');
|
75
|
|
76
|
$this->cache->hmset($keys[$x],array("results" =>$backup,"query" =>$query,"persistent" =>$persistent,"shadow" =>$shadowResults,"fetchMode" =>$fetchMode,"backup" =>$backup));
|
77
|
$this->log->info("Restored ".$keys[$x]);
|
78
|
|
79
|
|
80
|
}
|
81
|
|
82
|
}//catch
|
83
|
catch(Exception $e) {
|
84
|
|
85
|
$this->log->error('Error : '.$e->getMessage());
|
86
|
|
87
|
}
|
88
|
}
|
89
|
else
|
90
|
{ $this->log->info("Ignoring chart key: ".$keys[$x]); }
|
91
|
}
|
92
|
|
93
|
$this->cache->quit();
|
94
|
|
95
|
$this->log->info("Done! ");
|
96
|
}
|
97
|
}
|
98
|
|
99
|
|
100
|
?>
|