Sqlite.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. ];
  26. /**
  27. * 构造函数
  28. * @param array $options 缓存参数
  29. * @throws \BadFunctionCallException
  30. * @access public
  31. */
  32. public function __construct($options = [])
  33. {
  34. if (!extension_loaded('sqlite')) {
  35. throw new \BadFunctionCallException('not support: sqlite');
  36. }
  37. if (!empty($options)) {
  38. $this->options = array_merge($this->options, $options);
  39. }
  40. $func = $this->options['persistent'] ? 'sqlite_popen' : 'sqlite_open';
  41. $this->handler = $func($this->options['db']);
  42. }
  43. /**
  44. * 获取实际的缓存标识
  45. * @access public
  46. * @param string $name 缓存名
  47. * @return string
  48. */
  49. protected function getCacheKey($name)
  50. {
  51. return $this->options['prefix'] . sqlite_escape_string($name);
  52. }
  53. /**
  54. * 判断缓存
  55. * @access public
  56. * @param string $name 缓存变量名
  57. * @return bool
  58. */
  59. public function has($name)
  60. {
  61. $name = $this->getCacheKey($name);
  62. $sql = 'SELECT value FROM ' . $this->options['table'] . ' WHERE var=\'' . $name . '\' AND (expire=0 OR expire >' . $_SERVER['REQUEST_TIME'] . ') LIMIT 1';
  63. $result = sqlite_query($this->handler, $sql);
  64. return sqlite_num_rows($result);
  65. }
  66. /**
  67. * 读取缓存
  68. * @access public
  69. * @param string $name 缓存变量名
  70. * @param mixed $default 默认值
  71. * @return mixed
  72. */
  73. public function get($name, $default = false)
  74. {
  75. $name = $this->getCacheKey($name);
  76. $sql = 'SELECT value FROM ' . $this->options['table'] . ' WHERE var=\'' . $name . '\' AND (expire=0 OR expire >' . $_SERVER['REQUEST_TIME'] . ') LIMIT 1';
  77. $result = sqlite_query($this->handler, $sql);
  78. if (sqlite_num_rows($result)) {
  79. $content = sqlite_fetch_single($result);
  80. if (function_exists('gzcompress')) {
  81. //启用数据压缩
  82. $content = gzuncompress($content);
  83. }
  84. return unserialize($content);
  85. }
  86. return $default;
  87. }
  88. /**
  89. * 写入缓存
  90. * @access public
  91. * @param string $name 缓存变量名
  92. * @param mixed $value 存储数据
  93. * @param integer|\DateTime $expire 有效时间(秒)
  94. * @return boolean
  95. */
  96. public function set($name, $value, $expire = null)
  97. {
  98. $name = $this->getCacheKey($name);
  99. $value = sqlite_escape_string(serialize($value));
  100. if (is_null($expire)) {
  101. $expire = $this->options['expire'];
  102. }
  103. if ($expire instanceof \DateTime) {
  104. $expire = $expire->getTimestamp();
  105. } else {
  106. $expire = (0 == $expire) ? 0 : (time() + $expire); //缓存有效期为0表示永久缓存
  107. }
  108. if (function_exists('gzcompress')) {
  109. //数据压缩
  110. $value = gzcompress($value, 3);
  111. }
  112. if ($this->tag) {
  113. $tag = $this->tag;
  114. $this->tag = null;
  115. } else {
  116. $tag = '';
  117. }
  118. $sql = 'REPLACE INTO ' . $this->options['table'] . ' (var, value, expire, tag) VALUES (\'' . $name . '\', \'' . $value . '\', \'' . $expire . '\', \'' . $tag . '\')';
  119. if (sqlite_query($this->handler, $sql)) {
  120. return true;
  121. }
  122. return false;
  123. }
  124. /**
  125. * 自增缓存(针对数值缓存)
  126. * @access public
  127. * @param string $name 缓存变量名
  128. * @param int $step 步长
  129. * @return false|int
  130. */
  131. public function inc($name, $step = 1)
  132. {
  133. if ($this->has($name)) {
  134. $value = $this->get($name) + $step;
  135. } else {
  136. $value = $step;
  137. }
  138. return $this->set($name, $value, 0) ? $value : false;
  139. }
  140. /**
  141. * 自减缓存(针对数值缓存)
  142. * @access public
  143. * @param string $name 缓存变量名
  144. * @param int $step 步长
  145. * @return false|int
  146. */
  147. public function dec($name, $step = 1)
  148. {
  149. if ($this->has($name)) {
  150. $value = $this->get($name) - $step;
  151. } else {
  152. $value = -$step;
  153. }
  154. return $this->set($name, $value, 0) ? $value : false;
  155. }
  156. /**
  157. * 删除缓存
  158. * @access public
  159. * @param string $name 缓存变量名
  160. * @return boolean
  161. */
  162. public function rm($name)
  163. {
  164. $name = $this->getCacheKey($name);
  165. $sql = 'DELETE FROM ' . $this->options['table'] . ' WHERE var=\'' . $name . '\'';
  166. sqlite_query($this->handler, $sql);
  167. return true;
  168. }
  169. /**
  170. * 清除缓存
  171. * @access public
  172. * @param string $tag 标签名
  173. * @return boolean
  174. */
  175. public function clear($tag = null)
  176. {
  177. if ($tag) {
  178. $name = sqlite_escape_string($tag);
  179. $sql = 'DELETE FROM ' . $this->options['table'] . ' WHERE tag=\'' . $name . '\'';
  180. sqlite_query($this->handler, $sql);
  181. return true;
  182. }
  183. $sql = 'DELETE FROM ' . $this->options['table'];
  184. sqlite_query($this->handler, $sql);
  185. return true;
  186. }
  187. }