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.

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