Single.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Heanup\Frame\Package\Counter;
  3. /**
  4. * 计数器类
  5. * redis连不上server会抛出异常!!
  6. */
  7. class Single extends \Heanup\Frame\Package\Counter
  8. {
  9. const CONFIG_CLASS = 'Redis\\Counter';
  10. const PREFIX = '';
  11. const EXPIRE = 2592000; // 30 天
  12. /**
  13. * 单个读取
  14. * @param $id
  15. * @return array
  16. */
  17. public static function get($id)
  18. {
  19. $key = self::getKey($id);
  20. try {
  21. $result = self::getConnection()->get($key);
  22. $result = intval($result);
  23. } catch (\Exception $ex) {
  24. $result = null;
  25. }
  26. return $result;
  27. }
  28. /**
  29. * 批量获取
  30. * @param array $idList
  31. * @return array|null
  32. */
  33. public static function mGet(Array $idList)
  34. {
  35. $keyList = array();
  36. foreach ($idList as $id) {
  37. $k = self::getKey($id);
  38. $keyList[$k] = $id;
  39. }
  40. try {
  41. $result = array();
  42. $data = self::getConnection()->mget(array_keys($keyList));
  43. $i = 0;
  44. foreach ($keyList as $k => $v) {
  45. $result[$v] = intval($data[$i]);
  46. $i++;
  47. }
  48. } catch (\Exception $ex) {
  49. $result = null;
  50. }
  51. return $result;
  52. }
  53. /**
  54. * @param $id
  55. * @param int $value
  56. * @return bool
  57. */
  58. public static function set($id, $value)
  59. {
  60. $key = self::getKey($id);
  61. try {
  62. $result = self::getConnection(true)->set($key, $value);
  63. } catch (\Exception $ex) {
  64. $result = false;
  65. }
  66. return $result;
  67. }
  68. /**
  69. * 加1
  70. * @param $id
  71. * @param int $value
  72. * @return int
  73. */
  74. public static function increase($id, $value = 1)
  75. {
  76. $key = self::getKey($id);
  77. $value = abs($value);
  78. try {
  79. $result = self::getConnection(true)->incr($key, $value);
  80. } catch (\Exception $ex) {
  81. $result = false;
  82. }
  83. return $result;
  84. }
  85. /**
  86. * @param $id , 如果值小于0则设为0
  87. * @param int $value
  88. * @return int
  89. */
  90. public static function decrease($id, $value = 1)
  91. {
  92. $key = self::getKey($id);
  93. $value = abs($value);
  94. try {
  95. $result = self::getConnection(true)->decr($key, $value);
  96. if ($result < 0) {
  97. $result = self::getConnection(true)->set($key, 0);
  98. }
  99. } catch (\Exception $ex) {
  100. $result = false;
  101. }
  102. return $result;
  103. }
  104. /**
  105. * 删除
  106. * @param $id
  107. * @return bool
  108. */
  109. public static function delete($id)
  110. {
  111. $key = self::getKey($id);
  112. try {
  113. $result = self::getConnection(true)->delete($key);
  114. } catch (\Exception $ex) {
  115. $result = false;
  116. }
  117. return $result;
  118. }
  119. /**
  120. * 获取连接池
  121. * @param bool $isMaster
  122. * @return \Redis
  123. */
  124. protected static function getConnection($isMaster = false)
  125. {
  126. $class = get_called_class();
  127. $configClass = $class::CONFIG_CLASS;
  128. $redis = new \Heanup\Frame\Library\Redis($configClass);
  129. $connection = $redis->getConnection($isMaster);
  130. return $connection;
  131. }
  132. }