You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
3.7 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage Cache
  7. * @since 2010-03-04
  8. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. class MemcacheCache extends Cache
  12. {
  13. /**
  14. * @var Memcache
  15. */
  16. protected $connection = null;
  17. protected $key_salt = null;
  18. /**
  19. * One hour to live default
  20. *
  21. * @var int
  22. */
  23. protected $expire = 3600;
  24. protected $keys = array();
  25. public function __construct($config)
  26. {
  27. $this->connection = new Memcache();
  28. if (isset($config['key_salt'])) {
  29. $this->key_salt = $config['key_salt'];
  30. unset($config['key_salt']);
  31. }
  32. $required = array('hostname', 'port');
  33. foreach ($config as $c) {
  34. foreach ($required as $option) {
  35. if (!isset($c[$option])) {
  36. throw new Exception('Configuration must have a "' . $option . '".');
  37. }
  38. }
  39. $this->connection->addServer($c['hostname'], $c['port']);
  40. }
  41. }
  42. /**
  43. * Add an item to the cache
  44. *
  45. * @param string $key
  46. * @param mixed $value
  47. * @param int $expire
  48. * @return bool
  49. */
  50. public function add($key, $value, $expire = 0)
  51. {
  52. return $this->connection->add($this->getKey($key), $value, null, $this->getExpire($expire));
  53. }
  54. /**
  55. * Decrement item's value
  56. *
  57. * @param string $key
  58. * @param int $decrement
  59. * @return bool
  60. */
  61. public function decrement($key, $decrement = 1)
  62. {
  63. return $this->connection->decrement($this->getKey($key), $decrement);
  64. }
  65. /**
  66. * Delete item from the cache
  67. *
  68. * @param string $key
  69. * @param int $value
  70. * @return bool
  71. */
  72. public function del($key)
  73. {
  74. return $this->connection->delete($this->getKey($key), 0);
  75. }
  76. /**
  77. * Flush all existing items
  78. *
  79. * @return bool
  80. */
  81. public function flush()
  82. {
  83. return $this->connection->flush();
  84. }
  85. /**
  86. * Retrieve item from the cache
  87. *
  88. * @param string $key
  89. * @return mixed
  90. */
  91. public function get($key)
  92. {
  93. return $this->connection->get($this->getKey($key));
  94. }
  95. /**
  96. * Increment item's value
  97. *
  98. * @param string $key
  99. * @param int $increment
  100. * @return bool
  101. */
  102. public function increment($key, $increment = 1)
  103. {
  104. return $this->connection->increment($this->getKey($key), $increment);
  105. }
  106. /**
  107. * Replace value of the existing item
  108. *
  109. * @param string $key
  110. * @param mixed $var
  111. * @param int $expire
  112. * @return bool
  113. */
  114. public function replace($key, $value, $expire = 0)
  115. {
  116. return $this->connection->replace($this->getKey($key), $value, null, $this->getExpire($expire));
  117. }
  118. /**
  119. * Store data in the cache
  120. *
  121. * @param string $key
  122. * @param mixed $value
  123. * @param int $expire
  124. * @return bool
  125. */
  126. public function set($key, $value, $expire = 0)
  127. {
  128. return $this->connection->set($this->getKey($key), $value, null, $this->getExpire($expire));
  129. }
  130. /**
  131. * @param string $key
  132. * @return string
  133. */
  134. protected function getKey($key)
  135. {
  136. if (!isset($this->keys[$key])) {
  137. $this->keys[$key] = md5($this->key_salt . $key);
  138. }
  139. return $this->keys[$key];
  140. }
  141. public function getExpire($expire)
  142. {
  143. return ($expire > 0) ? $expire : $this->expire;
  144. }
  145. public function cleanKeys()
  146. {
  147. $this->keys = array();
  148. }
  149. }