Project

General

Profile

1 33214 eri.katsar
<?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 33689 eri.katsar
		global $host;
19
                global $db_name;
20 33214 eri.katsar
21
22 33689 eri.katsar
//$redis_host='localhost';
23
/*$redis_remote_host='duffy.di.uoa.gr';
24
25 33214 eri.katsar
                $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 33689 eri.katsar
public function migrate($redis_remote_host,$redis_remote_port)
50 33214 eri.katsar
{
51
52
global $redis_host;
53
                global $redis_port;
54
                global $redis_scheme;
55
                global $host;
56 33668 eri.katsar
57 33214 eri.katsar
58 33668 eri.katsar
$backupKeys=$this->cache->hkeys('STATS_NUMBERS');
59 33689 eri.katsar
$this->log->info("migrating Stat Numbers from ".$redis_host." to  ".$redis_remote_host." on ".$redis_remote_port);
60 33214 eri.katsar
61 33668 eri.katsar
try {
62 33214 eri.katsar
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 33689 eri.katsar
                                $this->log->info("redis host: ".$redis_remote_host." and redis port: ".$redis_remote_port);
66 33214 eri.katsar
                        } catch(Exception $e) {
67
                                $this->log->error("Error connecting to Redis server: ".$e->getMessage());
68
                                $this->cache = null;
69
                        }
70
71
72 33668 eri.katsar
$this->remotecache->connect();
73 33214 eri.katsar
74 33668 eri.katsar
for($x=0;$x<count($backupKeys);$x++)
75
{
76
        $key=$backupKeys[$x];
77
        $value=$this->cache->hget('STATS_NUMBERS',$key);
78 33214 eri.katsar
79 33668 eri.katsar
$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 33214 eri.katsar
84 34069 eri.katsar
85
86 34644 eri.katsar
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 33668 eri.katsar
$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 33214 eri.katsar
if ((strpos($keys[$x],'chart')===false)&&(strpos($keys[$x],'table')===false)&&(strpos($keys[$x],'STATS')===false) )
121
{
122
123 33668 eri.katsar
 try
124
{
125
126 33214 eri.katsar
$values=$this->cache->hgetall($keys[$x]);
127
128 34069 eri.katsar
129 33891 eri.katsar
//$persistent= $values['persistent'];
130
131
//if($persistent)
132
133 33668 eri.katsar
{
134 33214 eri.katsar
135
$this->log->info("Moving...");
136
137
$this->remotecache->hmset($keys[$x],$values);
138
139 33668 eri.katsar
$this->log->info("id : ".$keys[$x]);
140 33689 eri.katsar
//$this->log->info("query : ".$this->remotecache->hget($keys[$x],"query"));
141 34471 eri.katsar
142 34644 eri.katsar
//$this->log->info("res: ".$this->remotecache->hget($keys[$x],"results"));
143 33214 eri.katsar
144 33668 eri.katsar
}
145 33214 eri.katsar
146 34471 eri.katsar
147 33214 eri.katsar
148
}
149
catch(Exception $e) {
150
151
$this->log->error('Error : '.$e->getMessage());
152
}
153 33668 eri.katsar
}
154 33214 eri.katsar
 else
155
{ $this->log->info("Ignoring chart key: ".$keys[$x]); }
156 33668 eri.katsar
157
158 33214 eri.katsar
}
159
160 34471 eri.katsar
161
162
163 33668 eri.katsar
$this->remotecache->quit();
164 33214 eri.katsar
$this->cache->quit();
165
166 33668 eri.katsar
$this->log->info("Done migtating ! ");
167 33214 eri.katsar
168
}
169
170 33668 eri.katsar
}
171 33214 eri.katsar
172 33689 eri.katsar
//$rc = new MigrateCache();
173
//$rc->migrate();
174 33214 eri.katsar
?>