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