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.

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