aboutsummaryrefslogtreecommitdiff
path: root/class/cache.php
blob: b64ab9dc99c739a5796758e11ea475bfc0873721 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php

class cache {
	public $token = "";
	public $bypassCache;

	private $db;

	private $dbnmr;
	private $dbpagecache;
	private $db2nd;

	public function __construct($rconnect, $rdb){
		$this->db = new Redis();	
		$this->bypassCache=false;

		try {
			$this->db->connect($rconnect);
		} catch (Exception $e) {
			return $e->getMessage();
		}
		try {
			$this->db->ping();
		} catch (Exception $e) {
			return $e->getMessage();
		}
		try {
			$this->db->select($rdb);
		} catch (Exception $e) {
			return $e->getMessage();
		}
		$this->dbnmr = $rdb;
		$this->dbpagecache = PAGECACHE_DB;
		$this->db2nd = SECOND_CACHE;
	}

	public function check(){
		try {
			return $this->db->ping();
		} catch (Exception $e) {
			return $e->getMessage();
		}
	}

	public function setKey($key, $value, $ttl = null){
		$this->db->set($key, $value, $ttl);
	}

	public function getValue($key){
		return $this->db->get($key);
	}

	public function getToken($data, $append = ""){
		$this->token = CACHEPREFIX . $append . md5(strtolower($data));
		return $this->token;
	}
	
	public function exists($key){
		return $this->db->exists($key);
	}

	public function delete($key){
		return $this->db->delete($key);
	}

	public function del($key){
		return $this->db->delete($key);
	}

	public function setPageCache($key, $value, $ttl = 3600){
		$this->db->select( $this->dbpagecache );
		$this->db->set( $key, $value, $ttl );
		$this->db->select( $this->dbnmr );
	}

	public function getPageCache($key){
		$this->db->select( $this->dbpagecache );
		$data = $this->db->get( $key );
		$this->db->select( $this->dbnmr );
		return $data;
	}

	public function existsPageCache($key){
		$this->db->select( $this->dbpagecache );
		$data = $this->exists( $key );
		$this->db->select( $this->dbnmr );
		return $data;
	}

	public function flushPageCache(){
		$this->db->select( $this->dbpagecache );
		$this->db->flushDB();
		$this->db->select( $this->dbnmr );
	}

	public function flush($token = null){
		$this->flushPageCache();
		if ( is_null($token) )
			return $this->db->flushDB();
		else
			return $this->db->delete($token);
	}

	public function flushAll(){
		$this->db->flushDB();
		$this->flushPageCache();
		$this->flush2();
	}

	public function set2($key, $value, $ttl = null){
		$this->db->select( $this->db2nd );
		$this->db->set($key, $value, $ttl);
		$this->db->select( $this->dbnmr );
	}

	public function get2($key){
		$this->db->select( $this->db2nd );
		$data = $this->db->get($key);
		$this->db->select( $this->dbnmr );
		return $data;
	}

	public function exists2($key){
		$this->db->select( $this->db2nd );
		$data = $this->db->exists($key);
		$this->db->select( $this->dbnmr );
		return $data;
	}

	public function flush2($token = null){
		$this->db->select( $this->db2nd );
		if ( is_null($token) )
			$data = $this->db->flushDB();
		else
			$data = $this->db->delete($token);

		$this->db->select( $this->dbnmr );
		$this->flushPageCache();
		return $data;
	}
}