Memcached.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace Heanup\Library;
  3. use Exception;
  4. use Heanup\Config\Memcached\Core;
  5. class Memcached
  6. {
  7. private $memcache;
  8. private $type;
  9. private static $instance;
  10. /**
  11. * Memcache constructor.
  12. * @throws Exception
  13. */
  14. public function __construct()
  15. {
  16. $config = Core::getData(null);
  17. $host = $config[0]['host'];
  18. $port = $config[0]['port'];
  19. $this->type = $config[0]['type'] ?: 'memcached';
  20. try {
  21. $this->memcache = new \Memcached;
  22. $this->memcache->setOption(\Memcached::OPT_COMPRESSION, true);
  23. $this->memcache->setOption(\Memcached::OPT_DISTRIBUTION, true);
  24. $this->memcache->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
  25. $this->memcache->setOption(\Memcached::OPT_NO_BLOCK, true);
  26. $this->memcache->setOption(\Memcached::OPT_CONNECT_TIMEOUT, 50);
  27. $this->memcache->setOption(\Memcached::OPT_POLL_TIMEOUT, 50);
  28. $this->memcache->addServer($host, $port);
  29. } catch (Exception $ex) {
  30. $message = "Memcached connection failed : [" . $this->type . ';' . $host . ';' . $port . ']' . $ex->getMessage();
  31. throw new Exception($message, 90301);
  32. }
  33. }
  34. public static function instance()
  35. {
  36. if (!isset(self::$instance)) {
  37. try {
  38. $instance = new self();
  39. } catch (Exception $e) {
  40. }
  41. self::$instance = $instance;
  42. } else {
  43. $instance = self::$instance;
  44. }
  45. return $instance;
  46. }
  47. public function __destruct()
  48. {
  49. if ($this->type != 'memcached') {
  50. $this->memcache->close();
  51. }
  52. }
  53. public function get($key)
  54. {
  55. if (empty($key)) {
  56. return '';
  57. } else {
  58. return $this->memcache->get($key);
  59. }
  60. }
  61. public function delete($key, $time = 0)
  62. {
  63. return $this->memcache->delete($key, $time);
  64. }
  65. public function set($key, $data, $life_time_limit = 0, $memcache_compressed = 2)
  66. {
  67. if ($this->type == 'memcached') {
  68. return $this->memcache->set($key, $data, $life_time_limit);
  69. } else {
  70. return $this->memcache->set($key, $data, $memcache_compressed, $life_time_limit);
  71. }
  72. }
  73. public function replace($key, $data, $memcache_compressed = 2, $life_time_limit = 0)
  74. {
  75. if ($this->type == 'memcached') {
  76. return $this->memcache->replace($key, $data, $life_time_limit);
  77. } else {
  78. return $this->memcache->replace($key, $data, $memcache_compressed, $life_time_limit);
  79. }
  80. }
  81. public function increment($key, $value = 1)
  82. {
  83. return $this->memcache->increment($key, $value);
  84. }
  85. public function decrement($key, $value = 1)
  86. {
  87. return $this->memcache->decrement($key, $value);
  88. }
  89. public function flush()
  90. {
  91. return $this->memcache->flush();
  92. }
  93. }