Sqlite.class.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 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;
  13. /**
  14. * Sqlite缓存驱动
  15. */
  16. class Sqlite extends Cache
  17. {
  18. /**
  19. * 架构函数
  20. * @param array $options 缓存参数
  21. * @access public
  22. */
  23. public function __construct($options = array())
  24. {
  25. if (!extension_loaded('sqlite')) {
  26. E(L('_NOT_SUPPORT_') . ':sqlite');
  27. }
  28. if (empty($options)) {
  29. $options = array(
  30. 'db' => ':memory:',
  31. 'table' => 'sharedmemory',
  32. );
  33. }
  34. $this->options = $options;
  35. $this->options['prefix'] = isset($options['prefix']) ? $options['prefix'] : C('DATA_CACHE_PREFIX');
  36. $this->options['length'] = isset($options['length']) ? $options['length'] : 0;
  37. $this->options['expire'] = isset($options['expire']) ? $options['expire'] : C('DATA_CACHE_TIME');
  38. $func = $this->options['persistent'] ? 'sqlite_popen' : 'sqlite_open';
  39. $this->handler = $func($this->options['db']);
  40. }
  41. /**
  42. * 读取缓存
  43. * @access public
  44. * @param string $name 缓存变量名
  45. * @return mixed
  46. */
  47. public function get($name)
  48. {
  49. N('cache_read', 1);
  50. $name = $this->options['prefix'] . sqlite_escape_string($name);
  51. $sql = 'SELECT value FROM ' . $this->options['table'] . ' WHERE var=\'' . $name . '\' AND (expire=0 OR expire >' . time() . ') LIMIT 1';
  52. $result = sqlite_query($this->handler, $sql);
  53. if (sqlite_num_rows($result)) {
  54. $content = sqlite_fetch_single($result);
  55. if (C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
  56. //启用数据压缩
  57. $content = gzuncompress($content);
  58. }
  59. return unserialize($content);
  60. }
  61. return false;
  62. }
  63. /**
  64. * 写入缓存
  65. * @access public
  66. * @param string $name 缓存变量名
  67. * @param mixed $value 存储数据
  68. * @param integer $expire 有效时间(秒)
  69. * @return boolean
  70. */
  71. public function set($name, $value, $expire = null)
  72. {
  73. N('cache_write', 1);
  74. $name = $this->options['prefix'] . sqlite_escape_string($name);
  75. $value = sqlite_escape_string(serialize($value));
  76. if (is_null($expire)) {
  77. $expire = $this->options['expire'];
  78. }
  79. $expire = (0 == $expire) ? 0 : (time() + $expire); //缓存有效期为0表示永久缓存
  80. if (C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
  81. //数据压缩
  82. $value = gzcompress($value, 3);
  83. }
  84. $sql = 'REPLACE INTO ' . $this->options['table'] . ' (var, value,expire) VALUES (\'' . $name . '\', \'' . $value . '\', \'' . $expire . '\')';
  85. if (sqlite_query($this->handler, $sql)) {
  86. if ($this->options['length'] > 0) {
  87. // 记录缓存队列
  88. $this->queue($name);
  89. }
  90. return true;
  91. }
  92. return false;
  93. }
  94. /**
  95. * 删除缓存
  96. * @access public
  97. * @param string $name 缓存变量名
  98. * @return boolean
  99. */
  100. public function rm($name)
  101. {
  102. $name = $this->options['prefix'] . sqlite_escape_string($name);
  103. $sql = 'DELETE FROM ' . $this->options['table'] . ' WHERE var=\'' . $name . '\'';
  104. sqlite_query($this->handler, $sql);
  105. return true;
  106. }
  107. /**
  108. * 清除缓存
  109. * @access public
  110. * @return boolean
  111. */
  112. public function clear()
  113. {
  114. $sql = 'DELETE FROM ' . $this->options['table'];
  115. sqlite_query($this->handler, $sql);
  116. return;
  117. }
  118. }