LD 5 лет назад
Родитель
Сommit
288937c60c

+ 18 - 0
app/Console/Commands/MakeModelCommand.php

@@ -13,5 +13,23 @@ class MakeModelCommand extends \Illuminate\Foundation\Console\ModelMakeCommand
         }
         return __DIR__ . '/stubs/model.stub';
     }
+    public function handle()
+    {
+        if (parent::handle() === false && ! $this->option('force')) {
+            return false;
+        }
+
+        if ($this->option('all')) {
+            $this->input->setOption('service', true);
+        }
+
+        if ($this->option('service')) {
+            $this->createService();
+        }
+    }
+    public function createService()
+    {
+
+    }
 
 }

+ 1 - 2
app/Http/Controllers/TestController.php

@@ -291,8 +291,7 @@ sql;
 
     function tlog(Request $request)
     {
-        $user = User::query()->first();
-        $user->update();
+        Cache::forget('order_freeze_condition_pool');
     }
 
     function setCache(Request $request)

+ 4 - 0
app/Services/MaterialBoxService.php

@@ -13,6 +13,10 @@ use App\Traits\ServiceAppAop;
 class MaterialBoxService
 {
     use ServiceAppAop;
+    function getModelClass(): string
+    {
+        return MaterialBox::class;
+    }
 
     function get(array $kvPairs):?Collection{
         return Cache::remember($this->cachingKey($kvPairs), config('cache.expirations.rarelyChange'), function ()use($kvPairs) {

+ 55 - 0
app/Traits/ServiceAppAop.php

@@ -4,10 +4,17 @@
 namespace App\Traits;
 
 
+use App\Exceptions\FatalException;
+use App\MaterialBox;
 use Illuminate\Contracts\Foundation\Application;
+use Illuminate\Support\Collection;
+use Illuminate\Support\Facades\Cache;
 
 trait ServiceAppAop
 {
+
+    protected $modelClass=null;
+
     /**
      * 实例化一个service的入口,为了测试方便做出来的,这样测试时可以代入假的柱件替换service的实现
      * 应当在一般性用app的地方换成本方法才可以做到使用柱件
@@ -28,4 +35,52 @@ trait ServiceAppAop
         }
         return $targetService;
     }
+    /**
+     * 缓存过了
+     * @param array $kvPairs
+     * @return Collection|null
+     */
+    function get(array $kvPairs):?Collection{
+        return Cache::remember($this->cachingKey($kvPairs), config('cache.expirations.serviceAutoCache'), function ()use($kvPairs) {
+            $query = $this->getModelClass()::query();
+            foreach ($kvPairs as $column => $value){
+                if (is_array($value))$query->whereIn($column,$value);
+                else $query->where($column,$value);
+            }
+            return $query->get();
+        });
+    }
+
+    protected function getModelClass(){
+        if(!$this->modelClass){
+            throw new FatalException(__CLASS__.': 没有为 service 设定 $modelClass 的模型类');
+        }
+        return $this->modelClass;
+    }
+
+    protected function cachingKey(array $kvPairs): string
+    {
+        ksort($kvPairs);
+        return 'MaterialBox_'.md5(json_encode($kvPairs));
+    }
+
+    protected function clearCache(array $kvPairs){
+        Cache::forget($this->cachingKey($kvPairs));
+    }
+
+    function create(array $kvPairs){
+        $materialBox=$this->get($kvPairs);
+        if($materialBox->isNotEmpty())
+            return $materialBox->first();
+        $this->clearCache($kvPairs);
+        return $this->getModelClass()::query()->create($kvPairs);
+    }
+
+    function firstOrCreate(array $kvPairs){
+        $materialBox=$this->get($kvPairs);
+        if($materialBox->isNotEmpty())
+            return $materialBox->first();
+        $this->clearCache($kvPairs);
+        return $this->create($kvPairs);
+    }
 }

+ 2 - 0
config/cache.php

@@ -12,6 +12,8 @@ return [
         'commonFrequent'=>200,  //一般频率
         'forever' =>null,       //永久
 
+        'serviceAutoCache'=>300,           //服务中自动缓存
+
         'owners'=>20,           //模型Owner
         'orderCountingRecord'=>1800,           //模型Owner
         'logisticsCountingRecords'=>86400,           //快递饼图缓存时间