国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊 PHP緩存集成庫phpFastCache學習教程

PHP緩存集成庫phpFastCache學習教程

May 26, 2016 am 08:19 AM
phpfastcache php cache

PHP緩存的方法有很多種,常用的有memcache, memcached?,F(xiàn)在我們來學習一個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調(diào)用性能,代碼如下:

<?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 
	


本文地址:

轉(zhuǎn)載隨意,但請附上文章地址:-)

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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

PHP Tutorial
1502
276
Use PhpFastCache to improve the performance of PHP frameworks Use PhpFastCache to improve the performance of PHP frameworks Jul 07, 2023 pm 01:36 PM

Use PhpFastCache to improve the performance of PHP framework Introduction: In the process of developing PHP applications, performance is a crucial factor. To improve the performance of our application, we can use various optimization techniques and tools. This article will explore how to use PhpFastCache, a powerful caching library, to improve the performance of the PHP framework. We will introduce the characteristics and usage of PhpFastCache, and provide some code examples to implement the caching function. IntroductionPhpFastCach

How to use PHP development cache to optimize image loading speed How to use PHP development cache to optimize image loading speed Nov 08, 2023 pm 05:58 PM

How to use PHP to develop cache and optimize image loading speed. With the rapid development of the Internet, web page loading speed has become one of the important factors in user experience. Image loading speed is one of the important factors affecting web page loading speed. In order to speed up the loading of images, we can use PHP development cache to optimize the image loading speed. This article will introduce how to use PHP to develop cache to optimize image loading speed, and provide specific code examples. 1. Principle of cache Cache is a technology for storing data by temporarily storing data in high-speed memory.

Output caching in PHP Output caching in PHP May 23, 2023 pm 08:10 PM

Output caching in the PHP language is one of the commonly used performance optimization methods, which can greatly improve the performance of web applications. This article will introduce output caching in PHP and how to use it to optimize the performance of web applications. 1. What is output caching? In web applications, when we use PHP to output a piece of HTML code, PHP will output this code to the client line by line. Each line output will be immediately sent to the client. This method will cause a large number of network I/O operations, and network I/O is a bottleneck for web application performance.

How to use PhpFastCache for cache management in PHP projects How to use PhpFastCache for cache management in PHP projects Jul 07, 2023 am 08:34 AM

How to use PhpFastCache for cache management in PHP projects Introduction: With the development of Internet applications, caching has become one of the important means to improve application performance and response speed. PhpFastCache is a simple and easy-to-use PHP caching library that provides support for multiple caching backends (such as files, databases, and memory) and has an elegant API design. This article will introduce how to use PhpFastCache for cache management in PHP projects. 1. Install PhpFas

How to manage server-side caching with PhpFastCache How to manage server-side caching with PhpFastCache Jul 07, 2023 pm 02:48 PM

Introduction to how to use PhpFastCache to manage server-side caching: In server-side development, caching is one of the important means to improve application performance and response speed. PhpFastCache is a cache management library based on PHP. It provides a simple and easy-to-use interface and rich caching strategies, which can effectively manage server-side cache data. This article will introduce how to use PhpFastCache to manage server-side cache and explain in detail through code examples. 1. Install and configure PhpFa

Use PhpFastCache to improve data backup and recovery efficiency Use PhpFastCache to improve data backup and recovery efficiency Jul 07, 2023 am 10:33 AM

Use PhpFastCache to improve the efficiency of data backup and recovery. With the rapid development of the Internet, data has become one of the most crucial assets in modern society. For website administrators, data backup and recovery are an integral part of daily operation and maintenance work. How to improve the efficiency of data backup and recovery is an important issue that every administrator is concerned about. This article will introduce how to use the PhpFastCache library to improve the efficiency of data backup and recovery. PhpFastCache is a powerful

Research on the consistency and reliability of PHP data cache Research on the consistency and reliability of PHP data cache Aug 10, 2023 pm 06:10 PM

Research on the consistency and reliability of PHP data caching Introduction: In web development, data caching is one of the important means to improve application performance. As a commonly used server-side scripting language, PHP also provides a variety of data caching solutions. However, when using these caching solutions, we need to consider cache consistency and reliability issues. This article will explore the consistency and reliability of PHP data caching and provide corresponding code examples. 1. The issue of cache consistency When using data caching, the most important issue is how to ensure caching

Understanding PHP caching mechanisms: exploring different implementations Understanding PHP caching mechanisms: exploring different implementations Jan 23, 2024 am 09:53 AM

Explore the PHP caching mechanism: Understand different implementation methods and require specific code examples. The caching mechanism is a very important part of web development and can greatly improve the performance and response speed of the website. As a popular server-side language, PHP also provides a variety of caching mechanisms to optimize performance. This article will explore PHP's caching mechanism, introduce different implementation methods, and provide specific code examples. File Cache File cache is one of the simplest and most common PHP caching methods. Its principle is very simple

See all articles