Memcache.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ f2y0kF37TYFsbk3NVW0SwmVhMLKg/QRKhfEnQ3BrQ2Y=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2017 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.10 [rev.1.10.02]
  11. */
  12. namespace Cube\Cache\Adapter;
  13. class Memcache extends AbstractAdapter
  14. {
  15. /**
  16. *
  17. * host where memcached is listening for connections
  18. *
  19. * @var string
  20. */
  21. public static $host = 'localhost';
  22. /**
  23. * port where memcached is listening for connections
  24. *
  25. * @var string
  26. */
  27. public static $port = '11211';
  28. /**
  29. *
  30. * holds an instance of the memcache object
  31. *
  32. * @var \Memcache
  33. */
  34. private static $_object;
  35. /**
  36. *
  37. * memcache variables namespace/prefix
  38. *
  39. * @var string
  40. */
  41. protected $_namespace;
  42. /**
  43. *
  44. * class constructor
  45. *
  46. * @param array $options configuration array
  47. *
  48. * @throws \RuntimeException
  49. */
  50. public function __construct($options = array())
  51. {
  52. parent::__construct($options);
  53. if (isset($options['namespace'])) {
  54. $this->setNamespace($options['namespace']);
  55. }
  56. if (!self::enabled()) {
  57. throw new \RuntimeException("Memcache cache module is not available.");
  58. }
  59. }
  60. /**
  61. *
  62. * returns an instance of the memcache object and creates it if it wasnt instantiated yet
  63. *
  64. * @return \Memcache
  65. */
  66. public static function getObject()
  67. {
  68. if (!self::$_object instanceof \Memcache) {
  69. if (class_exists('Memcache')) {
  70. $memcache = new \Memcache();
  71. $connect = @$memcache->connect(self::$host, self::$port);
  72. if ($connect !== false) {
  73. $memcache->addServer(self::$host, self::$port);
  74. self::$_object = $memcache;
  75. }
  76. }
  77. }
  78. return self::$_object;
  79. }
  80. /**
  81. *
  82. * get namespace
  83. *
  84. * @return string
  85. */
  86. public function getNamespace()
  87. {
  88. return $this->_namespace;
  89. }
  90. /**
  91. *
  92. * set namespace
  93. *
  94. * @param string $namespace
  95. *
  96. * @return $this
  97. */
  98. public function setNamespace($namespace)
  99. {
  100. $this->_namespace = $namespace;
  101. return $this;
  102. }
  103. /**
  104. *
  105. * check if module is available and enabled
  106. *
  107. * @return bool
  108. */
  109. public static function enabled()
  110. {
  111. return (self::getObject() instanceof \Memcache) ? true : false;
  112. }
  113. /**
  114. *
  115. * reads the contents of a cache file and returns the output or false if the file could not be found
  116. *
  117. * @param string $name
  118. * @param string $type
  119. *
  120. * @return string|false
  121. */
  122. public function read($name, $type)
  123. {
  124. $namespace = $this->getNamespace();
  125. return self::getObject()->get($namespace . $type . $name);
  126. }
  127. /**
  128. *
  129. * create/update cache file
  130. *
  131. * @param string $name
  132. * @param string $type
  133. * @param mixed $data
  134. * @param int $expires
  135. *
  136. * @return $this
  137. * @throws \RuntimeException
  138. */
  139. public function write($name, $type, $data, $expires = null)
  140. {
  141. $namespace = $this->getNamespace();
  142. if ($expires === null) {
  143. $expires = $this->getExpires();
  144. }
  145. $result = self::getObject()
  146. ->add($namespace . $type . $name, $data, false, $expires);
  147. if (!$result) {
  148. throw new \RuntimeException("Memcache add failure.");
  149. }
  150. return $this;
  151. }
  152. /**
  153. *
  154. * delete a variable from cache
  155. *
  156. * @param string $name
  157. * @param string $type
  158. *
  159. * @return boolean
  160. */
  161. public function delete($name, $type)
  162. {
  163. $namespace = $this->getNamespace();
  164. return self::getObject()->delete($namespace . $type . $name);
  165. }
  166. /**
  167. *
  168. * purge cache
  169. * - for memcache cache variables expire automatically so this method does nothing
  170. *
  171. * @param string $type
  172. * @param boolean $force
  173. *
  174. * @return $this
  175. */
  176. public function purge($type, $force = false)
  177. {
  178. return $this;
  179. }
  180. /**
  181. *
  182. * clear cache
  183. *
  184. * @return $this
  185. */
  186. public function clear()
  187. {
  188. self::getObject()->flush();
  189. return $this;
  190. }
  191. }