Hash.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace PhpLife\Frame\Package\Counter;
  3. /**
  4. * 计数器类,使用hash数据结构
  5. * redis连不上server会抛出异常!!
  6. */
  7. class Hash extends \PhpLife\Frame\Package\Counter
  8. {
  9. const CONFIG_CLASS = 'Redis\\Counter';
  10. const PREFIX = '';
  11. const EXPIRE = 2592000; // 30 天
  12. protected static $hashKeyList = array();
  13. /**
  14. * 批量获取一系列的id
  15. * @param $idList
  16. * @return array
  17. */
  18. public static function hBatchGetAll($idList)
  19. {
  20. $data = array();
  21. foreach ($idList as $id) {
  22. $data[$id] = self::hGetAll($id);
  23. }
  24. return $data;
  25. }
  26. /**
  27. * 读取单个hash
  28. * @param $id
  29. * @return array
  30. */
  31. public static function hGetAll($id)
  32. {
  33. $key = self::getKey($id);
  34. try {
  35. $result = self::getConnection()->hMGet($key, self::getHashKeyList());
  36. foreach ($result as $k => $v) {
  37. $result[$k] = intval($v);
  38. }
  39. } catch (\Exception $ex) {
  40. $result = null;
  41. }
  42. return $result;
  43. }
  44. /**
  45. * @param $id
  46. * @param $hashKey
  47. * @param int $value
  48. * @return bool
  49. */
  50. public static function hSet($id, $hashKey, $value)
  51. {
  52. $key = self::getKey($id);
  53. try {
  54. $result = self::getConnection(true)->hSet($key, $hashKey, $value);
  55. } catch (\Exception $ex) {
  56. $result = false;
  57. }
  58. return $result;
  59. }
  60. /**
  61. * 加1
  62. * @param $id
  63. * @param $hashKey
  64. * @param int $value 负数时减少
  65. * @return int
  66. */
  67. public static function hIncrBy($id, $hashKey, $value = 1)
  68. {
  69. $key = self::getKey($id);
  70. try {
  71. $result = self::getConnection(true)->hIncrBy($key, $hashKey, $value);
  72. } catch (\Exception $ex) {
  73. $result = false;
  74. }
  75. return $result;
  76. }
  77. /**
  78. * 删除
  79. * @param $id
  80. * @return bool
  81. */
  82. public static function delete($id)
  83. {
  84. $key = self::getKey($id);
  85. try {
  86. $result = self::getConnection(true)->delete($key);
  87. } catch (\Exception $ex) {
  88. $result = false;
  89. }
  90. return $result;
  91. }
  92. /**
  93. * 设置过期时间
  94. * @param $id
  95. * @param $expire
  96. * @return mixed
  97. */
  98. public static function setExpire($id, $expire = null)
  99. {
  100. if (is_null($expire)) {
  101. $class = get_called_class();
  102. $expire = $class::EXPIRE;
  103. } else {
  104. $expire = intval($expire);
  105. }
  106. $key = self::getKey($id);
  107. try {
  108. $result = self::getConnection(true)->expire($key, $expire);
  109. } catch (\Exception $ex) {
  110. $result = false;
  111. }
  112. return $result;
  113. }
  114. /**
  115. * 获取hashKey列表
  116. * @return mixed
  117. */
  118. protected static function getHashKeyList()
  119. {
  120. $class = get_called_class();
  121. $hashKeyList = $class::$hashKeyList;
  122. return $hashKeyList;
  123. }
  124. /**
  125. * 获取连接池
  126. * @param bool $isMaster
  127. * @return \Redis
  128. */
  129. protected static function getConnection($isMaster = false)
  130. {
  131. $class = get_called_class();
  132. $configClass = $class::CONFIG_CLASS;
  133. $redis = new \PhpLife\Frame\Library\Redis($configClass);
  134. $connection = $redis->getConnection($isMaster);
  135. return $connection;
  136. }
  137. }