Table.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ 14CJ6xKrnXRLp6SxTu7GaDlaFdQX4Akn0llscsB5IWs=
  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\Cache\Adapter;
  13. use Cube\Db\Table\AbstractTable,
  14. Cube\Db\Expr;
  15. class Table extends AbstractAdapter
  16. {
  17. /**
  18. *
  19. * cache table object
  20. *
  21. * @var \Cube\Db\Table\AbstractTable
  22. */
  23. protected $_table;
  24. /**
  25. *
  26. * class constructor
  27. *
  28. * @param array $options configuration array
  29. *
  30. * @throws \RuntimeException
  31. */
  32. public function __construct($options = array())
  33. {
  34. parent::__construct($options);
  35. if (!isset($options['table'])) {
  36. throw new \RuntimeException("Cache table not specified.");
  37. }
  38. $tableClass = $options['table'];
  39. $this->setTable(
  40. new $tableClass());
  41. }
  42. /**
  43. *
  44. * get cache table
  45. *
  46. * @return string
  47. */
  48. public function getTable()
  49. {
  50. return $this->_table;
  51. }
  52. /**
  53. *
  54. * set cache table
  55. *
  56. * @param \Cube\Db\Table\AbstractTable $table
  57. *
  58. * @return $this
  59. * @throws \InvalidArgumentException
  60. */
  61. public function setTable(AbstractTable $table)
  62. {
  63. $this->_table = $table;
  64. return $this;
  65. }
  66. /**
  67. *
  68. * read from cache table
  69. *
  70. * @param string $name
  71. * @param string $type
  72. *
  73. * @return string|false
  74. */
  75. public function read($name, $type)
  76. {
  77. $where = $this->_table->select()
  78. ->where('name = ?', $name)
  79. ->where('type = ?', $type);
  80. $row = $this->_table->fetchRow($where);
  81. if ($row !== null) {
  82. $contents = $row['data'];
  83. return ($this->_serialization === true) ? unserialize($contents) : $contents;
  84. }
  85. return false;
  86. }
  87. /**
  88. *
  89. * write to cache table
  90. *
  91. * @param string $name
  92. * @param string $type
  93. * @param mixed $data
  94. * @param int $expires
  95. *
  96. * @return $this
  97. * @throws \RuntimeException
  98. */
  99. public function write($name, $type, $data, $expires = null)
  100. {
  101. $where = $this->_table->select()
  102. ->where('name = ?', $name)
  103. ->where('type = ?', $type);
  104. /** @var \Cube\Db\Table\Row $row */
  105. $row = $this->_table->fetchRow($where);
  106. if ($row === null) {
  107. if ($this->_serialization === true) {
  108. $data = serialize($data);
  109. }
  110. $this->_table->insert(array(
  111. 'name' => $name,
  112. 'type' => $type,
  113. 'data' => $data,
  114. 'created_at' => new Expr('now()'),
  115. ));
  116. }
  117. return $this;
  118. }
  119. /**
  120. *
  121. * delete a variable from cache
  122. *
  123. * @param string $name
  124. * @param string $type
  125. *
  126. * @return boolean
  127. */
  128. public function delete($name, $type)
  129. {
  130. $adapter = $this->_table->getAdapter();
  131. $where = array(
  132. $adapter->quoteInto('name = ?', $name),
  133. $adapter->quoteInto('type = ?', $type),
  134. );
  135. $this->_table->delete($where);
  136. return true;
  137. }
  138. /**
  139. *
  140. * purge cache
  141. *
  142. * @param string $type
  143. * @param boolean $force
  144. *
  145. * @return $this
  146. */
  147. public function purge($type, $force = false)
  148. {
  149. $adapter = $this->_table->getAdapter();
  150. $where[] = $adapter->quoteInto('type = ?', $type);
  151. if ($force !== true) {
  152. $where[] = $adapter->quoteInto('created_at < ?',
  153. new Expr('(now() - interval ' . intval($this->_expires) . ' second)'));
  154. }
  155. $this->_table->delete($where);
  156. return $this;
  157. }
  158. /**
  159. *
  160. * clear cache
  161. *
  162. * @return $this
  163. */
  164. public function clear()
  165. {
  166. $this->_table->delete('');
  167. return $this;
  168. }
  169. }