Project

General

Profile

1
<?php
2
require_once('./js/log4php/Logger.php');
3
require_once('./paths.php');
4
require_once('./MYDB.php');
5

    
6
class MigrateCache {
7
	private $log;
8
	private $db;
9
	private $cache = null; //predis
10

    
11
private $remotecache=null;
12

    
13
public function __construct(){
14
		
15
		global $redis_host;
16
		global $redis_port;
17
		global $redis_scheme;
18
		global $host;  
19
                global $db_name;
20
                
21

    
22
//$redis_host='localhost';
23
/*$redis_remote_host='duffy.di.uoa.gr';
24

    
25
                $redis_remote_port=$redis_port;
26
                $redis_remote_scheme=$redis_scheme;
27
              */
28
               $this->log = Logger::getLogger("__CLASS__");
29
               Logger::configure('./js/log4php/log4php.xml');	
30
 
31
             if(class_exists("Predis\Client")){
32

    
33
                        try {
34
//todo changed timeout from 0 to -1 
35
                        $this->cache = new Predis\Client(array("scheme" => $redis_scheme,"host" => $redis_host,"port" => $redis_port,"read_write_timeout" => -1));
36
                                $this->log->info("redis host: ".$redis_host." and redis port: ".$redis_port);
37
                        } catch(Exception $e) {
38
                                $this->log->error("Error connecting to Redis server: ".$e->getMessage());
39
                                $this->cache = null;
40
                        }
41
                }
42
        else{
43
            $this->log->info("cache does not exist"); //predis
44
                        exit;
45
        }
46

    
47

    
48
	}
49
public function migrate($redis_remote_host,$redis_remote_port)
50
{
51
 	
52
global $redis_host;
53
                global $redis_port;
54
                global $redis_scheme;
55
                global $host;
56
               
57

    
58
$backupKeys=$this->cache->hkeys('STATS_NUMBERS');
59
$this->log->info("migrating Stat Numbers from ".$redis_host." to  ".$redis_remote_host." on ".$redis_remote_port);
60

    
61
try {
62

    
63
//todo changed timeout from 0 to -1 
64
                        $this->remotecache = new Predis\Client(array("scheme" => $redis_scheme,"host" => $redis_remote_host,"port" => $redis_port,"read_write_timeout" => -1));
65
                                $this->log->info("redis host: ".$redis_remote_host." and redis port: ".$redis_remote_port);
66
                        } catch(Exception $e) {
67
                                $this->log->error("Error connecting to Redis server: ".$e->getMessage());
68
                                $this->cache = null;
69
                        }
70

    
71

    
72
$this->remotecache->connect();
73

    
74
for($x=0;$x<count($backupKeys);$x++)
75
{
76
        $key=$backupKeys[$x];
77
        $value=$this->cache->hget('STATS_NUMBERS',$key);
78

    
79
$this->log->info("saving  : ".$key." : ".$value);
80
$this->remotecache->hmset('STATS_NUMBERS',$key,$value);
81
$this->log->info("query saved  : ".$this->remotecache->hget('STATS_NUMBERS',$key));
82
}
83

    
84

    
85

    
86

    
87

    
88

    
89
$this->log->info("migrating Shadow Stat Numbers from ".$redis_host." to  ".$redis_remote_host." on ".$redis_remote_port);
90

    
91

    
92

    
93
$backupKeys=$this->cache->hkeys('SHADOW_STATS_NUMBERS');
94

    
95

    
96

    
97
for($x=0;$x<count($backupKeys);$x++)
98
{
99
        $key=$backupKeys[$x];
100
        $value=$this->cache->hget('SHADOW_STATS_NUMBERS',$key);
101

    
102
$this->log->info("saving  : ".$key." : ".$value);
103
$this->remotecache->hmset('SHADOW_STATS_NUMBERS',$key,$value);
104
$this->log->info("query saved  : ".$this->remotecache->hget('SHADOW_STATS_NUMBERS',$key));
105
}
106

    
107

    
108

    
109
//*****************************
110

    
111

    
112
$this->log->info("Migrating data queries...");
113
$this->log->info("*******************************************");
114

    
115
$keys = $this->cache->keys("*");
116

    
117

    
118
for($x=0;$x<count($keys);$x++) {
119

    
120
if ((strpos($keys[$x],'chart')===false)&&(strpos($keys[$x],'table')===false)&&(strpos($keys[$x],'STATS')===false) )
121
{
122

    
123
 try
124
{
125

    
126
$values=$this->cache->hgetall($keys[$x]);
127

    
128

    
129
//$persistent= $values['persistent'];
130

    
131
//if($persistent)
132

    
133
{ 
134

    
135
$this->log->info("Moving...");
136

    
137
$this->remotecache->hmset($keys[$x],$values);
138

    
139
$this->log->info("id : ".$keys[$x]);
140
//$this->log->info("query : ".$this->remotecache->hget($keys[$x],"query"));
141

    
142
//$this->log->info("res: ".$this->remotecache->hget($keys[$x],"results"));
143

    
144
}
145

    
146
 
147

    
148
}
149
catch(Exception $e) {
150

    
151
$this->log->error('Error : '.$e->getMessage());
152
}
153
}
154
 else
155
{ $this->log->info("Ignoring chart key: ".$keys[$x]); }
156

    
157

    
158
}
159

    
160

    
161

    
162

    
163
$this->remotecache->quit();
164
$this->cache->quit();
165

    
166
$this->log->info("Done migtating ! ");
167

    
168
}
169

    
170
}
171

    
172
//$rc = new MigrateCache();
173
//$rc->migrate();
174
?>
(12-12/28)