2020-03-03 10:31:01 +01:00
|
|
|
<?php
|
2020-09-07 15:04:06 +02:00
|
|
|
/*
|
|
|
|
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
|
2021-01-20 15:08:51 +01:00
|
|
|
* Copyright (C) DevCode s.r.l.
|
2020-09-07 15:04:06 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-03-03 10:31:01 +01:00
|
|
|
|
|
|
|
namespace Models;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Carbon\CarbonInterval;
|
2020-09-22 20:28:37 +02:00
|
|
|
use Common\SimpleModelTrait;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2020-09-23 13:36:37 +02:00
|
|
|
use Traits\LocalPoolTrait;
|
2020-03-03 10:31:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Risorsa di cache per la gestione delle informazioni temporanee del gestionale.
|
|
|
|
* Da utilizzare in sola lettura oppure in creazione e diretta scritture.
|
|
|
|
*/
|
|
|
|
class Cache extends Model
|
|
|
|
{
|
2020-09-22 20:28:37 +02:00
|
|
|
use SimpleModelTrait;
|
2020-09-23 13:36:37 +02:00
|
|
|
use LocalPoolTrait;
|
2020-03-03 10:31:01 +01:00
|
|
|
|
|
|
|
protected $table = 'zz_cache';
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
'content' => 'array',
|
|
|
|
];
|
|
|
|
|
|
|
|
protected $dates = [
|
|
|
|
'expire_at',
|
|
|
|
];
|
|
|
|
|
|
|
|
public static function build($name, $valid_time, $expire_at = null)
|
|
|
|
{
|
|
|
|
$model = new self();
|
|
|
|
|
|
|
|
$model->name = $name;
|
|
|
|
$model->valid_time = $valid_time;
|
|
|
|
$model->expire_at = $expire_at;
|
|
|
|
|
|
|
|
$model->save();
|
|
|
|
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isValid()
|
|
|
|
{
|
|
|
|
return $this->valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getValidAttribute()
|
|
|
|
{
|
|
|
|
return !empty($this->expire_at) && $this->expire_at->greaterThanOrEqualTo(Carbon::now());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function set($content)
|
|
|
|
{
|
|
|
|
$this->content = $content;
|
|
|
|
|
|
|
|
return $this->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function save(array $options = [])
|
|
|
|
{
|
|
|
|
if (!empty($this->valid_time)) {
|
|
|
|
$interval = CarbonInterval::make($this->valid_time);
|
|
|
|
$this->expire_at = (new Carbon())->add($interval);
|
|
|
|
} elseif (empty($this->expire_at)) {
|
|
|
|
$interval = CarbonInterval::make('6 hours');
|
|
|
|
$this->expire_at = (new Carbon())->add($interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::save($options);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete()
|
|
|
|
{
|
|
|
|
if (empty($this->valid_time)) {
|
|
|
|
return parent::delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scopeValid($query)
|
|
|
|
{
|
|
|
|
return $query->where('expire_at', '>', Carbon::now());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scopeInvalid($query)
|
|
|
|
{
|
|
|
|
return $query->where('expire_at', '<=', Carbon::now());
|
|
|
|
}
|
|
|
|
}
|