GamePaySwitchCache.php 2.3 KB

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