Memcache.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace PhpLife\Frame\Package\Cache;
  3. use PhpLife\Frame\Request;
  4. /**
  5. * 使用Memcache做缓存
  6. */
  7. class Memcache extends \PhpLife\Frame\Package\Cache
  8. {
  9. const CONFIG_CLASS = 'Memcache\\Core';
  10. const PREFIX = '';
  11. const EXPIRE = 259200;
  12. /**
  13. * 单个读取
  14. * @param $id
  15. * @return array
  16. */
  17. public static function get($id)
  18. {
  19. if(Request::isCacheEnabled() == false){
  20. return array();
  21. }
  22. $key = self::getKey($id);
  23. $result = self::getConnection()->get($key);
  24. return $result;
  25. }
  26. /**
  27. * 批量读取,如果没有数据,则不在返回的列表中
  28. * @param array $idList
  29. * @return array
  30. */
  31. public static function getBatch(Array $idList)
  32. {
  33. if(Request::isCacheEnabled() == false){
  34. return array();
  35. }
  36. $keyList = array();
  37. foreach ($idList as $id) {
  38. $keyList[$id] = self::getKey($id);
  39. }
  40. $result = self::getConnection()->getMulti($keyList);
  41. $data = array();
  42. if ($result) {
  43. $keyList = array_flip($keyList);
  44. foreach ($result as $k => $v) {
  45. $k = $keyList[$k];
  46. $data[$k] = $v;
  47. }
  48. }
  49. unset($result);
  50. unset($idList);
  51. unset($keyList);
  52. return $data;
  53. }
  54. /**
  55. * @param $id
  56. * @param $data
  57. * @param expire
  58. * @return bool
  59. */
  60. public static function set($id, $data, $expire = null)
  61. {
  62. $key = self::getKey($id);
  63. $class = get_called_class();
  64. if(is_null($expire)){
  65. $expire = $class::EXPIRE;
  66. }
  67. $expire = time() + intval($expire);
  68. $result = self::getConnection()->set($key, $data, $expire);
  69. return $result;
  70. }
  71. /**
  72. * 批量设置
  73. * @param array $data
  74. * @param $expire
  75. * @return bool
  76. */
  77. public static function setBatch($data, $expire = null)
  78. {
  79. $dataList = array();
  80. foreach ($data as $k => $v) {
  81. $k = self::getKey($k);
  82. $dataList[$k] = $v;
  83. }
  84. $class = get_called_class();
  85. if(is_null($expire)){
  86. $expire = $class::EXPIRE;
  87. }
  88. $expire = time() + intval($expire);
  89. $result = self::getConnection()->setMulti($dataList, $expire);
  90. unset($data);
  91. unset($dataList);
  92. return $result;
  93. }
  94. /**
  95. * 增加
  96. * @param $id
  97. * @param int $value
  98. * @return int
  99. */
  100. public static function increase($id, $value = 1)
  101. {
  102. $key = self::getKey($id);
  103. $value = abs($value);
  104. return self::getConnection()->increment($key, $value);
  105. }
  106. /**
  107. * 减少
  108. * @param $id
  109. * @param int $value
  110. * @return int
  111. */
  112. public static function decrease($id, $value = 1)
  113. {
  114. $key = self::getKey($id);
  115. $value = abs($value);
  116. return self::getConnection()->decrement($key, $value);
  117. }
  118. /**
  119. * 删除
  120. * @param $id
  121. * @return bool
  122. */
  123. public static function delete($id)
  124. {
  125. $key = self::getKey($id);
  126. return self::getConnection()->delete($key);
  127. }
  128. /**
  129. * 获取连接
  130. * @return \Memcached
  131. */
  132. protected static function getConnection()
  133. {
  134. $class = get_called_class();
  135. $configClass = $class::CONFIG_CLASS;
  136. $memcached = new \PhpLife\Frame\Library\Memcache($configClass);
  137. $connection = $memcached->getConnection();
  138. return $connection;
  139. }
  140. }