CMemUtil.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace loyalsoft;
  3. /*
  4. * 操作内存数据库 Memcache工具类
  5. * version:
  6. * 2016.4.12 (gwang)改造出一个基类
  7. * 2016.4.12 以前的记录无
  8. */
  9. /**
  10. * Description of MemCacheUtil
  11. * CMem工具类[APHP三大操作单元之一]
  12. * @deprecated since version 2017.08.08
  13. * @author jgao,gwang
  14. */
  15. class CMemUtil extends CMemBase
  16. {
  17. //put your code here
  18. public $mem = 0;
  19. public $keyDic = null;
  20. public function conn($host, $port, $pwd = "")
  21. {
  22. $this->keyDic = new \stdClass();
  23. $this->mem = new Memcache();
  24. return $this->mem->connect($host, $port);
  25. }
  26. /**
  27. *
  28. * @param string $key
  29. * @return any
  30. */
  31. public function get($key)
  32. {
  33. $cas_token = "";
  34. if ($GLOBALS['mem_version'] == "3.0") {
  35. $result = memcache_get($this->mem, $key, null, $cas_token);
  36. } else {
  37. $result = memcache_get($this->mem, $key);
  38. }
  39. if (!$result || $result == "" || $result == null) {
  40. return null;
  41. }
  42. if ($GLOBALS['mem_version'] == "3.0") {
  43. $this->keyDic->$key = $cas_token;
  44. }
  45. return JsonUtil::decode($result);
  46. }
  47. public function set($key, $value, $ts = 0)
  48. {
  49. if ($value == null || $value == "") {
  50. return false;
  51. }
  52. return memcache_set($this->mem, $key, JsonUtil::encode($value), MEMCACHE_COMPRESSED, $ts);
  53. }
  54. /**
  55. * 取没有进行json编码的数据
  56. * @param type $key
  57. * @return string
  58. */
  59. public function getWithoutJson($key)
  60. {
  61. $cas_token = "";
  62. if ($GLOBALS['mem_version'] == "3.0") {
  63. $result = memcache_get($this->mem, $key, null, $cas_token);
  64. } else {
  65. $result = memcache_get($this->mem, $key);
  66. }
  67. if (!$result || $result == "" || $result == null) {
  68. return null;
  69. }
  70. if ($GLOBALS['mem_version'] == "3.0") {
  71. $this->keyDic->$key = $cas_token;
  72. }
  73. return $result;
  74. }
  75. /**
  76. * 设置值,内部不加 json_encode
  77. * @param string $key
  78. * @param string $value
  79. * @param int $ts expirets Ps.当ts的值大于一个月的时候将被视为时间戳
  80. * @return boolean
  81. */
  82. public function setWithoutJson($key, $value, $ts = 0)
  83. {
  84. if ($value == null || $value == "") {
  85. return false;
  86. }
  87. return memcache_set($this->mem, $key, JsonUtil::encode($value), MEMCACHE_COMPRESSED, $ts);
  88. }
  89. public function add($key, $value, $ts = 0)
  90. {
  91. if ($value == null || $value == "") {
  92. return false;
  93. }
  94. return memcache_add($this->mem, $key, JsonUtil::encode($value), MEMCACHE_COMPRESSED, $ts);
  95. }
  96. public function replace($key, $value, $ts = 0)
  97. {
  98. if ($value == null || $value == "") {
  99. return false;
  100. }
  101. return memcache_replace($this->mem, $key, JsonUtil::encode($value), MEMCACHE_COMPRESSED, $ts);
  102. }
  103. public function delete($key)
  104. {
  105. return memcache_delete($this->mem, $key);
  106. }
  107. public function increment($key)
  108. {
  109. return memcache_increment($this->mem, $key);
  110. }
  111. public function getMulti($keys)
  112. {
  113. $retArray = array();
  114. foreach ($keys as $key) {
  115. $result = $this->get($key);
  116. if ($result == null) {
  117. $retArray[] = null;
  118. } else {
  119. $retArray[] = $result;
  120. }
  121. }
  122. return $retArray;
  123. }
  124. public function setMutlti($dict, $expireTs = 0)
  125. {
  126. ;
  127. }
  128. public function cas($key, $value, $ts)
  129. {
  130. if ($GLOBALS['mem_version'] == "3.0") {
  131. if ($value == null || $value == "") {
  132. return false;
  133. }
  134. if (!property_exists($this->keyDic, $key)) {
  135. return memcache_set($this->mem, $key, JsonUtil::encode($value), MEMCACHE_COMPRESSED, $ts);
  136. }
  137. $cas_token = $this->keyDic->$key;
  138. return memcache_cas($this->mem, $key, JsonUtil::encode($value), MEMCACHE_COMPRESSED, $ts, $cas_token);
  139. } else {
  140. return $this->set($key, $value, $ts);
  141. }
  142. }
  143. public function copy($surKey, $desKey)
  144. {
  145. return $this->set($desKey, $this->get($surKey));
  146. }
  147. public function close()
  148. {
  149. return memcache_close($this->mem);
  150. }
  151. }