/libraries/memcached_library.php

https://github.com/tomschlick/memcached-library · PHP · 381 lines · 216 code · 55 blank · 110 comment · 40 complexity · e607a25a936a39b9921a63ea28ceedb8 MD5 · raw file

  1. <?php
  2. if (!defined('BASEPATH')) {
  3. exit('No direct script access allowed');
  4. }
  5. class Memcached_library
  6. {
  7. private $config;
  8. private $m;
  9. private $client_type;
  10. private $ci;
  11. protected $errors = [];
  12. public function __construct()
  13. {
  14. $this->ci = &get_instance();
  15. // Load the memcached library config
  16. $this->ci->load->config('memcached');
  17. $this->config = $this->ci->config->item('memcached');
  18. // Lets try to load Memcache or Memcached Class
  19. $this->client_type = class_exists($this->config['config']['engine']) ? $this->config['config']['engine'] : false;
  20. if ($this->client_type) {
  21. // Which one should be loaded
  22. switch ($this->client_type) {
  23. case 'Memcached':
  24. $this->m = new Memcached();
  25. break;
  26. case 'Memcache':
  27. $this->m = new Memcache();
  28. // Set Automatic Compression Settings
  29. if ($this->config['config']['auto_compress_tresh']) {
  30. $this->setcompressthreshold($this->config['config']['auto_compress_tresh'], $this->config['config']['auto_compress_savings']);
  31. }
  32. break;
  33. }
  34. log_message('debug', 'Memcached Library: '.$this->client_type.' Class Loaded');
  35. $this->auto_connect();
  36. } else {
  37. log_message('error', 'Memcached Library: Failed to load Memcached or Memcache Class');
  38. }
  39. }
  40. /*
  41. +-------------------------------------+
  42. Name: auto_connect
  43. Purpose: runs through all of the servers defined in
  44. the configuration and attempts to connect to each
  45. @param return : none
  46. +-------------------------------------+
  47. */
  48. private function auto_connect()
  49. {
  50. foreach ($this->config['servers'] as $key => $server) {
  51. if (!$this->add_server($server)) {
  52. $this->errors[] = "Memcached Library: Could not connect to the server named $key";
  53. log_message('error', 'Memcached Library: Could not connect to the server named "'.$key.'"');
  54. } else {
  55. log_message('debug', 'Memcached Library: Successfully connected to the server named "'.$key.'"');
  56. }
  57. }
  58. }
  59. /*
  60. +-------------------------------------+
  61. Name: add_server
  62. Purpose:
  63. @param return : TRUE or FALSE
  64. +-------------------------------------+
  65. */
  66. public function add_server($server)
  67. {
  68. extract($server);
  69. return $this->m->addServer($host, $port, $weight);
  70. }
  71. /*
  72. +-------------------------------------+
  73. Name: add
  74. Purpose: add an item to the memcache server(s)
  75. @param return : TRUE or FALSE
  76. +-------------------------------------+
  77. */
  78. public function add($key = null, $value = null, $expiration = null)
  79. {
  80. if (is_null($expiration)) {
  81. $expiration = $this->config['config']['expiration'];
  82. }
  83. if (is_array($key)) {
  84. foreach ($key as $multi) {
  85. if (!isset($multi['expiration']) || $multi['expiration'] == '') {
  86. $multi['expiration'] = $this->config['config']['expiration'];
  87. }
  88. $this->add($this->key_name($multi['key']), $multi['value'], $multi['expiration']);
  89. }
  90. } else {
  91. switch ($this->client_type) {
  92. case 'Memcache':
  93. $add_status = $this->m->add($this->key_name($key), $value, $this->config['config']['compression'], $expiration);
  94. break;
  95. default:
  96. case 'Memcached':
  97. $add_status = $this->m->add($this->key_name($key), $value, $expiration);
  98. break;
  99. }
  100. return $add_status;
  101. }
  102. }
  103. /*
  104. +-------------------------------------+
  105. Name: set
  106. Purpose: similar to the add() method but uses set
  107. @param return : TRUE or FALSE
  108. +-------------------------------------+
  109. */
  110. public function set($key = null, $value = null, $expiration = null)
  111. {
  112. if (is_null($expiration)) {
  113. $expiration = $this->config['config']['expiration'];
  114. }
  115. if (is_array($key)) {
  116. foreach ($key as $multi) {
  117. if (!isset($multi['expiration']) || $multi['expiration'] == '') {
  118. $multi['expiration'] = $this->config['config']['expiration'];
  119. }
  120. $this->set($this->key_name($multi['key']), $multi['value'], $multi['expiration']);
  121. }
  122. } else {
  123. switch ($this->client_type) {
  124. case 'Memcache':
  125. $add_status = $this->m->set($this->key_name($key), $value, $this->config['config']['compression'], $expiration);
  126. break;
  127. default:
  128. case 'Memcached':
  129. $add_status = $this->m->set($this->key_name($key), $value, $expiration);
  130. break;
  131. }
  132. return $add_status;
  133. }
  134. }
  135. /*
  136. +-------------------------------------+
  137. Name: get
  138. Purpose: gets the data for a single key or an array of keys
  139. @param return : array of data or multi-dimensional array of data
  140. +-------------------------------------+
  141. */
  142. public function get($key = null)
  143. {
  144. if ($this->m) {
  145. if (is_null($key)) {
  146. $this->errors[] = 'The key value cannot be NULL';
  147. return false;
  148. }
  149. if (is_array($key)) {
  150. foreach ($key as $n => $k) {
  151. $key[$n] = $this->key_name($k);
  152. }
  153. return $this->m->getMulti($key);
  154. } else {
  155. return $this->m->get($this->key_name($key));
  156. }
  157. }
  158. return false;
  159. }
  160. /*
  161. +-------------------------------------+
  162. Name: delete
  163. Purpose: deletes a single or multiple data elements from the memached servers
  164. @param return : none
  165. +-------------------------------------+
  166. */
  167. public function delete($key, $expiration = null)
  168. {
  169. if (is_null($key)) {
  170. $this->errors[] = 'The key value cannot be NULL';
  171. return false;
  172. }
  173. if (is_null($expiration)) {
  174. $expiration = $this->config['config']['delete_expiration'];
  175. }
  176. if (is_array($key)) {
  177. foreach ($key as $multi) {
  178. $this->delete($multi, $expiration);
  179. }
  180. } else {
  181. return $this->m->delete($this->key_name($key), $expiration);
  182. }
  183. }
  184. /*
  185. +-------------------------------------+
  186. Name: replace
  187. Purpose: replaces the value of a key that already exists
  188. @param return : none
  189. +-------------------------------------+
  190. */
  191. public function replace($key = null, $value = null, $expiration = null)
  192. {
  193. if (is_null($expiration)) {
  194. $expiration = $this->config['config']['expiration'];
  195. }
  196. if (is_array($key)) {
  197. foreach ($key as $multi) {
  198. if (!isset($multi['expiration']) || $multi['expiration'] == '') {
  199. $multi['expiration'] = $this->config['config']['expiration'];
  200. }
  201. $this->replace($multi['key'], $multi['value'], $multi['expiration']);
  202. }
  203. } else {
  204. switch ($this->client_type) {
  205. case 'Memcache':
  206. $replace_status = $this->m->replace($this->key_name($key), $value, $this->config['config']['compression'], $expiration);
  207. break;
  208. default:
  209. case 'Memcached':
  210. $replace_status = $this->m->replace($this->key_name($key), $value, $expiration);
  211. break;
  212. }
  213. return $replace_status;
  214. }
  215. }
  216. /*
  217. +-------------------------------------+
  218. Name: increment
  219. Purpose: increments a value
  220. @param return : none
  221. +-------------------------------------+
  222. */
  223. public function increment($key = null, $by = 1)
  224. {
  225. return $this->m->increment($this->key_name($key), $by);
  226. }
  227. /*
  228. +-------------------------------------+
  229. Name: decrement
  230. Purpose: decrements a value
  231. @param return : none
  232. +-------------------------------------+
  233. */
  234. public function decrement($key = null, $by = 1)
  235. {
  236. return $this->m->decrement($this->key_name($key), $by);
  237. }
  238. /*
  239. +-------------------------------------+
  240. Name: flush
  241. Purpose: flushes all items from cache
  242. @param return : none
  243. +-------------------------------------+
  244. */
  245. public function flush()
  246. {
  247. return $this->m->flush();
  248. }
  249. /*
  250. +-------------------------------------+
  251. Name: getversion
  252. Purpose: Get Server Vesion Number
  253. @param Returns a string of server version number or FALSE on failure.
  254. +-------------------------------------+
  255. */
  256. public function getversion()
  257. {
  258. return $this->m->getVersion();
  259. }
  260. /*
  261. +-------------------------------------+
  262. Name: getstats
  263. Purpose: Get Server Stats
  264. Possible: "reset, malloc, maps, cachedump, slabs, items, sizes"
  265. @param returns an associative array with server's statistics. Array keys correspond to stats parameters and values to parameter's values.
  266. +-------------------------------------+
  267. */
  268. public function getstats($type = 'items')
  269. {
  270. switch ($this->client_type) {
  271. case 'Memcache':
  272. $stats = $this->m->getStats($type);
  273. break;
  274. default:
  275. case 'Memcached':
  276. $stats = $this->m->getStats();
  277. break;
  278. }
  279. return $stats;
  280. }
  281. /*
  282. +-------------------------------------+
  283. Name: setcompresstreshold
  284. Purpose: Set When Automatic compression should kick-in
  285. @param return TRUE/FALSE
  286. +-------------------------------------+
  287. */
  288. public function setcompressthreshold($tresh, $savings = 0.2)
  289. {
  290. switch ($this->client_type) {
  291. case 'Memcache':
  292. $setcompressthreshold_status = $this->m->setCompressThreshold($tresh, $savings = 0.2);
  293. break;
  294. default:
  295. $setcompressthreshold_status = true;
  296. break;
  297. }
  298. return $setcompressthreshold_status;
  299. }
  300. /*
  301. +-------------------------------------+
  302. Name: key_name
  303. Purpose: standardizes the key names for memcache instances
  304. @param return : md5 key name
  305. +-------------------------------------+
  306. */
  307. private function key_name($key)
  308. {
  309. return md5(strtolower($this->config['config']['prefix'].$key));
  310. }
  311. /*
  312. +--------------------------------------+
  313. Name: isConected
  314. Purpose: Check if the memcache server is connected.
  315. +--------------------------------------+
  316. */
  317. public function isConnected()
  318. {
  319. foreach ($this->getstats() as $key => $server) {
  320. if ($server['pid'] == -1) {
  321. return false;
  322. }
  323. return true;
  324. }
  325. }
  326. }