1
0
mirror of https://github.com/devcode-it/openstamanager.git synced 2025-01-27 14:09:41 +01:00
openstamanager/src/Hooks/CachedManager.php

66 lines
1.6 KiB
PHP
Raw Normal View History

2019-08-26 18:02:05 +02: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/>.
*/
2019-08-26 18:02:05 +02:00
namespace Hooks;
2020-03-03 10:31:01 +01:00
use Models\Cache;
use Models\Hook;
2019-08-26 18:02:05 +02:00
abstract class CachedManager extends Manager
{
2024-01-15 15:30:45 +01:00
protected $cache;
2019-08-26 18:02:05 +02:00
2020-03-03 10:31:01 +01:00
public function __construct(Hook $hook)
2019-08-29 11:25:13 +02:00
{
2020-03-03 10:31:01 +01:00
parent::__construct($hook);
$this->cache = Cache::pool($this->getCacheName());
2019-08-29 11:25:13 +02:00
}
2020-03-03 10:31:01 +01:00
abstract public function cacheData();
2019-08-27 15:42:13 +02:00
2020-03-03 10:31:01 +01:00
abstract public function getCacheName();
2019-08-27 15:42:13 +02:00
2020-03-03 10:31:01 +01:00
public function needsExecution()
{
return !$this->isCached();
2019-08-27 15:42:13 +02:00
}
2019-08-26 18:02:05 +02:00
2020-03-03 10:31:01 +01:00
public function execute()
2019-08-26 18:02:05 +02:00
{
2020-03-03 10:31:01 +01:00
if (!$this->isCached()) {
$data = $this->cacheData();
2019-08-29 11:25:13 +02:00
2020-03-03 10:31:01 +01:00
$this->getCache()->set($data);
2019-08-26 18:02:05 +02:00
}
2020-03-03 10:31:01 +01:00
return $this->getCache()->content;
2019-08-26 18:02:05 +02:00
}
2020-03-03 10:31:01 +01:00
public function getCache()
2019-08-26 18:02:05 +02:00
{
2020-03-03 10:31:01 +01:00
return $this->cache;
2019-08-26 18:02:05 +02:00
}
2020-03-03 10:31:01 +01:00
public function isCached()
2019-08-26 18:02:05 +02:00
{
2020-03-03 10:31:01 +01:00
return $this->getCache()->isValid();
2019-08-26 18:02:05 +02:00
}
}