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.

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