Table of Contents
完整类源码  " >完整类源码  
Home php教程 PHP源码 SQLITE缓存

SQLITE缓存

May 26, 2016 am 08:18 AM
php

完整类源码  

<?php

/**
 * Sqlite缓存类
 * @author WeakSun <52132522@qq.com>
 */

class Sqlite {

	static protected $handler;
	protected $options = array(
		&#39;table&#39; => &#39;iCaches&#39;
	);

	/**
	 * 架构函数
	 * @access public
	 */
	public function __construct($options = array()) {
		$this->options[&#39;prefix&#39;] = isset($options[&#39;prefix&#39;]) ? $options[&#39;prefix&#39;] : C(&#39;DATA_CACHE_PREFIX&#39;, null, &#39;&#39;);
		$this->options[&#39;expire&#39;] = isset($options[&#39;expire&#39;]) ? $options[&#39;expire&#39;] : C(&#39;DATA_CACHE_TIME&#39;, null, 36000);
		$this->options[&#39;nowTime&#39;] = isset($GLOBALS[&#39;_beginTime&#39;]) ? $GLOBALS[&#39;_beginTime&#39;] : microtime(true);
		$dbFile = TEMP_PATH . &#39;Caches.tmp&#39;;
		$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 = &#39;UTF8&#39;;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[&#39;table&#39;]}` WHERE `expire` < strftime(&#39;%s&#39;,&#39;now&#39;);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__ . &#39;:&#39; . $method . L(&#39;_METHOD_NOT_EXIST_&#39;));
			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[&#39;table&#39;]}` WHERE `id`=&#39;{$id}&#39; AND `expire` > strftime(&#39;%s&#39;,&#39;now&#39;) LIMIT 1", PDO::FETCH_NUM);
		if (!empty($sth)) {
			N(&#39;cache_read&#39;, 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(&#39;cache_write&#39;, 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[&#39;table&#39;]}` VALUES(&#39;{$id}&#39;,&#39;{$data}&#39;,{$expire}+strftime(&#39;%s&#39;,&#39;now&#39;))");
	}

	/**
	 * 删除缓存
	 * @access public
	 * @param string $name 缓存变量名
	 * @return boolean
	 */
	public function rm($name) {
		$id = $this->getName($name);
		return $this->exec("DELETE FROM `{$this->options[&#39;table&#39;]}` WHERE `id` = &#39;{$id}&#39;");
	}

	/**
	 * 清除缓存
	 * @access public
	 * @param string $name 缓存变量名
	 * @return boolean
	 */
	public function clear() {
		return $this->exec("DELETE FROM `{$this->options[&#39;table&#39;]}`;");
	}

	/**
	 * 检查当前表是否存在
	 * @return bool 返回检查结果,存在返回True,失败返回False
	 */
	protected function chkTable() {
		return in_array($this->options[&#39;table&#39;], $this->getTables());
	}

	/**
	 * 获取当前数据库的数据表列表
	 * @return array 返回获取到的数据表列表数组
	 */
	protected function getTables() {
		$tables = $data = array();
		$sth = $this->query("SELECT `name` FROM `sqlite_master` WHERE `type` = &#39;table&#39; 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[&#39;table&#39;]}` (`id` VARCHAR PRIMARY KEY ON CONFLICT FAIL NOT NULL COLLATE &#39;NOCASE&#39;,`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[&#39;prefix&#39;] . $name;
	}

}
Copy after login

                   

                   

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles