GameReyunModel.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * GameReyunModel.php UTF-8
  4. * 热云游戏对照表
  5. *
  6. * @date : 2019/4/23 10:09
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace reyun\model;
  13. use huo\model\common\CommonModel;
  14. use huolib\constant\CacheConst;
  15. use think\Cache;
  16. class GameReyunModel extends CommonModel {
  17. protected $name = 'game_reyun';
  18. protected $cache_key_prefix = CacheConst::CACHE_GAME_REYUN_PREFIX;
  19. /**
  20. * 获取单条记录缓存key
  21. *
  22. * @param int $app_id 游戏ID
  23. *
  24. * @return string
  25. */
  26. protected function getSingleCacheKey($app_id) {
  27. return $this->cache_key_prefix.$app_id;
  28. }
  29. /**
  30. * 添加数据
  31. *
  32. * @param array $data 需要添加的数据
  33. *
  34. * @return false|int 添加失败返回 false 添加成功 返回添加的ID
  35. */
  36. public function addData($data) {
  37. if (empty($data)) {
  38. return false;
  39. }
  40. $_data = $data;
  41. $_model = new static();
  42. $_rs = $_model->allowField(true)->isUpdate(false)->save($_data, []);
  43. if (false !== $_rs) {
  44. return $_model->getLastInsID();
  45. }
  46. return false;
  47. }
  48. /**
  49. * 通过ID获取信息
  50. *
  51. * @param int $app_id 主键ID
  52. *
  53. * @return array|false
  54. */
  55. public function getInfoByAppId($app_id) {
  56. /* 缓存操作 */
  57. $_single_cache_key = $this->getSingleCacheKey($app_id);
  58. $_data = Cache::get($_single_cache_key);
  59. if (!empty($_data)) {
  60. return $_data;
  61. }
  62. $_map['app_id'] = $app_id;
  63. $_info = $this->where($_map)->find();
  64. if (false === $_info) {
  65. return [];
  66. }
  67. if (is_object($_info)) {
  68. $_info = $_info->toArray();
  69. }
  70. if (empty($_info)) {
  71. return [];
  72. }
  73. Cache::set($_single_cache_key, $_info);
  74. return $_info;
  75. }
  76. /**
  77. * 更新单条数据
  78. *
  79. * @param array $data 数据
  80. * @param int $app_id 游戏ID
  81. *
  82. * @return bool
  83. */
  84. public function updateDataByAppId($data, $app_id) {
  85. $_data = $data;
  86. $_map['app_id'] = $app_id;
  87. $_rs = $this->allowField(true)->isUpdate(true)->save($_data, $_map);
  88. if (false === $_rs) {
  89. return false;
  90. }
  91. /* 缓存操作 */
  92. $_single_cache_key = $this->getSingleCacheKey($app_id);
  93. Cache::rm($_single_cache_key);
  94. /* TAG缓存操作 */
  95. return true;
  96. }
  97. }