Sqlite.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\cache\driver;
  12. use think\cache\Driver;
  13. /**
  14. * Sqlite缓存驱动
  15. * @author liu21st <liu21st@gmail.com>
  16. */
  17. class Sqlite extends Driver
  18. {
  19. protected $options = [
  20. 'db' => ':memory:',
  21. 'table' => 'sharedmemory',
  22. 'prefix' => '',
  23. 'expire' => 0,
  24. 'persistent' => false,
  25. 'serialize' => true,
  26. ];
  27. /**
  28. * 架构函数
  29. * @access public
  30. * @param array $options 缓存参数
  31. * @throws \BadFunctionCallException
  32. */
  33. public function __construct($options = [])
  34. {
  35. if (!extension_loaded('sqlite')) {
  36. throw new \BadFunctionCallException('not support: sqlite');
  37. }
  38. if (!empty($options)) {
  39. $this->options = array_merge($this->options, $options);
  40. }
  41. $func = $this->options['persistent'] ? 'sqlite_popen' : 'sqlite_open';
  42. $this->handler = $func($this->options['db']);
  43. }
  44. /**
  45. * 获取实际的缓存标识
  46. * @access public
  47. * @param string $name 缓存名
  48. * @return string
  49. */
  50. protected function getCacheKey($name)
  51. {
  52. return $this->options['prefix'] . sqlite_escape_string($name);
  53. }
  54. /**
  55. * 判断缓存
  56. * @access public
  57. * @param string $name 缓存变量名
  58. * @return bool
  59. */
  60. public function has($name)
  61. {
  62. $name = $this->getCacheKey($name);
  63. $sql = 'SELECT value FROM ' . $this->options['table'] . ' WHERE var=\'' . $name . '\' AND (expire=0 OR expire >' . time() . ') LIMIT 1';
  64. $result = sqlite_query($this->handler, $sql);
  65. return sqlite_num_rows($result);
  66. }
  67. /**
  68. * 读取缓存
  69. * @access public
  70. * @param string $name 缓存变量名
  71. * @param mixed $default 默认值
  72. * @return mixed
  73. */
  74. public function get($name, $default = false)
  75. {
  76. $this->readTimes++;
  77. $name = $this->getCacheKey($name);
  78. $sql = 'SELECT value FROM ' . $this->options['table'] . ' WHERE var=\'' . $name . '\' AND (expire=0 OR expire >' . time() . ') LIMIT 1';
  79. $result = sqlite_query($this->handler, $sql);
  80. if (sqlite_num_rows($result)) {
  81. $content = sqlite_fetch_single($result);
  82. if (function_exists('gzcompress')) {
  83. //启用数据压缩
  84. $content = gzuncompress($content);
  85. }
  86. return $this->unserialize($content);
  87. }
  88. return $default;
  89. }
  90. /**
  91. * 写入缓存
  92. * @access public
  93. * @param string $name 缓存变量名
  94. * @param mixed $value 存储数据
  95. * @param integer|\DateTime $expire 有效时间(秒)
  96. * @return boolean
  97. */
  98. public function set($name, $value, $expire = null)
  99. {
  100. $this->writeTimes++;
  101. $name = $this->getCacheKey($name);
  102. $value = sqlite_escape_string($this->serialize($value));
  103. if (is_null($expire)) {
  104. $expire = $this->options['expire'];
  105. }
  106. if ($expire instanceof \DateTime) {
  107. $expire = $expire->getTimestamp();
  108. } else {
  109. $expire = (0 == $expire) ? 0 : (time() + $expire); //缓存有效期为0表示永久缓存
  110. }
  111. if (function_exists('gzcompress')) {
  112. //数据压缩
  113. $value = gzcompress($value, 3);
  114. }
  115. if ($this->tag) {
  116. $tag = $this->tag;
  117. $this->tag = null;
  118. } else {
  119. $tag = '';
  120. }
  121. $sql = 'REPLACE INTO ' . $this->options['table'] . ' (var, value, expire, tag) VALUES (\'' . $name . '\', \'' . $value . '\', \'' . $expire . '\', \'' . $tag . '\')';
  122. if (sqlite_query($this->handler, $sql)) {
  123. return true;
  124. }
  125. return false;
  126. }
  127. /**
  128. * 自增缓存(针对数值缓存)
  129. * @access public
  130. * @param string $name 缓存变量名
  131. * @param int $step 步长
  132. * @return false|int
  133. */
  134. public function inc($name, $step = 1)
  135. {
  136. if ($this->has($name)) {
  137. $value = $this->get($name) + $step;
  138. } else {
  139. $value = $step;
  140. }
  141. return $this->set($name, $value, 0) ? $value : false;
  142. }
  143. /**
  144. * 自减缓存(针对数值缓存)
  145. * @access public
  146. * @param string $name 缓存变量名
  147. * @param int $step 步长
  148. * @return false|int
  149. */
  150. public function dec($name, $step = 1)
  151. {
  152. if ($this->has($name)) {
  153. $value = $this->get($name) - $step;
  154. } else {
  155. $value = -$step;
  156. }
  157. return $this->set($name, $value, 0) ? $value : false;
  158. }
  159. /**
  160. * 删除缓存
  161. * @access public
  162. * @param string $name 缓存变量名
  163. * @return boolean
  164. */
  165. public function rm($name)
  166. {
  167. $this->writeTimes++;
  168. $name = $this->getCacheKey($name);
  169. $sql = 'DELETE FROM ' . $this->options['table'] . ' WHERE var=\'' . $name . '\'';
  170. sqlite_query($this->handler, $sql);
  171. return true;
  172. }
  173. /**
  174. * 清除缓存
  175. * @access public
  176. * @param string $tag 标签名
  177. * @return boolean
  178. */
  179. public function clear($tag = null)
  180. {
  181. if ($tag) {
  182. $name = sqlite_escape_string($this->getTagKey($tag));
  183. $sql = 'DELETE FROM ' . $this->options['table'] . ' WHERE tag=\'' . $name . '\'';
  184. sqlite_query($this->handler, $sql);
  185. return true;
  186. }
  187. $this->writeTimes++;
  188. $sql = 'DELETE FROM ' . $this->options['table'];
  189. sqlite_query($this->handler, $sql);
  190. return true;
  191. }
  192. }