CacheManager.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\Cache;
  3. use Symfony\Component\Cache\Simple\FilesystemCache;
  4. use ThurData\Servers\KerioEmail\Core\ModuleConstants;
  5. use ThurData\Servers\KerioEmail\Core\Interfaces\CacheManagerInterface;
  6. /**
  7. * Description of CacheManager
  8. *
  9. * @author Rafał Ossowski <rafal.os@thurdata.com>
  10. */
  11. class CacheManager implements CacheManagerInterface
  12. {
  13. protected static $instance = null;
  14. /**
  15. * @var CacheManagerInterface
  16. */
  17. protected $manager;
  18. /**
  19. * @var string
  20. */
  21. protected $dir = '';
  22. /**
  23. * @var string
  24. */
  25. protected $namespace = '';
  26. /**
  27. * @var string
  28. */
  29. protected $key;
  30. /**
  31. * @var string|array|object
  32. */
  33. protected $data;
  34. public function __construct()
  35. {
  36. $this->namespace = ModuleConstants::getRootNamespace();
  37. $this->dir = ModuleConstants::getModuleRootDir() . DS . 'storage' . DS . 'framework';
  38. $this->manager = new FilesystemCache('', 0, $this->dir);
  39. }
  40. private function __clone()
  41. {
  42. }
  43. /**
  44. * @param string $key
  45. * @return $this
  46. * @throws \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\MGModuleException
  47. */
  48. public function setKey($key = 'default')
  49. {
  50. try
  51. {
  52. $this->key = $key;
  53. $this->setData($this->manager->get($this->key));
  54. }
  55. catch (\Exception $ex)
  56. {
  57. throw new \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\MGModuleException(self::class, $ex->getMessage(), $ex->getCode(), $ex);
  58. }
  59. return $this;
  60. }
  61. /**
  62. * @return string
  63. */
  64. public function getKey()
  65. {
  66. return $this->key;
  67. }
  68. /**
  69. * @param mixed $data
  70. * @return $this
  71. */
  72. public function setData($data = null)
  73. {
  74. if ($data !== null)
  75. {
  76. $this->data = $data;
  77. }
  78. return $this;
  79. }
  80. /**
  81. * @return mixed
  82. * @throws \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\MGModuleException
  83. */
  84. public function getData()
  85. {
  86. try
  87. {
  88. if (isset($this->key))
  89. {
  90. $this->data = $this->manager->get($this->key);
  91. }
  92. return $this->data;
  93. }
  94. catch (\Exception $ex)
  95. {
  96. throw new \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\MGModuleException(self::class, $ex->getMessage(), $ex->getCode(), $ex);
  97. }
  98. }
  99. /**
  100. * @return $this
  101. * @throws \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\MGModuleException
  102. */
  103. public function save()
  104. {
  105. try
  106. {
  107. $this->manager->set($this->key, $this->data);
  108. }
  109. catch (\Exception $ex)
  110. {
  111. throw new \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\MGModuleException(self::class, $ex->getMessage(), $ex->getCode(), $ex);
  112. }
  113. return $this;
  114. }
  115. /**
  116. * @return $this
  117. * @throws \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\MGModuleException
  118. */
  119. public function remove()
  120. {
  121. try
  122. {
  123. $this->manager->delete($this->key);
  124. }
  125. catch (\Exception $ex)
  126. {
  127. throw new \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\MGModuleException(self::class, $ex->getMessage(), $ex->getCode(), $ex);
  128. }
  129. return $this;
  130. }
  131. /**
  132. * @return $this
  133. * @throws \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\MGModuleException
  134. */
  135. public function clearAll()
  136. {
  137. try
  138. {
  139. $this->manager->clear();
  140. }
  141. catch (\Exception $ex)
  142. {
  143. throw new \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\MGModuleException(self::class, $ex->getMessage(), $ex->getCode(), $ex);
  144. }
  145. return $this;
  146. }
  147. /**
  148. * @return type
  149. * @throws \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\MGModuleException
  150. */
  151. public function exist()
  152. {
  153. try
  154. {
  155. return $this->manager->has($this->key);
  156. }
  157. catch (\Exception $ex)
  158. {
  159. throw new \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\MGModuleException(self::class, $ex->getMessage(), $ex->getCode(), $ex);
  160. }
  161. }
  162. /**
  163. * @return CacheManager
  164. */
  165. public static function instance()
  166. {
  167. if (CacheManager::$instance === null)
  168. {
  169. CacheManager::$instance = new CacheManager();
  170. }
  171. return CacheManager::$instance;
  172. }
  173. public static function cache($key = null)
  174. {
  175. $istance = self::instance();
  176. if ($key)
  177. {
  178. $istance->setKey($key);
  179. }
  180. return $istance;
  181. }
  182. }