// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\NoticeModel; use App\Models\PriceModel; use App\Services\BaseService; use App\Services\RedisService; /** * 价格服务-服务类 * @author laravel开发员 * @since 2020/11/11 * @package App\Services\Api */ class PriceService extends BaseService { // 静态对象 protected static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * NoticeService constructor. */ public function __construct() { $this->model = new PriceModel(); } /** * 静态入口 */ public static function make() { if (!self::$instance) { self::$instance = new static(); } return self::$instance; } /** * 当前价格 * @param int $type 1-数字资产,2-绿色积分 * @param false $refresh * @return array|mixed|string */ public function getTodayPrice($type=1, $refresh=false) { $cacheKey = "caches:prices:today_price_{$type}"; $data = RedisService::get($cacheKey); $price = isset($data['price'])?$data['price']: '0.00'; if($data && $price && !$refresh){ return $price; } $data = $this->model->where(['type'=>$type,'status'=>1,'mark'=>1]) ->orderBy('date','desc') ->first(); $data = $data?$data->toArray() : []; $price = isset($data['price'])?$data['price']: '0.00'; if($data && $price){ RedisService::set($cacheKey, $data, rand(300,600)); } return $price; } public function updatePrice($type=1) { } }