Cache.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ Geg1DnRtwCPJ6exu0snYrA6mThBak7I6KyZli1TYyZY=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2017 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.10 [rev.1.10.01]
  11. */
  12. namespace Cube;
  13. class Cache
  14. {
  15. /**
  16. *
  17. * returns an instance of the object and creates it if it wasn't instantiated yet
  18. *
  19. * @return \Cube\Cache
  20. */
  21. private static $_instance;
  22. /**
  23. *
  24. * cache adapter
  25. *
  26. * @var \Cube\Cache\Adapter\AbstractAdapter
  27. */
  28. protected $_adapter;
  29. /**
  30. *
  31. * class constructor
  32. *
  33. * @param array $options configuration array
  34. *
  35. * @throws \RuntimeException
  36. */
  37. protected function __construct($options = array())
  38. {
  39. // initialize adapter from configuration
  40. if (!isset($options['adapter'])) {
  41. throw new \RuntimeException("Cache adapter not specified.");
  42. }
  43. $adapterClass = $options['adapter'];
  44. $this->setAdapter(
  45. new $adapterClass($options));
  46. }
  47. /**
  48. *
  49. * initialize application as singleton
  50. *
  51. * @param array $options configuration array
  52. *
  53. * @return \Cube\Cache
  54. */
  55. public static function getInstance($options = array())
  56. {
  57. if (!self::$_instance instanceof self) {
  58. self::$_instance = new self($options);
  59. }
  60. return self::$_instance;
  61. }
  62. /**
  63. *
  64. * get adapter
  65. *
  66. * @return Cache\Adapter\AbstractAdapter
  67. */
  68. public function getAdapter()
  69. {
  70. return $this->_adapter;
  71. }
  72. /**
  73. *
  74. * set adapter
  75. *
  76. * @param Cache\Adapter\AbstractAdapter $adapter
  77. *
  78. * @return $this
  79. */
  80. public function setAdapter($adapter)
  81. {
  82. $this->_adapter = $adapter;
  83. return $this;
  84. }
  85. /**
  86. *
  87. * get cache adapter queries flag
  88. *
  89. * @return bool
  90. */
  91. public function getCacheQueries()
  92. {
  93. return $this->getAdapter()->getCacheQueries();
  94. }
  95. /**
  96. *
  97. * get cache adapter metadata flag
  98. *
  99. * @return bool
  100. */
  101. public function getCacheMetadata()
  102. {
  103. return $this->getAdapter()->getCacheMetadata();
  104. }
  105. /**
  106. *
  107. * get cache adapter routes flag
  108. *
  109. * @return bool
  110. */
  111. public function getCacheRoutes()
  112. {
  113. return $this->getAdapter()->getCacheRoutes();
  114. }
  115. /**
  116. *
  117. * read data from cache
  118. *
  119. * @param string $name
  120. * @param string $type
  121. *
  122. * @return string|false
  123. */
  124. public function read($name, $type)
  125. {
  126. return $this->getAdapter()->read($name, $type);
  127. }
  128. /**
  129. *
  130. * write data to cache
  131. *
  132. * @param string $name
  133. * @param string $type
  134. * @param mixed $data
  135. * @param int $expires
  136. *
  137. * @return $this
  138. */
  139. public function write($name, $type, $data, $expires = null)
  140. {
  141. $this->getAdapter()->write($name, $type, $data, $expires);
  142. return $this;
  143. }
  144. }