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

    
8
/* TODO Eri: Copies all the contentes of the source redis cache to a remote redis instance. Does not overwrite the backup data in the remote cache. */
9

    
10
	private $log;
11
	private $db;
12
	private $cache = null; //predis
13

    
14
private $remotecache=null;
15

    
16
public function __construct(){
17
		
18
		global $redis_host;
19
		global $redis_port;
20
		global $redis_scheme;
21
		global $host;  
22
                global $db_name;
23
                
24

    
25
//$redis_host='localhost';
26
/*$redis_remote_host='duffy.di.uoa.gr';
27

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

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

    
50

    
51
	}
52
public function migrate($redis_remote_host,$redis_remote_port)
53
{
54
 	
55
global $redis_host;
56
                global $redis_port;
57
                global $redis_scheme;
58
                global $host;
59
               
60
//$this->cache->migrate(
61
  //      $redis_remote_host,    # localhost in my example
62
    //    $redis_port,    # 1234
63
      //  'STATS_NUMBERS',                    # The key
64
        //$db_name,-1); //migrate 
65

    
66
//$this->cache->executeRaw(["MIGRATE", $redis_remote_host, $redis_port,'STATS_NUMBERS', 'stats', -1]);
67
//response = $client->executeRaw(["MIGRATE", $host, $port, $key, $destination, $timeout]);
68
//Predis\Client::executeRaw()
69

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

    
73
try {
74

    
75
//todo changed timeout from 0 to -1 
76
                        $this->remotecache = new Predis\Client(array("scheme" => $redis_scheme,"host" => $redis_remote_host,"port" => $redis_port,"read_write_timeout" => -1));
77
                                $this->log->info("redis host: ".$redis_remote_host." and redis port: ".$redis_remote_port);
78
                        } catch(Exception $e) {
79
                                $this->log->error("Error connecting to Redis server: ".$e->getMessage());
80
                                $this->cache = null;
81
                        }
82

    
83

    
84
$this->remotecache->connect();
85

    
86
for($x=0;$x<count($backupKeys);$x++)
87
{
88
        $key=$backupKeys[$x];
89
        $value=$this->cache->hget('STATS_NUMBERS',$key);
90

    
91
$this->log->info("saving  : ".$key." : ".$value);
92
$this->remotecache->hmset('STATS_NUMBERS',$key,$value);
93
$this->log->info("query saved  : ".$this->remotecache->hget('STATS_NUMBERS',$key));
94
}
95

    
96

    
97

    
98
$this->log->info("Migrating data queries...");
99
$this->log->info("*******************************************");
100

    
101
$keys = $this->cache->keys("*");
102

    
103

    
104
for($x=0;$x<count($keys);$x++) {
105

    
106
if ((strpos($keys[$x],'chart')===false)&&(strpos($keys[$x],'table')===false)&&(strpos($keys[$x],'STATS')===false) )
107
{
108

    
109
 try
110
{
111

    
112
$values=$this->cache->hgetall($keys[$x]);
113

    
114

    
115
//$persistent= $values['persistent'];
116

    
117
//if($persistent)
118

    
119
{ 
120

    
121
$this->log->info("Moving...");
122

    
123
$this->remotecache->hmset($keys[$x],$values);
124

    
125
$this->log->info("id : ".$keys[$x]);
126
//$this->log->info("query : ".$this->remotecache->hget($keys[$x],"query"));
127

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

    
130
}
131

    
132
 
133

    
134
}
135
catch(Exception $e) {
136

    
137
$this->log->error('Error : '.$e->getMessage());
138
}
139
}
140
 else
141
{ $this->log->info("Ignoring chart key: ".$keys[$x]); }
142

    
143

    
144
}
145

    
146

    
147

    
148

    
149
$this->remotecache->quit();
150
$this->cache->quit();
151

    
152
$this->log->info("Done migtating ! ");
153

    
154
}
155

    
156
}
157

    
158
//$rc = new MigrateCache();
159
//$rc->migrate();
160
?>
(12-12/28)