| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- namespace ThurData\Servers\KerioEmail\Core\Helper;
- use \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\Exception;
- use \ThurData\Servers\KerioEmail\Core\HandlerError\ErrorCodes\ErrorCodesLib;
- use \ThurData\Servers\KerioEmail\Core\UI\Traits\RequestObjectHandler;
- /**
- * Helper for caching data in database
- * stores json in the $modelClass
- *
- * @autor ThurData <info@thrudata.ch>
- */
- class DatabaseCache
- {
- use RequestObjectHandler;
- /**
- * @var string
- */
- protected $modelClass = '\ThurData\Servers\KerioEmail\Core\Models\ModuleSettings\Model';
- /**
- * @var misc
- * whatever you need to store
- */
- protected $data = null;
- /**
- * @var int
- * timestamp of the last update
- */
- protected $lastDataUpdate = null;
- /**
- * @var int
- * valid period for stored data, after this it will be autoupdated by callback
- * in secounds like a timestamp
- */
- protected $validPeriod = 300;
- /**
- * @var string
- * key for data store
- */
- protected $dataKey = null;
- /**
- * @var \ThurData\Servers\KerioEmail\Core\Models\ModuleSettings\Model
- */
- protected $model = null;
- /**
- * @var callable
- * function returning data for the key
- */
- protected $callback = null;
- protected $assocJsonDecode = false;
- public function __construct($key, $callback, $timeout = 300, $assoc = false, $forceReload = false)
- {
- $this->model = sl($this->modelClass);
- $this->dataKey = $key;
- $this->validPeriod = (int)$timeout;
- $this->callback = $callback;
- $this->assocJsonDecode = $assoc;
- $this->initLoadProcess($forceReload);
- }
- /**
- * wrapper for loading data process
- * @param bool $forceReload
- */
- protected function initLoadProcess($forceReload = false)
- {
- if ($forceReload)
- {
- $this->updateRemoteData();
- return;
- }
- $this->loadDataFromDb();
- if (!$this->isDataValid())
- {
- $this->updateRemoteData();
- }
- }
- /**
- * loads remote data and updates to local storage
- */
- protected function updateRemoteData()
- {
- $data = $this->loadRemoteData();
- $time = \time();
- $this->updateDbCache($data, $time);
- $this->data = $data;
- $this->lastDataUpdate = $time;
- }
- /**
- * updates data in database
- */
- protected function updateDbCache($data, $time)
- {
- $dbData = $this->model->where('setting', $this->dataKey)->first();
- if ($dbData)
- {
- $dbData->update(['value' => json_encode($data)]);
- }
- else
- {
- $this->model->create([
- 'setting' => $this->dataKey,
- 'value' => json_encode($data)
- ]);
- }
- $dbDataTime = $this->model->where('setting', $this->dataKey . '_lastDataUpdate')->first();
- if ($dbDataTime)
- {
- $dbDataTime->update(['value' => $time]);
- }
- else
- {
- $this->model->create([
- 'setting' => $this->dataKey . '_lastDataUpdate',
- 'value' => $time
- ]);
- }
- }
- /**
- * using callback function to load data from custom source
- */
- protected function loadRemoteData()
- {
- if (!is_callable($this->callback))
- {
- throw new Exception(ErrorCodesLib::CORE_CDB_000001, ['callback' => $this->callback]);
- }
- $data = call_user_func_array($this->callback, []);
- return $data;
- }
- /**
- * returns loaded data
- */
- public function getData()
- {
- return $this->data;
- }
- /**
- * static wrapper for creating instance and retriving data
- */
- public static function loadData($key, $callback, $timeout = 300, $assoc = false, $forceReload = false)
- {
- $loader = new DatabaseCache($key, $callback, $timeout, $assoc, $forceReload);
- return $loader->getData();
- }
- /**
- * loads data stored in DB
- */
- protected function loadDataFromDb()
- {
- $dbData = $this->model->where('setting', $this->dataKey)->first();
- if (!$dbData)
- {
- return false;
- }
- $this->data = json_decode($dbData->value, $this->assocJsonDecode);
- $lastUpdate = $this->model->where('setting', $this->dataKey . '_lastDataUpdate')->first();
- if ($lastUpdate)
- {
- $this->lastDataUpdate = (int)$lastUpdate->value;
- }
- }
- /**
- * Check if data is still befeore renewal time
- */
- protected function isDataValid()
- {
- if (!$this->data || !$this->lastDataUpdate)
- {
- return false;
- }
- if (time() > ($this->lastDataUpdate + $this->validPeriod))
- {
- return false;
- }
- return true;
- }
- }
|