Redis.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace components\service;
  3. use Yii;
  4. /**
  5. * redis(方便统计队列)
  6. * Class Redis
  7. * @package components\service
  8. */
  9. class Redis
  10. {
  11. const KEYS_STATUS = 'statistic:status';
  12. const TYPE_STATUS = 'status';
  13. const TYPE_RESULT = 'result';
  14. /**
  15. * [设置缓存]
  16. * @author: libingke
  17. * @param $queue
  18. * @param $mid
  19. * @param $type
  20. * @param $val
  21. * @return mixed
  22. */
  23. public static function set($queue, $mid, $type, $val)
  24. {
  25. $key = static::getKey($queue, $mid, $type);
  26. if ($type == static::TYPE_STATUS)
  27. Yii::$app->redis->hmset(static::KEYS_STATUS, $mid, $val);//统计状态
  28. return Yii::$app->redis->set($key, $val);
  29. }
  30. /**
  31. * [获取状态keys]
  32. * @author: libingke
  33. * @return mixed
  34. */
  35. public static function getStatusList()
  36. {
  37. return Yii::$app->redis->hgetall(static::KEYS_STATUS);
  38. }
  39. /**
  40. * [获取缓存]
  41. * @author: libingke
  42. * @param $queue
  43. * @param $mid
  44. * @param $type
  45. * @return mixed
  46. */
  47. public static function get($queue, $mid, $type)
  48. {
  49. $key = static::getKey($queue, $mid, $type);
  50. return Yii::$app->redis->get($key);
  51. }
  52. /**
  53. * [删除缓存]
  54. * @author: libingke
  55. * @param $queue
  56. * @param $mid
  57. * @param $type
  58. * @return mixed
  59. */
  60. public static function del($queue, $mid, $type)
  61. {
  62. $key = static::getKey($queue, $mid, $type);
  63. return Yii::$app->redis->del($key);
  64. }
  65. /**
  66. * [失效设置]
  67. * @author: libingke
  68. * @param $queue
  69. * @param $mid
  70. * @param $type
  71. * @param int $expiration
  72. * @return mixed
  73. */
  74. public static function expire($queue, $mid, $type, $expiration = 86400 * 7)
  75. {
  76. $key = static::getKey($queue, $mid, $type);
  77. return Yii::$app->redis->expire($key, $expiration);
  78. }
  79. /**
  80. * [获取key]
  81. * @author: libingke
  82. * @param $queue
  83. * @param $mid
  84. * @param $type
  85. * @return string
  86. * @throws \Exception
  87. */
  88. public static function getKey($queue, $mid, $type)
  89. {
  90. if ( $queue && $mid && in_array($type, [static::TYPE_STATUS, static::TYPE_RESULT]) )
  91. return "queue:$queue:$mid:$type";
  92. throw new \Exception("参数设置错误");
  93. }
  94. /**
  95. * purge 清空消息
  96. * @author: libingke
  97. * @param $queue
  98. */
  99. public static function purge($queue)
  100. {
  101. }
  102. /**
  103. * 批量删除 key
  104. * @author: libingke
  105. * @param $queue
  106. * @param array $delete
  107. * @param $type
  108. */
  109. public static function batchDel($queue, $delete, $type)
  110. {
  111. }
  112. }