App.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Api\Service;
  3. use Illuminate\Support\Facades\Cache;
  4. use App\Models\App as Apps;
  5. use Carbon\Carbon;
  6. /**
  7. * API服务端 - App应用相关
  8. *
  9. * @author Flc <2016-7-31 17:47:50>
  10. * @example App::getInstance('10001')->info();
  11. */
  12. class App
  13. {
  14. /**
  15. * appid
  16. * @var [type]
  17. */
  18. protected $app_id;
  19. /**
  20. * 缓存key前缀
  21. * @var string
  22. */
  23. protected $cache_key_prefix = 'love:app:info:';
  24. /**
  25. * 初始化
  26. * @param [type] $app_id [description]
  27. */
  28. public function __construct($app_id)
  29. {
  30. $this->app_id = $app_id;
  31. }
  32. /**
  33. * 获取当前对象
  34. * @param string $app_id appid
  35. * @return object
  36. */
  37. public static function getInstance($app_id)
  38. {
  39. static $_instances = [];
  40. if (array_key_exists($app_id, $_instances))
  41. return $_instances[$app_id];
  42. return $_instances[$app_id] = new self($app_id);
  43. }
  44. /**
  45. * 获取app信息
  46. * @return AppModel
  47. */
  48. public function info()
  49. {
  50. $cache_key = $this->cache_key_prefix . $this->app_id;
  51. if (Cache::has($cache_key)) {
  52. return Cache::get($cache_key);
  53. }
  54. $app = Apps::where('app_id' , $this->app_id)->first();
  55. if ($app)
  56. Cache::put($cache_key, $app, Carbon::now()->addMinutes(60)); // 写入缓存
  57. return $app;
  58. }
  59. }