完整类源码
<?php /** * Sqlite缓存类 * @author WeakSun <52132522@qq.com> */ class Sqlite { static protected $handler; protected $options = array( 'table' => 'iCaches' ); /** * 架构函数 * @access public */ public function __construct($options = array()) { $this->options['prefix'] = isset($options['prefix']) ? $options['prefix'] : C('DATA_CACHE_PREFIX', null, ''); $this->options['expire'] = isset($options['expire']) ? $options['expire'] : C('DATA_CACHE_TIME', null, 36000); $this->options['nowTime'] = isset($GLOBALS['_beginTime']) ? $GLOBALS['_beginTime'] : microtime(true); $dbFile = TEMP_PATH . 'Caches.tmp'; $isCreate = is_file($dbFile); if (empty(static::$handler)) { static::$handler = new PDO("sqlite:{$dbFile}", null, null, array(PDO::ATTR_PERSISTENT => true)); empty($isCreate) && $this->exec("PRAGMA encoding = 'UTF8';PRAGMA temp_store = 2;PRAGMA auto_vacuum = 0;PRAGMA count_changes = 1;PRAGMA cache_size = 9000;"); $this->chkTable() || $this->createTable(); } } public function __destruct() { return $this->exec("DELETE FROM `{$this->options['table']}` WHERE `expire` < strftime('%s','now');VACUUM;"); } public function __call($method, $arguments) { if (method_exists(self::$handler, $method)) { return call_user_func_array(array(self::$handler, $method), $arguments); } else { E(__CLASS__ . ':' . $method . L('_METHOD_NOT_EXIST_')); return; } } /** * 读取缓存 * @access public * @param string $name 缓存变量名 * @return mixed */ public function get($name) { $id = $this->getName($name); $sth = static::$handler->query("SELECT `value` FROM `{$this->options['table']}` WHERE `id`='{$id}' AND `expire` > strftime('%s','now') LIMIT 1", PDO::FETCH_NUM); if (!empty($sth)) { N('cache_read', 1); list($data) = $sth->fetch(); return unserialize($data); } else { return false; } } /** * 写入缓存 * @access public * @param string $name 缓存变量名 * @param mixed $value 存储数据 * @param int $expire 有效时间 0为永久 * @return boolean */ public function set($name, $value, $expire = 0) { N('cache_write', 1); $data = serialize($value); if ($expire < 0 || $expire === false) { return true; } elseif (is_null($value) || $value === false) { return $this->rm($name); } elseif ($expire < 1) { $expire = 315360000; } $id = $this->getName($name); return $this->exec("REPLACE INTO `{$this->options['table']}` VALUES('{$id}','{$data}',{$expire}+strftime('%s','now'))"); } /** * 删除缓存 * @access public * @param string $name 缓存变量名 * @return boolean */ public function rm($name) { $id = $this->getName($name); return $this->exec("DELETE FROM `{$this->options['table']}` WHERE `id` = '{$id}'"); } /** * 清除缓存 * @access public * @param string $name 缓存变量名 * @return boolean */ public function clear() { return $this->exec("DELETE FROM `{$this->options['table']}`;"); } /** * 检查当前表是否存在 * @return bool 返回检查结果,存在返回True,失败返回False */ protected function chkTable() { return in_array($this->options['table'], $this->getTables()); } /** * 获取当前数据库的数据表列表 * @return array 返回获取到的数据表列表数组 */ protected function getTables() { $tables = $data = array(); $sth = $this->query("SELECT `name` FROM `sqlite_master` WHERE `type` = 'table' UNION ALL SELECT `name` FROM `sqlite_temp_master`"); if (!empty($sth)) { while ($row = $sth->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) { $tables[] = $row[0]; } unset($sth, $row); } return $tables; } /** * 创建当前数据表 * @return integer 成功返回1,失败返回0 */ protected function createTable() { return $this->exec("CREATE TABLE IF NOT EXISTS `{$this->options['table']}` (`id` VARCHAR PRIMARY KEY ON CONFLICT FAIL NOT NULL COLLATE 'NOCASE',`value` TEXT NOT NULL,`expire` INTEGER NOT NULL);"); } /** * 获取缓存名称 * @param string $name * @return string */ protected function getName($name) { if (!is_string($name) && !is_numeric($name)) { $name = md5(serialize($name)); } return $this->options['prefix'] . $name; } }
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
刺客信条阴影:贝壳谜语解决方案
1 周前ByDDD
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SublimeText3 Linux新版
SublimeText3 Linux最新版

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3汉化版
中文版,非常好用

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。