PageRenderTime 35ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/cache/memcache.php

https://github.com/MilkZoft/zan
PHP | 155 lines | 125 code | 30 blank | 0 comment | 14 complexity | 8a5ad95e4709092f3fd83723a920ef18 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. if (!defined("ACCESS")) {
  3. die("Error: You don't have permission to access here...");
  4. }
  5. if (!function_exists("memcache_connect")) {
  6. die("Memcache extension doesn't exists");
  7. }
  8. class ZP_Memcache extends ZP_Load
  9. {
  10. private $Memcache = null;
  11. public function add($key, $value, $flag = false, $expire = 30)
  12. {
  13. if (!CACHE_STATUS) {
  14. return false;
  15. }
  16. if (is_object($this->Memcache)) {
  17. $this->Memcache->add($key, $value, $flag, $expire);
  18. return true;
  19. }
  20. return false;
  21. }
  22. public function addServer($host, $port = 11211)
  23. {
  24. if (!CACHE_STATUS) {
  25. return false;
  26. }
  27. $Memcache = new Memcache;
  28. $Memcache->addServer($host, $port);
  29. return true;
  30. }
  31. public function close()
  32. {
  33. if (!CACHE_STATUS) {
  34. return false;
  35. }
  36. if (is_object($this->Memcache)) {
  37. $this->Memcache->close();
  38. }
  39. return true;
  40. }
  41. public function connect()
  42. {
  43. if (!CACHE_STATUS) {
  44. return false;
  45. }
  46. if (is_null($this->Memcache)) {
  47. $this->Memcache = new Memcache;
  48. $this->Memcache->connect(_cacheServer, _cachePort);
  49. }
  50. return true;
  51. }
  52. public function clear()
  53. {
  54. if (!CACHE_STATUS) {
  55. return false;
  56. }
  57. if (is_object($this->Memcache)) {
  58. $this->Memcache->flush();
  59. $time = time() + 1;
  60. while(time() < $time) {}
  61. }
  62. }
  63. public function decrement($key, $value = 1)
  64. {
  65. if (!CACHE_STATUS) {
  66. return false;
  67. }
  68. return is_object($this->Memcache) ? $this->Memcache->decrement($key, $value) : false;
  69. }
  70. public function increment($key, $value = 1)
  71. {
  72. return is_object($this->Memcache) ? $this->Memcache->increment($key, $value) : false;
  73. }
  74. public function fetch($key)
  75. {
  76. return (CACHE_STATUS and is_object($this->Memcache)) ? $this->Memcache->get($key) : false;
  77. }
  78. public function serverStatus($host, $port = 11211)
  79. {
  80. return (CACHE_STATUS and is_object($this->Memcache)) ? $this->Memcache->getServerStatus($host, $port) : false;
  81. }
  82. public function stats($type, $slabid, $limit = 100)
  83. {
  84. return (CACHE_STATUS and is_object($this->Memcache)) ? $this->Memcache->getStats($type, $slabid, $limit) : false;
  85. }
  86. public function version()
  87. {
  88. return (CACHE_STATUS and is_object($this->Memcache)) ? $this->Memcache->getVersion() : false;
  89. }
  90. public function pConnect()
  91. {
  92. if (!CACHE_STATUS) {
  93. return false;
  94. }
  95. if (is_null($this->Memcache)) {
  96. $this->Memcache = new Memcache;
  97. $this->Memcache->pconnect(_cacheServer, _cachePort);
  98. }
  99. return true;
  100. }
  101. public function replace($key, $value, $flag = false, $expire = 30)
  102. {
  103. return (CACHE_STATUS and is_object($this->Memcache)) ? $this->Memcache->replace($key, $value, $flag, $expire) : false;
  104. }
  105. public function set($key, $value, $flag = false, $expire = 30)
  106. {
  107. return (CACHE_STATUS and is_object($this->Memcache)) ? $this->Memcache->set($key, $value, $flag, $expire) : false;
  108. }
  109. public function compressThreshold($threshold = 20000, $savings = 0.2)
  110. {
  111. return (CACHE_STATUS and is_object($this->Memcache)) ? $this->Memcache->setCompressThreshold($threshold, $savings) : false;
  112. }
  113. public function delete($key)
  114. {
  115. return (CACHE_STATUS and is_object($this->Memcache)) ? $this->Memcache->delete($key) : false;
  116. }
  117. public function serverParams($host, $port = 11211, $timeout, $interval = false, $status, $callback)
  118. {
  119. if (CACHE_STATUS and is_object($this->Memcache)) {
  120. $this->Memcache->setServerParams($host, $port, $timeout, $interval, $status, $callback);
  121. }
  122. return false;
  123. }
  124. }