Source for file MemcachedStorage.php

Documentation is available at MemcachedStorage.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see http://nettephp.com
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    http://nettephp.com/license  Nette license
  15. 15:  * @link       http://nettephp.com
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Caching
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: require_once dirname(__FILE__'/../Object.php';
  23. 23:  
  24. 24: require_once dirname(__FILE__'/../Caching/ICacheStorage.php';
  25. 25:  
  26. 26:  
  27. 27:  
  28. 28: /**
  29. 29:  * Memcached storage.
  30. 30:  *
  31. 31:  * @author     David Grudl
  32. 32:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  33. 33:  * @package    Nette\Caching
  34. 34:  */
  35. 35: class MemcachedStorage extends Object implements ICacheStorage
  36. 36: {
  37. 37:     /**#@+ @ignore internal cache structure */
  38. 38:     const META_CALLBACKS 'callbacks';
  39. 39:     const META_DATA 'data';
  40. 40:     const META_DELTA 'delta';
  41. 41:     /**#@-*/
  42. 42:  
  43. 43:     /** @var Memcache */
  44. 44:     private $memcache;
  45. 45:  
  46. 46:     /** @var string */
  47. 47:     private $prefix;
  48. 48:  
  49. 49:  
  50. 50:  
  51. 51:     /**
  52. 52:      * Checks if Memcached extension is available.
  53. 53:      * @return bool 
  54. 54:      */
  55. 55:     public static function isAvailable()
  56. 56:     {
  57. 57:         return extension_loaded('memcache');
  58. 58:     }
  59. 59:  
  60. 60:  
  61. 61:  
  62. 62:     public function __construct($host 'localhost'$port 11211$prefix '')
  63. 63:     {
  64. 64:         if (!self::isAvailable()) {
  65. 65:             throw new Exception("PHP extension 'memcache' is not loaded.");
  66. 66:         }
  67. 67:  
  68. 68:         $this->prefix $prefix;
  69. 69:         $this->memcache new Memcache;
  70. 70:         $this->memcache->connect($host$port);
  71. 71:     }
  72. 72:  
  73. 73:  
  74. 74:  
  75. 75:     /**
  76. 76:      * Read from cache.
  77. 77:      * @param  string key
  78. 78:      * @return mixed|NULL
  79. 79:      */
  80. 80:     public function read($key)
  81. 81:     {
  82. 82:         $key $this->prefix $key;
  83. 83:         $meta $this->memcache->get($key);
  84. 84:         if (!$metareturn NULL;
  85. 85:  
  86. 86:         // meta structure:
  87. 87:         // array(
  88. 88:         //     data => stored data
  89. 89:         //     delta => relative (sliding) expiration
  90. 90:         //     callbacks => array of callbacks (function, args)
  91. 91:         // )
  92. 92:  
  93. 93:         // verify dependencies
  94. 94:         if (!empty($meta[self::META_CALLBACKS]&& !Cache::checkCallbacks($meta[self::META_CALLBACKS])) {
  95. 95:             $this->memcache->delete($key);
  96. 96:             return NULL;
  97. 97:         }
  98. 98:  
  99. 99:         if (!empty($meta[self::META_DELTA])) {
  100. 100:             $this->memcache->replace($key$meta0$meta[self::META_DELTAtime());
  101. 101:         }
  102. 102:  
  103. 103:         return $meta[self::META_DATA];
  104. 104:     }
  105. 105:  
  106. 106:  
  107. 107:  
  108. 108:     /**
  109. 109:      * Writes item into the cache.
  110. 110:      * @param  string key
  111. 111:      * @param  mixed  data
  112. 112:      * @param  array  dependencies
  113. 113:      * @return bool  TRUE if no problem
  114. 114:      */
  115. 115:     public function write($key$dataarray $dp)
  116. 116:     {
  117. 117:         if (!empty($dp[Cache::TAGS]|| isset($dp[Cache::PRIORITY]|| !empty($dp[Cache::ITEMS])) {
  118. 118:             throw new NotSupportedException('Tags, priority and dependent items are not supported by MemcachedStorage.');
  119. 119:         }
  120. 120:  
  121. 121:         $meta array(
  122. 122:             self::META_DATA => $data,
  123. 123:         );
  124. 124:  
  125. 125:         $expire 0;
  126. 126:         if (!empty($dp[Cache::EXPIRE])) {
  127. 127:             $expire = (int) $dp[Cache::EXPIRE];
  128. 128:             if (!empty($dp[Cache::SLIDING])) {
  129. 129:                 $meta[self::META_DELTA$expire// sliding time
  130. 130:             }
  131. 131:         }
  132. 132:  
  133. 133:         if (!empty($dp[Cache::CALLBACKS])) {
  134. 134:             $meta[self::META_CALLBACKS$dp[Cache::CALLBACKS];
  135. 135:         }
  136. 136:  
  137. 137:         return $this->memcache->set($this->prefix $key$meta0$expire);
  138. 138:     }
  139. 139:  
  140. 140:  
  141. 141:  
  142. 142:     /**
  143. 143:      * Removes item from the cache.
  144. 144:      * @param  string key
  145. 145:      * @return bool  TRUE if no problem
  146. 146:      */
  147. 147:     public function remove($key)
  148. 148:     {
  149. 149:         return $this->memcache->delete($this->prefix $key);
  150. 150:     }
  151. 151:  
  152. 152:  
  153. 153:  
  154. 154:     /**
  155. 155:      * Removes items from the cache by conditions & garbage collector.
  156. 156:      * @param  array  conditions
  157. 157:      * @return bool  TRUE if no problem
  158. 158:      */
  159. 159:     public function clean(array $conds)
  160. 160:     {
  161. 161:         if (!empty($conds[Cache::ALL])) {
  162. 162:             $this->memcache->flush();
  163. 163:  
  164. 164:         elseif (isset($conds[Cache::TAGS]|| isset($conds[Cache::PRIORITY])) {
  165. 165:             throw new NotSupportedException('Tags and priority is not supported by MemcachedStorage.');
  166. 166:         }
  167. 167:  
  168. 168:         return TRUE;
  169. 169:     }
  170. 170: