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.

159 lines
3.4 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. */
  9. class MemcacheCache extends Cache
  10. {
  11. /**
  12. * @var Memcache
  13. */
  14. protected $connection = null;
  15. /**
  16. * @var null|string
  17. */
  18. protected $key_salt = null;
  19. /**
  20. * One hour to live default
  21. *
  22. * @var int
  23. */
  24. protected $expire = 3600;
  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 InitializationException('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. * @internal 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 $value
  111. * @param int $expire
  112. * @internal param mixed $var
  113. * @return bool
  114. */
  115. public function replace($key, $value, $expire = 0)
  116. {
  117. return $this->connection->replace($this->getKey($key), $value, null, $this->getExpire($expire));
  118. }
  119. /**
  120. * Store data in the cache
  121. *
  122. * @param string $key
  123. * @param mixed $value
  124. * @param int $expire
  125. * @return bool
  126. */
  127. public function set($key, $value, $expire = 0)
  128. {
  129. return $this->connection->set($this->getKey($key), $value, null, $this->getExpire($expire));
  130. }
  131. /**
  132. * @param string $key
  133. * @return string
  134. */
  135. protected function getKey($key)
  136. {
  137. return md5($this->key_salt . $key);
  138. }
  139. public function getExpire($expire)
  140. {
  141. return ($expire > 0) ? $expire : $this->expire;
  142. }
  143. }