Cache some calls to Mongo for performance
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Models;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
use App\Traits\ScopeActive;
|
||||
@@ -12,6 +13,7 @@ use App\Traits\ScopeActive;
|
||||
class Domain extends Model
|
||||
{
|
||||
use HasFactory,ScopeActive;
|
||||
private const CACHE_TIME = 3600;
|
||||
|
||||
/* SCOPES */
|
||||
|
||||
@@ -59,14 +61,18 @@ class Domain extends Model
|
||||
if (! $this->echoareas->count())
|
||||
return collect();
|
||||
|
||||
$where = ['echoarea_id'=>$this->echoareas->pluck('id')->toArray()];
|
||||
$key = sprintf('%s_%d','daily_echoarea_stats',$this->id);
|
||||
|
||||
$echostats = Echomail::countGroupBy(['datetime',['datetime'=>'%Y-%m-%d']],$where);
|
||||
return Cache::driver('redis')->remember($key,self::CACHE_TIME,function() {
|
||||
$where = ['echoarea_id'=>$this->echoareas->pluck('id')->toArray()];
|
||||
|
||||
return $echostats
|
||||
->sortBy(function($item) { return $item->id->datetime; })
|
||||
->map(function($item) { return ['x'=>Carbon::createFromFormat('Y-m-d',$item->id->datetime)->timestamp*1000,'y'=>$item->count]; })
|
||||
->values();
|
||||
$echostats = Echomail::countGroupBy(['datetime',['datetime'=>'%Y-%m-%d']],$where);
|
||||
|
||||
return $echostats
|
||||
->sortBy(function($item) { return $item->id->datetime; })
|
||||
->map(function($item) { return ['x'=>Carbon::createFromFormat('Y-m-d',$item->id->datetime)->timestamp*1000,'y'=>$item->count]; })
|
||||
->values();
|
||||
});
|
||||
}
|
||||
|
||||
public function daily_echoarea_stats(Echoarea $o): Collection
|
||||
@@ -74,14 +80,18 @@ class Domain extends Model
|
||||
if (! $this->echoareas->count())
|
||||
return collect();
|
||||
|
||||
$where = ['echoarea_id'=>[$o->id]];
|
||||
$key = sprintf('%s_%d-%d','daily_echoarea_stats',$this->id,$o->id);
|
||||
|
||||
$echostats = Echomail::countGroupBy(['datetime',['datetime'=>'%Y-%m-%d']],$where);
|
||||
return Cache::driver('redis')->remember($key,self::CACHE_TIME,function() use ($o) {
|
||||
$where = ['echoarea_id'=>[$o->id]];
|
||||
|
||||
return $echostats
|
||||
->sortBy(function($item) { return $item->id->datetime; })
|
||||
->map(function($item) { return ['x'=>Carbon::createFromFormat('Y-m-d',$item->id->datetime)->timestamp*1000,'y'=>$item->count]; })
|
||||
->values();
|
||||
$echostats = Echomail::countGroupBy(['datetime',['datetime'=>'%Y-%m-%d']],$where);
|
||||
|
||||
return $echostats
|
||||
->sortBy(function($item) { return $item->id->datetime; })
|
||||
->map(function($item) { return ['x'=>Carbon::createFromFormat('Y-m-d',$item->id->datetime)->timestamp*1000,'y'=>$item->count]; })
|
||||
->values();
|
||||
});
|
||||
}
|
||||
|
||||
public function stats(System $o=NULL): Collection
|
||||
|
Reference in New Issue
Block a user