Cache.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace common\logic\Amqp;
  3. use Yii;
  4. /**
  5. * 缓存设置
  6. * Class Cache
  7. * @package common\logic\Amqp
  8. */
  9. class Cache
  10. {
  11. const CACHE_HEADER = 'message:';
  12. const STATUS_SEND_FAIL = -1;//发送失败
  13. const STATUS_SEND_OK = 1;//已发送,待处理
  14. const STATUS_WAIT = 2;//等待中
  15. const STATUS_HAND = 3;//处理中
  16. const STATUS_HAND_OK = 99;//处理成功
  17. const STATUS_HAND_FAIL = -99;//处理失败
  18. public static $statusMark = [
  19. -1 => '发送失败',
  20. 0 => '未知状态',
  21. 1 => '已发送,待处理',
  22. 2 => '等待中',
  23. 3 => '处理中',
  24. 99 => '处理成功',
  25. -99 => '处理失败',
  26. ];
  27. /**
  28. * [设置缓存状态]
  29. * @author: libingke
  30. * @param $key
  31. * @param $value
  32. * @param bool $time
  33. * @return bool
  34. */
  35. public static function setData($key, $value, $time = false)
  36. {
  37. $cache = Yii::$app->redis;
  38. if ($key) {
  39. if (is_numeric($time)) {
  40. return $cache->set(static::CACHE_HEADER . $key, $value, $time);
  41. }
  42. return $cache->set(static::CACHE_HEADER . $key, $value);
  43. }
  44. return false;
  45. }
  46. /**
  47. * [获取缓存状态]
  48. * @author: libingke
  49. * @param $key
  50. * @return bool
  51. */
  52. public static function getData($key)
  53. {
  54. $cache = Yii::$app->redis;
  55. return $cache->get(static::CACHE_HEADER . $key);
  56. }
  57. /**
  58. * [删除缓存状态]
  59. * @author: libingke
  60. * @param $key
  61. * @return mixed
  62. */
  63. public static function deleteData($key)
  64. {
  65. $cache = Yii::$app->redis;
  66. return $cache->delete(static::CACHE_HEADER . $key);
  67. }
  68. /**
  69. * [查询状态]
  70. * @author: libingke
  71. * @param $id
  72. * @return mixed|null
  73. */
  74. public static function getMarkById($id)
  75. {
  76. if (isset(self::$statusMark[$id]) && is_numeric($id)) {
  77. return self::$statusMark[$id];
  78. }
  79. return self::$statusMark[0];
  80. }
  81. }