Project

General

Profile

1 29394 stefania.m
<?php
2
require_once('./js/log4php/Logger.php');
3
require_once('./paths.php');
4
require_once('./controller.php');
5
6
class RefreshCache {
7
	private $log;
8 29395 stefania.m
	private $cache = null;
9 29394 stefania.m
10
	public function __construct(){
11
12
		global $redis_host;
13
		global $redis_port;
14
		global $redis_scheme;
15
16
		global $host;
17
18
		$this->log = Logger::getLogger(__CLASS__);
19
		if(class_exists("Predis\Client")){
20
21
			try {
22
23 29395 stefania.m
				$this->cache = new Predis\Client(array(
24 29394 stefania.m
					"scheme" => $redis_scheme,
25
					"host" => $redis_host,
26
        			"port" => $redis_port,
27
					"read_write_timeout" => 0));
28
29
				$this->log->info("redis host: ".$redis_host);
30
				$this->log->info("redis port: ".$redis_port);
31
32
			} catch(Exception $e) {
33
				$this->log->error("Error connecting to Redis server: ".$e->getMessage());
34 29395 stefania.m
				$this->cache = null;
35 29394 stefania.m
			}
36
		}
37
    	else{
38
            $this->log->info("cache does not exist"); //predis
39
			exit;
40
    	}
41
	}
42
43
	function refresh() {
44
45 29395 stefania.m
		if($this->cache != null){
46 29394 stefania.m
			$this->log->info("cache exists");
47
48
			$database = new MYDB();
49
			$database->doConnect($GLOBALS['schema_file']);
50
51
			$keys = $this->cache->keys("*");
52
			for($x=0;$x<count($keys);$x++) {
53
54 37784 eri.katsar
$pers=$this->cache->hget($keys[$x], 'persistent');
55
56
				if($pers=='true'||$pers=='1')
57
 {
58 29394 stefania.m
					if(($query = $this->cache->hget($keys[$x], 'query'))!=null) {
59
60
						$this->log->info("going to recalculate the results for key: ".$keys[$x]);
61 29407 stefania.m
						$fetchMode = $this->cache->hget($keys[$x], 'fetchMode');
62
						$results = $database->doQueryNoCache($query, $fetchMode);
63 29394 stefania.m
						$this->cache->hset($keys[$x], 'results', json_encode($results));
64
					}
65
66
  				} else {
67
68
  					$this->log->info("going to delete the key: ".$keys[$x]);
69
  					$this->cache->del($keys[$x]);
70
  				}
71
  			}
72 29404 stefania.m
73
  			$database->doDisconnect();
74 29394 stefania.m
75
		} else {
76
			$this->log->info("cache does not exist");
77
		}
78
	}
79
}
80
81
$rc = new RefreshCache();
82
$rc->refresh();
83
84
85 37784 eri.katsar
?>