Project

General

Profile

1 38716 eri.katsar
<?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
	private $cache = null;
9
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
				$this->cache = new Predis\Client(array(
24
					"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
				$this->cache = null;
35
			}
36
		}
37
    	else{
38
            $this->log->info("cache does not exist"); //predis
39
			exit;
40
    	}
41
	}
42
43
	function refresh() {
44
45
		if($this->cache != null){
46
			$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
$pers=$this->cache->hget($keys[$x], 'persistent');
55
56
				if($pers=='true'||$pers=='1')
57
 {
58
					if(($query = $this->cache->hget($keys[$x], 'query'))!=null) {
59
60
						$this->log->info("going to recalculate the results for key: ".$keys[$x]);
61
						$fetchMode = $this->cache->hget($keys[$x], 'fetchMode');
62
						$results = $database->doQueryNoCache($query, $fetchMode);
63
						$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
73
  			$database->doDisconnect();
74
75
		} else {
76
			$this->log->info("cache does not exist");
77
		}
78
	}
79
}
80
81
$rc = new RefreshCache();
82
$rc->refresh();
83
84
85
?>