GamePayShowCache.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * GamePayShowCache.php UTF-8
  4. * 充值显示规则缓存
  5. *
  6. * @date : 2019/8/27 19:55
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : dengcongshuai <dcs@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\controller\game;
  13. use huo\controller\common\Base;
  14. use huo\logic\game\GamePayShowLogic;
  15. use huolib\constant\CacheConst;
  16. use think\Cache;
  17. class GamePayShowCache extends Base {
  18. public static function ins() {
  19. return new static();
  20. }
  21. /**
  22. * 获取KEY
  23. *
  24. * @param string $app_id
  25. *
  26. * @return string
  27. */
  28. private function getPayShowKey($app_id) {
  29. return CacheConst::CACHE_GAME_PAY_SHOW_PREFIX.$app_id;
  30. }
  31. /**
  32. * 获取信息
  33. *
  34. * @param string $app_id
  35. *
  36. * @return array|bool|mixed
  37. */
  38. public function getInfoByAppId($app_id) {
  39. $_key = $this->getPayShowKey($app_id);
  40. $_data_json = Cache::get($_key);
  41. $_data = json_decode($_data_json, true);
  42. if (!is_array($_data)) {
  43. $_data = $_data_json;
  44. }
  45. if (!is_array($_data) || empty($_data)) {
  46. $_data = (new GamePayShowLogic())->getInfoByAppId($app_id);
  47. if (empty($_data)) {
  48. return false;
  49. }
  50. $this->savePayShowCache($app_id, $_data);
  51. }
  52. return $_data;
  53. }
  54. /**
  55. * 保存cache 数据
  56. *
  57. * @param $app_id
  58. * @param $data
  59. * @param int $ttl
  60. */
  61. public function savePayShowCache($app_id, $data, $ttl = 3600) {
  62. $_key = $this->getPayShowKey($app_id);
  63. Cache::set($_key, json_encode($data), $ttl);
  64. }
  65. /**
  66. * 更新信息
  67. *
  68. * @param string $app_id
  69. * @param array $data
  70. *
  71. * @param bool $rm_cache 是否删除缓存
  72. *
  73. * @return \huo\model\game\GamePayShowModel
  74. */
  75. public function updatePayShow($app_id, $data, $rm_cache = false) {
  76. if (true == $rm_cache) {
  77. $_key = $this->getPayShowKey($app_id);
  78. Cache::rm($_key);
  79. } else {
  80. $_old_data = $this->getInfoByAppId($app_id);
  81. if (!empty($_old_data)) {
  82. $data = array_merge($_old_data, $data);
  83. }
  84. $this->savePayShowCache($app_id, $data);
  85. }
  86. return (new GamePayShowLogic())->updatePaySwitch($data, $app_id);
  87. }
  88. }