찾다

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;
	}

}

                   

                   

성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

DVWA

DVWA

DVWA(Damn Vulnerable Web App)는 매우 취약한 PHP/MySQL 웹 애플리케이션입니다. 주요 목표는 보안 전문가가 법적 환경에서 자신의 기술과 도구를 테스트하고, 웹 개발자가 웹 응용 프로그램 보안 프로세스를 더 잘 이해할 수 있도록 돕고, 교사/학생이 교실 환경 웹 응용 프로그램에서 가르치고 배울 수 있도록 돕는 것입니다. 보안. DVWA의 목표는 다양한 난이도의 간단하고 간단한 인터페이스를 통해 가장 일반적인 웹 취약점 중 일부를 연습하는 것입니다. 이 소프트웨어는

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

에디트플러스 중국어 크랙 버전

에디트플러스 중국어 크랙 버전

작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음

Dreamweaver Mac版

Dreamweaver Mac版

시각적 웹 개발 도구

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경