心理咨询网
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * @copyright (C)2016-2099 Hnaoyun Inc.
  4. * @author XingMeng
  5. * @email hnxsh@foxmail.com
  6. * @date 2017年10月24日
  7. * Memcache缓存类
  8. */
  9. namespace core\cache;
  10. use core\basic\Config;
  11. class Memcache implements Builder
  12. {
  13. protected static $memcache;
  14. protected $conn;
  15. // 禁止直接实例化
  16. private function __construct()
  17. {}
  18. private function __clone()
  19. {
  20. error('禁止克隆实例!');
  21. }
  22. // 单一实例获取
  23. public static function getInstance()
  24. {
  25. if (! self::$memcache) {
  26. self::$memcache = new self();
  27. }
  28. return self::$memcache;
  29. }
  30. // 初始化连接
  31. protected function conn()
  32. {
  33. if (! $this->conn) {
  34. $this->conn = new Memcache();
  35. $server = Config::get('cache.server');
  36. if (is_multi_array($server)) {
  37. foreach ($server as $value) {
  38. $this->conn->addserver($value['host'], $value['port']);
  39. }
  40. } else {
  41. $this->conn->addserver($server['host'], $server['port']);
  42. }
  43. }
  44. return $this->conn;
  45. }
  46. // 设置值
  47. public function set($key, $value)
  48. {
  49. $memcache = $this->conn();
  50. return $memcache->set($key, $value);
  51. }
  52. // 读取值
  53. public function get($key)
  54. {
  55. $memcache = $this->conn();
  56. return $memcache->get($key);
  57. }
  58. // 删除
  59. public function delete($key)
  60. {
  61. $memcache = $this->conn();
  62. return $memcache->delete($key);
  63. }
  64. // 清理所有
  65. public function flush()
  66. {
  67. $memcache = $this->conn();
  68. return $memcache->flush();
  69. }
  70. // 版本信息
  71. public function status()
  72. {
  73. $memcache = $this->conn();
  74. return $memcache->getExtendedStats();
  75. }
  76. // 关闭连接
  77. public function __destruct()
  78. {
  79. $memcache = $this->conn();
  80. $memcache->close();
  81. }
  82. }