CacheSqlite.class.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2012 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. defined('THINK_PATH') or exit();
  12. /**
  13. * Sqlite缓存驱动
  14. * @category Extend
  15. * @package Extend
  16. * @subpackage Driver.Cache
  17. * @author liu21st <liu21st@gmail.com>
  18. */
  19. class CacheSqlite extends Cache {
  20. /**
  21. * 架构函数
  22. * @param array $options 缓存参数
  23. * @access public
  24. */
  25. public function __construct($options=array()) {
  26. if ( !extension_loaded('sqlite') ) {
  27. throw_exception(L('_NOT_SUPPERT_').':sqlite');
  28. }
  29. if(empty($options)) {
  30. $options = array (
  31. 'db' => ':memory:',
  32. 'table' => 'sharedmemory',
  33. );
  34. }
  35. $this->options = $options;
  36. $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX');
  37. $this->options['length'] = isset($options['length'])? $options['length'] : 0;
  38. $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME');
  39. $func = $this->options['persistent'] ? 'sqlite_popen' : 'sqlite_open';
  40. $this->handler = $func($this->options['db']);
  41. }
  42. /**
  43. * 读取缓存
  44. * @access public
  45. * @param string $name 缓存变量名
  46. * @return mixed
  47. */
  48. public function get($name) {
  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 boolen
  70. */
  71. public function set($name, $value,$expire=null) {
  72. N('cache_write',1);
  73. $name = $this->options['prefix'].sqlite_escape_string($name);
  74. $value = sqlite_escape_string(serialize($value));
  75. if(is_null($expire)) {
  76. $expire = $this->options['expire'];
  77. }
  78. $expire = ($expire==0)?0: (time()+$expire) ;//缓存有效期为0表示永久缓存
  79. if( C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
  80. //数据压缩
  81. $value = gzcompress($value,3);
  82. }
  83. $sql = 'REPLACE INTO '.$this->options['table'].' (var, value,expire) VALUES (\''.$name.'\', \''.$value.'\', \''.$expire.'\')';
  84. if(sqlite_query($this->handler, $sql)){
  85. if($this->options['length']>0) {
  86. // 记录缓存队列
  87. $this->queue($name);
  88. }
  89. return true;
  90. }
  91. return false;
  92. }
  93. /**
  94. * 删除缓存
  95. * @access public
  96. * @param string $name 缓存变量名
  97. * @return boolen
  98. */
  99. public function rm($name) {
  100. $name = $this->options['prefix'].sqlite_escape_string($name);
  101. $sql = 'DELETE FROM '.$this->options['table'].' WHERE var=\''.$name.'\'';
  102. sqlite_query($this->handler, $sql);
  103. return true;
  104. }
  105. /**
  106. * 清除缓存
  107. * @access public
  108. * @return boolen
  109. */
  110. public function clear() {
  111. $sql = 'DELETE FROM '.$this->options['table'];
  112. sqlite_query($this->handler, $sql);
  113. return ;
  114. }
  115. }