summaryrefslogtreecommitdiffstats
path: root/application/libraries/MemcacheLibrary.php
blob: 1716a1d1081942554677975f70d3c122ea8b5c70 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php

/**
* codeigniter-memcache v0.1
*
* codeigniter-memcache is a codeigniter library to work with memcached easily.
*
*
* @file MemcacheLibrary.php
* @copyright 2010 egnity - egnity.com
* @author Emre Yilmaz <mail@emreyilmaz.me>
* @license GNU General Public License
* @version 0.1
*/


class MemcacheLibrary {

	/**
	* variable that holds memcached backend instance
	*
	* @var object
	* @access public
	*/
	public $memcachedInstance;

	/**
	* variable that holds servers for the memcache
	*
	* @var array
	* @access public
	*/
	public $servers = array();


	/**
	* main CodeIgniter instance
	*
	* @var object
	* @access public
	*/
	public $CI;

	/**
	* constructor function for the library
	*/
	public function __construct() {

		/* initialize memcached instance */
		if(class_exists("Memcache")) {
			$this->memcachedInstance = new Memcache();
		} else {
			throw new Exception(
					"Memcached client doesn't exists in your PHP configuration"
				);
		}

		/* load super CI instance */
		$this->CI =& get_instance();

		/* load default server info */
		$this->CI->config->load("memcache");

		/* connect to default server */
		if($this->CI->config->item("MEMCACHE_HOST") && $this->CI->config->item("MEMCACHE_PORT") !== false) {
			$this->addServer($this->CI->config->item("MEMCACHE_HOST"), $this->CI->config->item("MEMCACHE_PORT"));
		}

	}

	/**
	* adder function for the memcache servers
	*
	* @access public
	* @return void
	*/
	public function addServer($server, $port) {
		$this->servers[] = array(
			"server" => $server,
			"port"	 => $port,
		);

		$this->memcachedInstance->addServer($server, $port);
	}

	/**
	* gets related key from the memcache
	*
	* @access public
	*/
	public function get($key) {
		$this->logDebugMessage(sprintf("%s key requested from memcache", $key));

		// hide notice if server is unreachable
		$old_error_level = error_reporting();
		error_reporting($old_error_level & ~E_NOTICE);

		$ret = $this->memcachedInstance->get($key);

		error_reporting($old_error_level);
		return $ret;
	}

	/**
	* sets related key to the memcache
	*
	* @access public
	*/
	public function set($key, $value, $expire = null) {
		$this->logDebugMessage(
				sprintf("%s key set to memcache. (expire: %s)",$key, $expire)
			);

		// hide notice if server is unreachable
		$old_error_level = error_reporting();
		error_reporting($old_error_level & ~E_NOTICE);

		$ret =  $this->memcachedInstance->set($key, $value, null, $expire);

		error_reporting($old_error_level);
		return $ret;
	}

	/**
	* deletes related key from the memcache
	*
	* @access public
	*/
	public function delete($key) {
		$this->logDebugMessage(sprintf("%s key deleted from memcache.", $key));
		// hide notice if server is unreachable
		$old_error_level = error_reporting();
		error_reporting($old_error_level & ~E_NOTICE);

		$ret = $this->memcachedInstance->delete($key);

		error_reporting($old_error_level);
		return $ret;
	}

	/**
	* increments related key from the memcache
	*
	* @access public
	*/
	public function increment($key, $offset = 1) {
		$this->logDebugMessage(sprintf("%s key incremented %s times", $key, $offset));
		return $this->memcachedInstance->increment($key,  $offset);
	}

	/**
	* decrements related key from the memcache
	*
	* @access public
	*/
	public function decrement($key, $offset = 1) {
		$this->logDebugMessage(sprintf("%s key decremented %s times", $key, $offset));
		return $this->memcachedInstance->decrement($key,  $offset);
	}

	/**
	* gets running memcached servers.
	*
	* @access public
	* @return array
	*/
	public function getRunningServers() {
		return $this->servers;
	}

	/**
	* array of server statistics, one entry per server.
	*
	* @access public
	* @return array
	*/
	public function getStatistics() {
		return $this->memcachedInstance->getStats();
	}

	/**
	* Invalidates all items from the memcache.
	*
	* @access public
	* @return boolean
	*/
	public function flush($delay = 0) {
		$this->logDebugMessage(sprintf("memcache flushed! (delay: %s)", $delay));
		return $this->memcachedInstance->flush($delay);
	}

	/**
	* logs the memcache actions to the codeigniter's main logging system.
	*
	* @access private
	*/
	private function logDebugMessage($message) {
		log_message("debug", $message);
	}
}

# vim: set noet: