model = new ShopGoodsSpecRelationModel(); } /** * 静态化入口 * @return static|null */ public static function make() { if(!self::$instance){ self::$instance = new static(); } return self::$instance; } /** * 获取商品对应数据 * @param $goodsId 商品ID * @param string $field 字段 * @param $cache 是否缓存数据,默认是 * @return array|mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getDataByGoods($goodsId, $field='', $cache=true) { $cacheKey = "caches:goodsSpec:relation_{$goodsId}".($field? '_'.md5($field):''); $list = RedisCache::get($cacheKey); if($list && $cache){ return $list; } $where = ['goods_id'=> $goodsId]; $field = $field? $field : 'spec_name,spec_value'; $data = $this->model->where($where)->field($field) ->withAttr('spec_value', function ($value) { return json_decode($value, true); })->select(); $data = $data? $data->toArray():[]; if($data){ RedisCache::set($cacheKey, $data, rand(5,10)); } return $data; } }