| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- namespace ThurData\Servers\KerioEmail\Core\Cache;
- use Symfony\Component\Cache\Simple\FilesystemCache;
- use ThurData\Servers\KerioEmail\Core\ModuleConstants;
- use ThurData\Servers\KerioEmail\Core\Interfaces\CacheManagerInterface;
- /**
- * Description of CacheManager
- *
- * @author Rafał Ossowski <rafal.os@thurdata.com>
- */
- class CacheManager implements CacheManagerInterface
- {
- protected static $instance = null;
- /**
- * @var CacheManagerInterface
- */
- protected $manager;
-
- /**
- * @var string
- */
- protected $dir = '';
-
- /**
- * @var string
- */
- protected $namespace = '';
-
- /**
- * @var string
- */
- protected $key;
-
- /**
- * @var string|array|object
- */
- protected $data;
- public function __construct()
- {
- $this->namespace = ModuleConstants::getRootNamespace();
- $this->dir = ModuleConstants::getModuleRootDir() . DS . 'storage' . DS . 'framework';
- $this->manager = new FilesystemCache('', 0, $this->dir);
- }
-
- private function __clone()
- {
-
- }
- /**
- * @param string $key
- * @return $this
- * @throws \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\MGModuleException
- */
- public function setKey($key = 'default')
- {
- try
- {
- $this->key = $key;
- $this->setData($this->manager->get($this->key));
- }
- catch (\Exception $ex)
- {
- throw new \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\MGModuleException(self::class, $ex->getMessage(), $ex->getCode(), $ex);
- }
-
-
- return $this;
- }
-
- /**
- * @return string
- */
- public function getKey()
- {
- return $this->key;
- }
-
- /**
- * @param mixed $data
- * @return $this
- */
- public function setData($data = null)
- {
- if ($data !== null)
- {
- $this->data = $data;
- }
-
- return $this;
- }
-
- /**
- * @return mixed
- * @throws \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\MGModuleException
- */
- public function getData()
- {
- try
- {
- if (isset($this->key))
- {
- $this->data = $this->manager->get($this->key);
- }
- return $this->data;
- }
- catch (\Exception $ex)
- {
- throw new \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\MGModuleException(self::class, $ex->getMessage(), $ex->getCode(), $ex);
- }
-
- }
-
- /**
- * @return $this
- * @throws \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\MGModuleException
- */
- public function save()
- {
- try
- {
- $this->manager->set($this->key, $this->data);
- }
- catch (\Exception $ex)
- {
- throw new \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\MGModuleException(self::class, $ex->getMessage(), $ex->getCode(), $ex);
- }
- return $this;
- }
-
- /**
- * @return $this
- * @throws \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\MGModuleException
- */
- public function remove()
- {
- try
- {
- $this->manager->delete($this->key);
- }
- catch (\Exception $ex)
- {
- throw new \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\MGModuleException(self::class, $ex->getMessage(), $ex->getCode(), $ex);
- }
- return $this;
- }
-
- /**
- * @return $this
- * @throws \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\MGModuleException
- */
- public function clearAll()
- {
- try
- {
- $this->manager->clear();
- }
- catch (\Exception $ex)
- {
- throw new \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\MGModuleException(self::class, $ex->getMessage(), $ex->getCode(), $ex);
- }
- return $this;
- }
-
- /**
- * @return type
- * @throws \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\MGModuleException
- */
- public function exist()
- {
- try
- {
- return $this->manager->has($this->key);
- }
- catch (\Exception $ex)
- {
- throw new \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\MGModuleException(self::class, $ex->getMessage(), $ex->getCode(), $ex);
- }
- }
- /**
- * @return CacheManager
- */
- public static function instance()
- {
- if (CacheManager::$instance === null)
- {
- CacheManager::$instance = new CacheManager();
- }
- return CacheManager::$instance;
- }
-
- public static function cache($key = null)
- {
- $istance = self::instance();
- if ($key)
- {
- $istance->setKey($key);
- }
- return $istance;
- }
- }
|