HeartTimeCache.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * HeartTimeCache.php UTF-8
  4. * 心跳时间缓存
  5. *
  6. * @date : 2019/11/30 15:12
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : HUOSDK 8.5
  11. */
  12. namespace huoIdentify\controller;
  13. use huo\controller\common\Base;
  14. use huolib\constant\CacheConst;
  15. use huolib\constant\CommonConst;
  16. use think\Cache;
  17. class HeartTimeCache extends Base {
  18. protected $cache_key_prefix = CacheConst::CACHE_LAST_HEART_TIME_PREFIX; //缓存前缀
  19. public static function ins() {
  20. return new static();
  21. }
  22. /***
  23. * 获取缓存前缀
  24. *
  25. * @param int $app_id 游戏id
  26. * @param int $mem_id 玩家id
  27. * @param string $device_id 设备ID android 为imei ios 为idfa h5 为 永久cookie标识 huoh5开头标识
  28. *
  29. * @return string
  30. */
  31. public function getCacheKeyByAppMemDevice($app_id, $mem_id, $device_id) {
  32. return $this->cache_key_prefix.'app_'.$app_id.'_mem_'.$mem_id.'_device_'.$device_id;
  33. }
  34. /**
  35. * 获取最后一次心跳时间
  36. *
  37. * @param int $app_id 游戏id
  38. * @param int $mem_id 玩家id
  39. * @param string $device_id 设备ID android 为imei ios 为idfa h5 为 永久cookie标识 huoh5开头标识
  40. *
  41. * @return int|mixed
  42. */
  43. public function getLastHeartTime($app_id, $mem_id, $device_id) {
  44. $_cache_key = $this->getCacheKeyByAppMemDevice($app_id, $mem_id, $device_id);
  45. $_time = Cache::get($_cache_key);
  46. if (empty($_time)) {
  47. return CommonConst::CONST_ZERO;
  48. }
  49. return $_time;
  50. }
  51. /**
  52. * 设置最后一次心跳时间
  53. *
  54. * @param int $time
  55. * @param int $app_id 游戏id
  56. * @param int $mem_id 玩家id
  57. * @param string $device_id 设备ID android 为imei ios 为idfa h5 为 永久cookie标识 huoh5开头标识
  58. */
  59. public function setLastHeartTime($time, $app_id, $mem_id, $device_id) {
  60. $_cache_key = $this->getCacheKeyByAppMemDevice($app_id, $mem_id, $device_id);
  61. Cache::set($_cache_key, $time);
  62. }
  63. }