summaryrefslogtreecommitdiff
path: root/inc/cache.php
blob: 3546e031ed54e975142054fc65514146993f8d61 (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
<?php
	class Cache {
		private static $cache;
		public static function init() {
			global $config;
			
			switch($config['cache']['enabled']) {
				case 'memcached':
					self::$cache = new Memcached();
					self::$cache->addServers($config['cache']['memcached']);
					break;
			}
		}
		public static function get($key) {
			global $config, $debug;
			
			$data = false;
			switch($config['cache']['enabled']) {
				case 'memcached':
					if(!self::$cache)
						self::init();
					$data = self::$cache->get($key);
					break;
				case 'apc':
					$data = apc_fetch($key);
					break;
				case 'xcache':
					$data = xcache_get($key);
					break;
			}
			
			// debug
			if($data && $config['debug']) {
				$debug['cached'][] = $key;
			}
			
			return $data;
		}
		public static function set($key, $value, $expires = false) {
			global $config;
			
			if(!$expires)
				$expires = $config['cache']['timeout'];
			
			switch($config['cache']['enabled']) {
				case 'memcached':
					if(!self::$cache)
						self::init();
					self::$cache->set($key, $value, $expires);
					break;
				case 'apc':
					apc_store($key, $value, $expires);
					break;
				case 'xcache':
					xcache_set($key, $value, $expires);
					break;
			}			
		}
		public static function delete($key) {
			global $config;
			
			switch($config['cache']['enabled']) {
				case 'memcached':
					if(!self::$cache)
						self::init();
					self::$cache->delete($key);
					break;
				case 'apc':
					apc_delete($key);
					break;
				case 'xcache':
					xcache_unset($key);
					break;
			}
		}
		public static function flush() {
			global $config;
			
			switch($config['cache']['enabled']) {
				case 'memcached':
					if(!self::$cache)
						self::init();
					return self::$cache->flush();
				case 'apc':
					return apc_clear_cache('user');
			}
			
			return false;
		}
	}