Memcached.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * Kohana_Cache_Memcached class
  4. *
  5. * LICENSE: THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS
  6. * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). THE WORK IS PROTECTED
  7. * BY COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS
  8. * AUTHORIZED UNDER THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED.
  9. *
  10. * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE TO
  11. * BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE MAY BE
  12. * CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED HERE
  13. * IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
  14. *
  15. * @package Kohana/Cache
  16. * @author gimpe <gimpehub@intljaywalkers.com>
  17. * @copyright 2011 International Jaywalkers
  18. * @copyright (c) 2018 Koseven Team
  19. * @license http://creativecommons.org/licenses/by/3.0/ CC BY 3.0
  20. * @link http://github.com/gimpe/kohana-memcached
  21. */
  22. class Kohana_Cache_Memcached extends Cache
  23. {
  24. protected $memcached_instance;
  25. protected function __construct(array $config)
  26. {
  27. if (!extension_loaded('memcached'))
  28. {
  29. // exception missing memcached extension
  30. throw new Kohana_Cache_Exception('memcached extension is not loaded');
  31. }
  32. parent::__construct($config);
  33. $this->memcached_instance = new Memcached;
  34. // load servers from configuration
  35. $servers = Arr::get($this->_config, 'servers', []);
  36. if (empty($servers))
  37. {
  38. // exception no server found
  39. throw new Kohana_Cache_Exception('no Memcached servers in config/cache.php');
  40. }
  41. // load options from configuration
  42. $options = Arr::get($this->_config, 'options', []);
  43. // set options
  44. foreach ($options as $option => $value)
  45. {
  46. if ($option === Memcached::OPT_SERIALIZER && $value === Memcached::SERIALIZER_IGBINARY
  47. && !Memcached::HAVE_IGBINARY)
  48. {
  49. // exception serializer Igbinary not supported
  50. throw new Kohana_Cache_Exception('serializer Igbinary not supported, please fix config/cache.php');
  51. }
  52. if ($option === Memcached::OPT_SERIALIZER && $value === Memcached::SERIALIZER_JSON
  53. && !Memcached::HAVE_JSON)
  54. {
  55. // exception serializer JSON not supported
  56. throw new Kohana_Cache_Exception('serializer JSON not supported, please fix config/cache.php');
  57. }
  58. $this->memcached_instance->setOption($option, $value);
  59. }
  60. // add servers
  61. foreach ($servers as $pos => $server)
  62. {
  63. $host = Arr::get($server, 'host');
  64. $port = Arr::get($server, 'port', NULL);
  65. $weight = Arr::get($server, 'weight', NULL);
  66. $status = Arr::get($server, 'status', TRUE);
  67. if (!empty($host))
  68. {
  69. // status can be used by an external healthcheck to mark the memcached instance offline
  70. if ($status === TRUE)
  71. {
  72. $this->memcached_instance->addServer($host, $port, $weight);
  73. }
  74. }
  75. else
  76. {
  77. // exception no server host
  78. throw new Kohana_Cache_Exception('no host defined for server[' .$pos . '] in config/cache.php');
  79. }
  80. }
  81. }
  82. /**
  83. * Retrieve a cached value entry by id.
  84. *
  85. * // Retrieve cache entry from default group
  86. * $data = Cache::instance()->get('foo');
  87. *
  88. * // Retrieve cache entry from default group and return 'bar' if miss
  89. * $data = Cache::instance()->get('foo', 'bar');
  90. *
  91. * // Retrieve cache entry from memcache group
  92. * $data = Cache::instance('memcache')->get('foo');
  93. *
  94. * @param string id of cache to entry
  95. * @param string default value to return if cache miss
  96. * @return mixed
  97. * @throws Kohana_Cache_Exception
  98. */
  99. public function get($id, $default = NULL)
  100. {
  101. $result = $this->memcached_instance->get($this->_sanitize_id($id));
  102. if ($this->memcached_instance->getResultCode() !== Memcached::RES_SUCCESS)
  103. {
  104. $result = $default;
  105. }
  106. return $result;
  107. }
  108. /**
  109. * Set a value to cache with id and lifetime
  110. *
  111. * $data = 'bar';
  112. *
  113. * // Set 'bar' to 'foo' in default group, using default expiry
  114. * Cache::instance()->set('foo', $data);
  115. *
  116. * // Set 'bar' to 'foo' in default group for 30 seconds
  117. * Cache::instance()->set('foo', $data, 30);
  118. *
  119. * // Set 'bar' to 'foo' in memcache group for 10 minutes
  120. * if (Cache::instance('memcache')->set('foo', $data, 600))
  121. * {
  122. * // Cache was set successfully
  123. * return
  124. * }
  125. *
  126. * @param string id of cache entry
  127. * @param string data to set to cache
  128. * @param integer lifetime in seconds
  129. * @return boolean
  130. */
  131. public function set($id, $data, $lifetime = 3600)
  132. {
  133. return $this->memcached_instance->set($this->_sanitize_id($id), $data, $lifetime);
  134. }
  135. /**
  136. * Delete a cache entry based on id
  137. *
  138. * // Delete 'foo' entry from the default group
  139. * Cache::instance()->delete('foo');
  140. *
  141. * // Delete 'foo' entry from the memcache group
  142. * Cache::instance('memcache')->delete('foo')
  143. *
  144. * @param string id to remove from cache
  145. * @return boolean
  146. */
  147. public function delete($id)
  148. {
  149. return $this->memcached_instance->delete($this->_sanitize_id($id));
  150. }
  151. /**
  152. * Delete all cache entries.
  153. *
  154. * Beware of using this method when
  155. * using shared memory cache systems, as it will wipe every
  156. * entry within the system for all clients.
  157. *
  158. * // Delete all cache entries in the default group
  159. * Cache::instance()->delete_all();
  160. *
  161. * // Delete all cache entries in the memcache group
  162. * Cache::instance('memcache')->delete_all();
  163. *
  164. * @return boolean
  165. */
  166. public function delete_all()
  167. {
  168. return $this->memcached_instance->flush();
  169. }
  170. }