MemcacheCache.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace Luracast\Restler;
  3. /**
  4. * Class MemcacheCache provides a memcache based cache for Restler
  5. *
  6. * @category Framework
  7. * @package Restler
  8. * @author Dave Drager <ddrager@gmail.com>
  9. * @copyright 2014 Luracast
  10. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  11. * @link http://luracast.com/products/restler/
  12. * @version 3.0.0rc5
  13. */
  14. class MemcacheCache implements iCache
  15. {
  16. /**
  17. * The namespace that all of the cached entries will be stored under. This allows multiple APIs to run concurrently.
  18. *
  19. * @var string
  20. */
  21. static public $namespace;
  22. /**
  23. * @var string the memcache server hostname / IP address. For the memcache
  24. * cache method.
  25. */
  26. static public $memcacheServer = '127.0.0.1';
  27. /**
  28. * @var int the memcache server port. For the memcache cache method.
  29. */
  30. static public $memcachePort = 11211;
  31. private $memcache;
  32. /**
  33. * @param string $namespace
  34. */
  35. function __construct($namespace = 'restler')
  36. {
  37. self::$namespace = $namespace;
  38. if (function_exists('memcache_connect')) {
  39. $this->memcache = new \Memcache;
  40. $this->memcache->connect(self::$memcacheServer, self::$memcachePort);
  41. } else {
  42. $this->memcacheNotAvailable('Memcache is not available for use as Restler Cache. Please make sure the the memcache php extension is installed.');
  43. }
  44. }
  45. /**
  46. * store data in the cache
  47. *
  48. *
  49. * @param string $name
  50. * @param mixed $data
  51. *
  52. * @return boolean true if successful
  53. */
  54. public function set($name, $data)
  55. {
  56. function_exists('memcache_set') || $this->memcacheNotAvailable();
  57. try {
  58. return $this->memcache->set(self::$namespace . "-" . $name, $data);
  59. } catch
  60. (\Exception $exception) {
  61. return false;
  62. }
  63. }
  64. private function memcacheNotAvailable($message = 'Memcache is not available.')
  65. {
  66. throw new \Exception($message);
  67. }
  68. /**
  69. * retrieve data from the cache
  70. *
  71. *
  72. * @param string $name
  73. * @param bool $ignoreErrors
  74. *
  75. * @throws \Exception
  76. * @return mixed
  77. */
  78. public function get($name, $ignoreErrors = false)
  79. {
  80. function_exists('memcache_get') || $this->memcacheNotAvailable();
  81. try {
  82. return $this->memcache->get(self::$namespace . "-" . $name);
  83. } catch (\Exception $exception) {
  84. if (!$ignoreErrors) {
  85. throw $exception;
  86. }
  87. return null;
  88. }
  89. }
  90. /**
  91. * delete data from the cache
  92. *
  93. *
  94. * @param string $name
  95. * @param bool $ignoreErrors
  96. *
  97. * @throws \Exception
  98. * @return boolean true if successful
  99. */
  100. public function clear($name, $ignoreErrors = false)
  101. {
  102. function_exists('memcache_delete') || $this->memcacheNotAvailable();
  103. try {
  104. $this->memcache->delete(self::$namespace . "-" . $name);
  105. } catch (\Exception $exception) {
  106. if (!$ignoreErrors) {
  107. throw $exception;
  108. }
  109. }
  110. }
  111. /**
  112. * check if the given name is cached
  113. *
  114. *
  115. * @param string $name
  116. *
  117. * @return boolean true if cached
  118. */
  119. public function isCached($name)
  120. {
  121. function_exists('memcache_get') || $this->memcacheNotAvailable();
  122. $data = $this->memcache->get(self::$namespace . "-" . $name);
  123. return !empty($data);
  124. }
  125. }