AbstractAdapter.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ LKzi3HZ/M17qHTD/gVJs23m3hNyTCrKYHLjTtFPRsdI=
  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.02]
  11. */
  12. namespace Cube\Cache\Adapter;
  13. abstract class AbstractAdapter
  14. {
  15. /**
  16. * cache types
  17. */
  18. const ROUTES = 'routes';
  19. const METADATA = 'metadata';
  20. const QUERIES = 'queries';
  21. /**
  22. * used for cached queries
  23. */
  24. const CACHE_COL = 'cache_col';
  25. const CACHE_WHERE = 'cache_where';
  26. /**
  27. *
  28. * whether to cache select queries
  29. *
  30. * @var bool
  31. */
  32. protected $_cacheQueries = false;
  33. /**
  34. *
  35. * whether to cache table metadatas
  36. *
  37. * @var bool
  38. */
  39. protected $_cacheMetadata = true;
  40. /**
  41. *
  42. * whether to cache routes
  43. *
  44. * @var bool
  45. */
  46. protected $_cacheRoutes = false;
  47. /**
  48. *
  49. * whether to serialize data upon read and write
  50. *
  51. * @var bool
  52. */
  53. protected $_serialization = true;
  54. /**
  55. *
  56. * number of seconds after a cached variable expires
  57. *
  58. * @var integer
  59. */
  60. protected $_expires = 259200; // 3 days
  61. /**
  62. *
  63. * class constructor
  64. *
  65. * @param array $options configuration array
  66. */
  67. public function __construct($options = array())
  68. {
  69. if (isset($options['queries'])) {
  70. $this->setCacheQueries($options['queries']);
  71. }
  72. if (isset($options['metadata'])) {
  73. $this->setCacheMetadata($options['metadata']);
  74. }
  75. if (isset($options['routes'])) {
  76. $this->setCacheRoutes($options['routes']);
  77. }
  78. if (isset($options['serialization'])) {
  79. $this->setSerialization($options['serialization']);
  80. }
  81. }
  82. /**
  83. *
  84. * get cache queries flag
  85. *
  86. * @return bool
  87. */
  88. public function getCacheQueries()
  89. {
  90. return $this->_cacheQueries;
  91. }
  92. /**
  93. *
  94. * set cache queries flag
  95. *
  96. * @param bool $cacheQueries
  97. *
  98. * @return $this
  99. */
  100. public function setCacheQueries($cacheQueries)
  101. {
  102. $this->_cacheQueries = $cacheQueries;
  103. return $this;
  104. }
  105. /**
  106. *
  107. * get cache metadata flag
  108. *
  109. * @return bool
  110. */
  111. public function getCacheMetadata()
  112. {
  113. return $this->_cacheMetadata;
  114. }
  115. /**
  116. *
  117. * set cache metadata flag
  118. *
  119. * @param bool $cacheMetadata
  120. *
  121. * @return $this
  122. */
  123. public function setCacheMetadata($cacheMetadata)
  124. {
  125. $this->_cacheMetadata = (bool)$cacheMetadata;
  126. return $this;
  127. }
  128. /**
  129. *
  130. * get cache routes flag
  131. *
  132. * @return bool
  133. */
  134. public function getCacheRoutes()
  135. {
  136. return $this->_cacheRoutes;
  137. }
  138. /**
  139. *
  140. * set cache routes flag
  141. *
  142. * @param bool $cacheRoutes
  143. *
  144. * @return $this
  145. */
  146. public function setCacheRoutes($cacheRoutes)
  147. {
  148. $this->_cacheRoutes = (bool)$cacheRoutes;
  149. return $this;
  150. }
  151. /**
  152. *
  153. * get serialization flag
  154. *
  155. * @return bool
  156. */
  157. public function getSerialization()
  158. {
  159. return $this->_serialization;
  160. }
  161. /**
  162. *
  163. * set serialization flag
  164. *
  165. * @param bool $serialization
  166. *
  167. * @return $this
  168. */
  169. public function setSerialization($serialization)
  170. {
  171. $this->_serialization = (bool)$serialization;
  172. return $this;
  173. }
  174. /**
  175. *
  176. * get expiration time
  177. *
  178. * @return int
  179. */
  180. public function getExpires()
  181. {
  182. return $this->_expires;
  183. }
  184. /**
  185. *
  186. * set expiration time
  187. *
  188. * @param int $expires
  189. *
  190. * @return $this
  191. */
  192. public function setExpires($expires)
  193. {
  194. $this->_expires = (int)$expires;
  195. return $this;
  196. }
  197. /**
  198. *
  199. * read from cache
  200. *
  201. * @param string $name
  202. * @param string $type
  203. */
  204. abstract public function read($name, $type);
  205. /**
  206. *
  207. * write to cache
  208. *
  209. * @param string $name
  210. * @param string $type
  211. * @param mixed $data
  212. * @param int $expires
  213. */
  214. abstract public function write($name, $type, $data, $expires = null);
  215. /**
  216. *
  217. * delete variable from cache
  218. *
  219. * @param string $name
  220. * @param string $type
  221. */
  222. abstract public function delete($name, $type);
  223. /**
  224. *
  225. * purge cached variables
  226. *
  227. * @param string $type
  228. * @param boolean $force
  229. */
  230. abstract public function purge($type, $force = false);
  231. /**
  232. *
  233. * clear cache
  234. */
  235. abstract public function clear();
  236. }