Source of file Cache.php

Size: 3,561 Bytes - Last Modified: 2020-10-24T02:46:31+00:00

/home/travis/build/NextDom/nextdom-core/src/Model/Entity/Cache.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
<?php
/* This file is part of NextDom Software.
 *
 * NextDom 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.
 *
 * NextDom Software 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 NextDom Software. If not, see <http://www.gnu.org/licenses/>.
 */

namespace NextDom\Model\Entity;

use NextDom\Enums\DateFormat;
use NextDom\Helpers\Utils;
use NextDom\Managers\CacheManager;

/**
 * Class Cache
 * @package NextDom\Model\Entity
 */
class Cache
{
    protected $key;
    protected $value = null;
    protected $lifetime = 0;
    protected $datetime;
    protected $options = null;

    /**
     * @return bool
     * @throws \Exception
     */
    public function save()
    {
        $this->setDatetime(date(DateFormat::FULL));
        if ($this->getLifetime() == 0) {
            return CacheManager::getCache()->save($this->getKey(), $this);
        } else {
            return CacheManager::getCache()->save($this->getKey(), $this, $this->getLifetime());
        }
    }

    /**
     * @return int
     */
    public function getLifetime()
    {
        return $this->lifetime;
    }

    /**
     * @param $lifetime
     * @return $this
     */
    public function setLifetime($lifetime)
    {
        $this->lifetime = $lifetime;
        return $this;
    }

    public function getKey()
    {
        return $this->key;
    }

    /**
     * @param $key
     * @return $this
     */
    public function setKey($key)
    {
        $this->key = $key;
        return $this;
    }

    public function remove()
    {
        try {
            CacheManager::getCache()->delete($this->getKey());
        } catch (\Exception $e) {

        }
    }

    /**
     * @return bool
     */
    public function hasExpired()
    {
        return true;
    }

    /**
     * @param string $_default
     * @return null|string
     */
    public function getValue($_default = '')
    {
        return ($this->value === null || (is_string($this->value) && trim($this->value) === '')) ? $_default : $this->value;
    }

    /**
     * @param $value
     * @return $this
     */
    public function setValue($value)
    {
        $this->value = $value;
        return $this;
    }

    public function getDatetime()
    {
        return $this->datetime;
    }

    /**
     * @param $datetime
     * @return $this
     */
    public function setDatetime($datetime)
    {
        $this->datetime = $datetime;
        return $this;
    }

    /**
     * @param string $_key
     * @param string $_default
     * @return array|bool|mixed|null|string
     */
    public function getOptions($_key = '', $_default = '')
    {
        return Utils::getJsonAttr($this->options, $_key, $_default);
    }

    /**
     * @param $_key
     * @param null $_value
     * @return $this
     */
    public function setOptions($_key, $_value = null)
    {
        $this->options = Utils::setJsonAttr($this->options, $_key, $_value);
        return $this;
    }

    /**
     * @param $options
     */
    public function setOptionsFromJson($options)
    {
        $this->options = json_encode($options, JSON_UNESCAPED_UNICODE);
    }
}