/Kernel/ThinkPHP/Lib/Think/Util/Cache/CacheSqlite.class.php

https://github.com/liujinsong668/epptime · PHP · 169 lines · 79 code · 8 blank · 82 comment · 7 complexity · 633f1111cfb966974469801734324085 MD5 · raw file

  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 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. // $Id$
  12. /**
  13. +------------------------------------------------------------------------------
  14. * Sqlite缓存类
  15. +------------------------------------------------------------------------------
  16. * @category Think
  17. * @package Think
  18. * @subpackage Util
  19. * @author liu21st <liu21st@gmail.com>
  20. * @version $Id$
  21. +------------------------------------------------------------------------------
  22. */
  23. class CacheSqlite extends Cache
  24. {
  25. /**
  26. +----------------------------------------------------------
  27. * 架构函数
  28. +----------------------------------------------------------
  29. * @access public
  30. +----------------------------------------------------------
  31. */
  32. public function __construct($options='')
  33. {
  34. if ( !extension_loaded('sqlite') ) {
  35. throw_exception(L('_NOT_SUPPERT_').':sqlite');
  36. }
  37. if(empty($options)){
  38. $options= array
  39. (
  40. 'db' => ':memory:',
  41. 'table' => 'sharedmemory',
  42. 'var' => 'var',
  43. 'value' => 'value',
  44. 'expire' => 'expire',
  45. 'persistent'=> false
  46. );
  47. }
  48. $this->options = $options;
  49. $func = $this->options['persistent'] ? 'sqlite_popen' : 'sqlite_open';
  50. $this->handler = $func($this->options['db']);
  51. $this->connected = is_resource($this->handler);
  52. $this->type = strtoupper(substr(__CLASS__,6));
  53. }
  54. /**
  55. +----------------------------------------------------------
  56. * 是否连接
  57. +----------------------------------------------------------
  58. * @access private
  59. +----------------------------------------------------------
  60. * @return boolen
  61. +----------------------------------------------------------
  62. */
  63. private function isConnected()
  64. {
  65. return $this->connected;
  66. }
  67. /**
  68. +----------------------------------------------------------
  69. * 读取缓存
  70. +----------------------------------------------------------
  71. * @access public
  72. +----------------------------------------------------------
  73. * @param string $name 缓存变量名
  74. +----------------------------------------------------------
  75. * @return mixed
  76. +----------------------------------------------------------
  77. */
  78. public function get($name)
  79. {
  80. N('cache_read',1);
  81. $name = sqlite_escape_string($name);
  82. $sql = 'SELECT '.$this->options['value'].
  83. ' FROM '.$this->options['table'].
  84. ' WHERE '.$this->options['var'].'=\''.$name.'\' AND ('.$this->options['expire'].'=-1 OR '.$this->options['expire'].'>'.time().
  85. ') LIMIT 1';
  86. $result = sqlite_query($this->handler, $sql);
  87. if (sqlite_num_rows($result)) {
  88. $content = sqlite_fetch_single($result);
  89. if(C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
  90. //启用数据压缩
  91. $content = gzuncompress($content);
  92. }
  93. return unserialize($content);
  94. }
  95. return false;
  96. }
  97. /**
  98. +----------------------------------------------------------
  99. * 写入缓存
  100. +----------------------------------------------------------
  101. * @access public
  102. +----------------------------------------------------------
  103. * @param string $name 缓存变量名
  104. * @param mixed $value 存储数据
  105. +----------------------------------------------------------
  106. * @return boolen
  107. +----------------------------------------------------------
  108. */
  109. public function set($name, $value,$expireTime=0)
  110. {
  111. N('cache_write',1);
  112. $expire = !empty($expireTime)? $expireTime : C('DATA_CACHE_TIME');
  113. $name = sqlite_escape_string($name);
  114. $value = sqlite_escape_string(serialize($value));
  115. $expire = ($expireTime==-1)?-1: (time()+$expire);
  116. if( C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
  117. //数据压缩
  118. $value = gzcompress($value,3);
  119. }
  120. $sql = 'REPLACE INTO '.$this->options['table'].
  121. ' ('.$this->options['var'].', '.$this->options['value'].','.$this->options['expire'].
  122. ') VALUES (\''.$name.'\', \''.$value.'\', \''.$expire.'\')';
  123. sqlite_query($this->handler, $sql);
  124. return true;
  125. }
  126. /**
  127. +----------------------------------------------------------
  128. * 删除缓存
  129. +----------------------------------------------------------
  130. * @access public
  131. +----------------------------------------------------------
  132. * @param string $name 缓存变量名
  133. +----------------------------------------------------------
  134. * @return boolen
  135. +----------------------------------------------------------
  136. */
  137. public function rm($name)
  138. {
  139. $name = sqlite_escape_string($name);
  140. $sql = 'DELETE FROM '.$this->options['table'].
  141. ' WHERE '.$this->options['var'].'=\''.$name.'\'';
  142. sqlite_query($this->handler, $sql);
  143. return true;
  144. }
  145. /**
  146. +----------------------------------------------------------
  147. * 清除缓存
  148. +----------------------------------------------------------
  149. * @access public
  150. +----------------------------------------------------------
  151. * @return boolen
  152. +----------------------------------------------------------
  153. */
  154. public function clear()
  155. {
  156. $sql = 'delete from `'.$this->options['table'].'`';
  157. sqlite_query($this->handler, $sql);
  158. return ;
  159. }
  160. }//类定义结束
  161. ?>