rlock.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace refill;
  3. use Cache;
  4. class rlock
  5. {
  6. //分配策略 1, 关闭 2,预留,3 分配
  7. const CLOSE = 1;
  8. const RETAIN = 2;
  9. const ASSIGN = 3;
  10. const sys_storage_name = 'system_storage';
  11. const mch_storage_name = 'merchant_storage';
  12. const locked_open_name = 'oil_amount_lock_turn';
  13. //system storage
  14. public static function set_open($card_type,$opened)
  15. {
  16. $ins = Cache::getInstance('cacheredis');
  17. $name = self::sys_storage_name;
  18. $key = "{$card_type}-open";
  19. $ins->hset($name, '', [$key => $opened]);
  20. }
  21. public static function get_open($card_type)
  22. {
  23. $ins = Cache::getInstance('cacheredis');
  24. $name = self::sys_storage_name;
  25. $key = "{$card_type}-open";
  26. $value = $ins->hget($name,'',$key);
  27. return $value;
  28. }
  29. public static function incr_sys_storage($card_type,$spec,$count)
  30. {
  31. $ins = Cache::getInstance('cacheredis');
  32. $name = self::sys_storage_name;
  33. $key = "{$card_type}-{$spec}";
  34. return $ins->hIncrBy($name, $key, $spec * $count);
  35. }
  36. public static function decr_sys_storage($card_type,$spec,$count)
  37. {
  38. $ins = Cache::getInstance('cacheredis');
  39. $name = self::sys_storage_name;
  40. $key = "{$card_type}-{$spec}";
  41. return $ins->hIncrBy($name, $key, -1 * $spec * $count);
  42. }
  43. public static function hget_sys_storage($card_type,$spec)
  44. {
  45. $ins = Cache::getInstance('cacheredis');
  46. $name = self::sys_storage_name;
  47. $key = "{$card_type}-{$spec}";
  48. $value = $ins->hget($name, '', $key);
  49. return $value;
  50. }
  51. public static function get_sys()
  52. {
  53. $ins = Cache::getInstance('cacheredis');
  54. $name = self::sys_storage_name;
  55. $value = $ins->hget($name,'');
  56. return $value;
  57. }
  58. public static function clear_all()
  59. {
  60. $ins = Cache::getInstance('cacheredis');
  61. $ins->hdel(self::sys_storage_name,'');
  62. $ins->hdel(self::mch_storage_name,'');
  63. }
  64. //merchant 总金额分配策略开关,及剩余金额变动和获取
  65. public static function incr_mch_total_storage($mchid,$card_type,$amount)
  66. {
  67. $ins = Cache::getInstance('cacheredis');
  68. $name = self::mch_storage_name;
  69. $key = "{$mchid}-{$card_type}-total";
  70. return $ins->hIncrBy($name, $key, $amount);
  71. }
  72. public static function decr_mch_total_storage($mchid,$card_type,$amount)
  73. {
  74. $ins = Cache::getInstance('cacheredis');
  75. $name = self::mch_storage_name;
  76. $key = "{$mchid}-{$card_type}-total";
  77. return $ins->hIncrBy($name, $key, -1 * $amount);
  78. }
  79. public static function hget_mch_total_storage($mchid,$card_type)
  80. {
  81. $ins = Cache::getInstance('cacheredis');
  82. $name = self::mch_storage_name;
  83. $key = "{$mchid}-{$card_type}-total";
  84. $value = $ins->hget($name, '', $key);
  85. return $value;
  86. }
  87. public static function hset_mch_total_storage_turn($mchid,$card_type,$val)
  88. {
  89. $ins = Cache::getInstance('cacheredis');
  90. $name = self::mch_storage_name;
  91. $key = "{$mchid}-{$card_type}-turn";
  92. $ins->hset($name, '', [$key => $val]);
  93. }
  94. public static function hget_mch_total_storage_turn($mchid,$card_type)
  95. {
  96. $ins = Cache::getInstance('cacheredis');
  97. $name = self::mch_storage_name;
  98. $key = "{$mchid}-{$card_type}-turn";
  99. return $ins->hget($name, '', $key);
  100. }
  101. //mchid storage count-lock
  102. public static function incr_mch_storage($mchid,$card_type,$spec,$count)
  103. {
  104. $ins = Cache::getInstance('cacheredis');
  105. $name = self::mch_storage_name;
  106. $key = "{$mchid}-{$card_type}-{$spec}";
  107. return $ins->hIncrBy($name, $key, $spec * $count);
  108. }
  109. public static function decr_mch_storage($mchid,$card_type,$spec,$count)
  110. {
  111. $ins = Cache::getInstance('cacheredis');
  112. $name = self::mch_storage_name;
  113. $key = "{$mchid}-{$card_type}-{$spec}";
  114. return $ins->hIncrBy($name, $key, -1 * $spec * $count);
  115. }
  116. public static function hget_mch_storage($mchid,$card_type,$spec)
  117. {
  118. $ins = Cache::getInstance('cacheredis');
  119. $name = self::mch_storage_name;
  120. $key = "{$mchid}-{$card_type}-{$spec}";
  121. $value = $ins->hget($name, '', $key);
  122. return $value;
  123. }
  124. public static function hset_mch_storage_turn($mchid,$card_type,$spec,$val)
  125. {
  126. $ins = Cache::getInstance('cacheredis');
  127. $name = self::mch_storage_name;
  128. $key = "{$mchid}-{$card_type}-{$spec}-turn";
  129. $ins->hset($name, '', [$key => $val]);
  130. }
  131. public static function hget_mch_storage_turn($mchid,$card_type,$spec)
  132. {
  133. $ins = Cache::getInstance('cacheredis');
  134. $name = self::mch_storage_name;
  135. $key = "{$mchid}-{$card_type}-{$spec}-turn";
  136. return $ins->hget($name, '', $key);
  137. }
  138. public static function get_merchant()
  139. {
  140. $ins = Cache::getInstance('cacheredis');
  141. $name = self::mch_storage_name;
  142. $value = $ins->hget($name,'');
  143. return $value;
  144. }
  145. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  146. }