/Services/GlobalCache/classes/class.ilGlobalCacheService.php

https://github.com/ILIAS-eLearning/ILIAS · PHP · 331 lines · 120 code · 57 blank · 154 comment · 7 complexity · 0bc9cb1a2348ae77946dff7b88ea5523 MD5 · raw file

  1. <?php
  2. /**
  3. * Class ilGlobalCacheService
  4. *
  5. * Base class for all concrete cache implementations.
  6. *
  7. * @author Fabian Schmid <fs@studer-raimann.ch>
  8. * @version 1.0.1
  9. */
  10. abstract class ilGlobalCacheService
  11. {
  12. /**
  13. * @var int
  14. */
  15. protected $current_time = 0;
  16. /**
  17. * @var array
  18. */
  19. protected $valid_keys = array();
  20. /**
  21. * @var bool
  22. */
  23. protected static $active = array();
  24. /**
  25. * @var bool
  26. */
  27. protected static $installable = array();
  28. /**
  29. * @var string
  30. */
  31. protected $service_id = '';
  32. /**
  33. * @var string
  34. */
  35. protected $component = '';
  36. /**
  37. * @var int
  38. */
  39. protected $service_type = ilGlobalCache::TYPE_STATIC;
  40. /**
  41. * @var string
  42. */
  43. protected $valid_key_hash = '';
  44. /**
  45. * @param $service_id
  46. * @param $component
  47. */
  48. public function __construct($service_id, $component)
  49. {
  50. $this->setComponent($component);
  51. $this->setServiceId($service_id);
  52. self::$active[get_called_class()] = $this->getActive();
  53. self::$installable[get_called_class()] = ($this->getInstallable() and $this->checkMemory());
  54. }
  55. /**
  56. * @return bool
  57. */
  58. abstract protected function getActive();
  59. /**
  60. * @return bool
  61. */
  62. abstract protected function getInstallable();
  63. /**
  64. * @param $serialized_value
  65. *
  66. * @return mixed
  67. */
  68. abstract public function unserialize($serialized_value);
  69. /**
  70. * @param $key
  71. *
  72. * @return mixed
  73. */
  74. abstract public function get($key);
  75. /**
  76. * @param $key
  77. * @param $serialized_value
  78. * @param null $ttl
  79. *
  80. * @return bool
  81. */
  82. abstract public function set($key, $serialized_value, $ttl = null);
  83. /**
  84. * @param $value
  85. *
  86. * @return mixed
  87. */
  88. abstract public function serialize($value);
  89. /**
  90. * @return string
  91. */
  92. public function getServiceId()
  93. {
  94. return $this->service_id;
  95. }
  96. /**
  97. * @param string $service_id
  98. */
  99. public function setServiceId($service_id)
  100. {
  101. $this->service_id = $service_id;
  102. }
  103. /**
  104. * @return string
  105. */
  106. public function getComponent()
  107. {
  108. return $this->component;
  109. }
  110. /**
  111. * @param string $component
  112. */
  113. public function setComponent($component)
  114. {
  115. $this->component = $component;
  116. }
  117. /**
  118. * @return bool
  119. */
  120. public function isActive()
  121. {
  122. return self::$active[get_called_class()];
  123. }
  124. /**
  125. * @return bool
  126. */
  127. public function isInstallable()
  128. {
  129. return self::$installable[get_called_class()];
  130. }
  131. /**
  132. * @param $key
  133. *
  134. * @return string
  135. */
  136. public function returnKey($key)
  137. {
  138. return $str = $this->getServiceId() . '_' . $this->getComponent() . '_' . $key;
  139. }
  140. /**
  141. * @return array
  142. */
  143. public function getInfo()
  144. {
  145. return array();
  146. }
  147. /**
  148. * @return string
  149. */
  150. public function getInstallationFailureReason()
  151. {
  152. if (!$this->getInstallable()) {
  153. return 'Not installed';
  154. }
  155. if (!$this->checkMemory()) {
  156. return 'Not enough Cache-Memory, set to at least ' . $this->getMinMemory() . 'M';
  157. }
  158. return 'Unknown reason';
  159. }
  160. /**
  161. * @return int
  162. */
  163. protected function getMemoryLimit()
  164. {
  165. return 9999;
  166. }
  167. /**
  168. * @return int
  169. */
  170. protected function getMinMemory()
  171. {
  172. return 0;
  173. }
  174. /**
  175. * @return bool
  176. */
  177. protected function checkMemory()
  178. {
  179. $matches = array();
  180. $memory_limit = $this->getMemoryLimit();
  181. if (preg_match('/([0-9]*)([M|K])/uism', $memory_limit, $matches)) {
  182. switch ($matches[2]) {
  183. case 'M':
  184. $memory_limit = $matches[1] * 1024 * 1024; // nnnM -> nnn MB
  185. break;
  186. case 'K':
  187. $memory_limit = $matches[1] * 1024; // nnnK -> nnn KB
  188. break;
  189. }
  190. } else {
  191. $memory_limit = $memory_limit * 1024 * 1024; // nnnM -> nnn MB
  192. }
  193. return ($memory_limit >= $this->getMinMemory() * 1024 * 1024);
  194. }
  195. /**
  196. * @param $key
  197. *
  198. * @return bool
  199. */
  200. abstract public function exists($key);
  201. /**
  202. * @param $key
  203. *
  204. * @return bool
  205. */
  206. abstract public function delete($key);
  207. /**
  208. * @param bool $complete
  209. *
  210. * @return mixed
  211. */
  212. abstract public function flush($complete = false);
  213. /**
  214. * @param int $service_type
  215. */
  216. public function setServiceType($service_type)
  217. {
  218. $this->service_type = $service_type;
  219. }
  220. /**
  221. * @return int
  222. */
  223. public function getServiceType()
  224. {
  225. return $this->service_type;
  226. }
  227. /**
  228. * Declare a key as valid. If the key is already known no action is taken.
  229. *
  230. * This method exists only for legacy reasons and has only a real function
  231. * in combination with XCache.
  232. *
  233. * @param string $key The key which should be declared as valid.
  234. *
  235. * @return void
  236. */
  237. public function setValid($key)
  238. {
  239. $this->valid_keys[$key] = true;
  240. }
  241. /**
  242. * Set the key as invalid.
  243. * This method will invalidate all keys if no argument is given or null.
  244. *
  245. * This method exists only for legacy reasons and has only a real function
  246. * in combination with XCache.
  247. *
  248. * @param string $key The key which should be invalidated or null to invalidate all.
  249. *
  250. * @return void
  251. */
  252. public function setInvalid($key = null)
  253. {
  254. if ($key !== null) {
  255. unset($this->valid_keys[$key]);
  256. } else {
  257. unset($this->valid_keys);
  258. }
  259. }
  260. /**
  261. * Checks whether the cache key is valid or not.
  262. *
  263. * This method exists only for legacy reasons and has only a real function
  264. * in combination with XCache.
  265. *
  266. * @param string $key The key which should be checked.
  267. *
  268. * @return bool True if the key is valid otherwise false.
  269. */
  270. public function isValid($key)
  271. {
  272. return isset($this->valid_keys[$key]);
  273. }
  274. }