Sqlite.class.php 4.2 KB

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