찾다
php教程php手册PHP缓存集成库phpFastCache学习教程

PHP缓存的方法有很多种,常用的有memcache, memcached。现在我们来学习一个php缓存集成库phpFastCache,就是开源的,只有一个简单的php文件,就可以支持包括apc,memcache,memcached,wincache,files,pdo and mpdo等缓存方法.

phpFastCache是一个开源的PHP缓存库,只提供一个简单的PHP文件,可方便集成到已有项目,支持多种缓存方法,包括:apc,memcache,memcached,wincache,files,pdo and mpdo,可通过简单的API来定义缓存的有效时间,代码如下:

<?php 
	// In your config file 
	include("phpfastcache/phpfastcache.php"); 
	phpFastCache::setup("storage","auto"); 
	 
	// phpFastCache support "apc", "memcache", "memcached", "wincache" ,"files", "sqlite" and "xcache" 
	// You don&#39;t need to change your code when you change your caching system. Or simple keep it auto 
	$cache = phpFastCache(); 
	 
	// In your Class, Functions, PHP Pages 
	// try to get from Cache first. product_page = YOUR Identity Keyword 
	$products = $cache->get("product_page"); 
	 
	if($products == null) { 
	    $products = YOUR DB QUERIES || GET_PRODUCTS_FUNCTION; 
	    // set products in to cache in 600 seconds = 10 minutes 
	    $cache->set("product_page", $products,600); 
	} 
	 
	// Output Your Contents $products HERE 
	 

提高cURL和API调用性能,代码如下:

<?php 
	include("phpfastcache/phpfastcache.php"); 
	 
	$cache = phpFastCache("memcached"); 
	 
	// try to get from Cache first. 
	$results = $cache->get("identity_keyword") 
	 
	if($results == null) { 
	    $results = cURL->get("http://www.youtube.com/api/json/url/keyword/page"); 
	    // Write to Cache Save API Calls next time 
	    $cache->set("identity_keyword", $results, 3600*24); 
	} 
	 
	foreach($results as $video) { 
	    // Output Your Contents HERE 
	} 
	 

全页缓存,代码如下:

<?php 
	// use Files Cache for Whole Page / Widget 
	 
	// keyword = Webpage_URL 
	$keyword_webpage = md5($_SERVER[&#39;HTTP_HOST&#39;].$_SERVER[&#39;REQUEST_URI&#39;].$_SERVER[&#39;QUERY_STRING&#39;]); 
	$html = __c("files")->get($keyword_webpage); 
	 
	if($html == null) { 
	    ob_start(); 
	    /* 
	        ALL OF YOUR CODE GO HERE 
	        RENDER YOUR PAGE, DB QUERY, WHATEVER 
	    */ 
	 
	    // GET HTML WEBPAGE 
	    $html = ob_get_contents(); 
	    // Save to Cache 30 minutes 
	    __c("files")->set($keyword_webpage,$html, 1800); 
	} 
	 
	echo $html; 
	 

挂件缓存,代码如下:

<?php 
	// use Files Cache for Whole Page / Widget 
	$cache = phpFastCache("files"); 
	 
	$html = $cache->widget_1; 
	 
	if($html == null) { 
	    $html = Render Your Page || Widget || "Hello World"; 
	    // Save to Cache 30 minutes 
	    $cache->widget_1 = array($html, 1800); 
	} 
	 
	echo or return your $html; 
	 

同时使用多种缓存,代码如下:

<?php 
	// in your config files 
	include("phpfastcache/phpfastcache.php"); 
	// auto | memcache | files ...etc. Will be default for $cache = __c(); 
	phpFastCache::$storage = "auto"; 
	 
	$cache1 = phpFastCache(); 
	 
	$cache2 = __c("memcache"); 
	$server = array(array("127.0.0.1",11211,100), array("128.5.1.3",11215,80)); 
	$cache2->option("server", $server); 
	 
	$cache3 = new phpFastCache("apc"); 
	 
	// How to Write? 
	$cache1->set("keyword1", "string|number|array|object", 300); 
	$cache2->keyword2 = array("something here", 600); 
	__c()->keyword3 = array("array|object", 3600*24); 
	 
	// How to Read? 
	$data = $cache1->get("keyword1"); 
	$data = $cache2->keyword2; 
	$data = __c()->keyword3; 
	$data = __c()->get("keyword4"); 
	 
	// Free to Travel between any caching methods 
	 
	$cache1 = phpFastCache("files"); 
	$cache1->set("keyword1", $value, $time); 
	$cache1->memcache->set("keyword1", $value, $time); 
	$cache1->apc->set("whatever", $value, 300); 
	 
	$cache2 = __c("apc"); 
	$cache2->keyword1 = array("so cool", 300); 
	$cache2->files->keyword1 = array("Oh yeah!", 600); 
	 
	$data = __c("memcache")->get("keyword1"); 
	$data = __c("files")->get("keyword2"); 
	$data = __c()->keyword3; 
	 
	// Multiple ? No Problem 
	 
	$list = $cache1->getMulti(array("key1","key2","key3")); 
	$cache2->setMulti(array("key1","value1", 300), 
	                  array("key2","value2", 600), 
	                  array("key3","value3", 1800), 
	                  ); 
	 
	$list = $cache1->apc->getMulti(array("key1","key2","key3")); 
	__c()->memcache->getMulti(array("a","b","c")); 
	 
	// want more? Check out document in source code 
	


本文地址:

转载随意,但请附上文章地址:-)

성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 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. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 채팅 명령 및 사용 방법
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

VSCode Windows 64비트 다운로드

VSCode Windows 64비트 다운로드

Microsoft에서 출시한 강력한 무료 IDE 편집기

DVWA

DVWA

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

SublimeText3 Linux 새 버전

SublimeText3 Linux 새 버전

SublimeText3 Linux 최신 버전

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

맨티스BT

맨티스BT

Mantis는 제품 결함 추적을 돕기 위해 설계된 배포하기 쉬운 웹 기반 결함 추적 도구입니다. PHP, MySQL 및 웹 서버가 필요합니다. 데모 및 호스팅 서비스를 확인해 보세요.