Home php教程 php手册 PHP缓存库phpFastCache

PHP缓存库phpFastCache

Jun 06, 2016 pm 07:36 PM
php phpfastcache cache

phpFastCache是一个开源的PHP 缓存 库,只提供一个简单的PHP文件,可方便集成到已有项目,支持多种 缓存 方法,包括:apc, memcache, memcached, wincache, files, pdo and mpdo。可通过简单的API来定义 缓存 的有效时间。 减少数据库查询 ? php // In your

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

减少数据库查询

<span>php
    </span><span>//</span><span> In your config file</span>
    <span>include</span>("phpfastcache/phpfastcache.php"<span>);
    phpFastCache</span>::setup("storage","auto"<span>);

    </span><span>//</span><span> phpFastCache support "apc", "memcache", "memcached", "wincache" ,"files", "sqlite" and "xcache"
    // You don't need to change your code when you change your caching system. Or simple keep it auto</span>
    <span>$cache</span> =<span> phpFastCache();

    </span><span>//</span><span> In your Class, Functions, PHP Pages
    // try to get from Cache first. product_page = YOUR Identity Keyword</span>
    <span>$products</span> = <span>$cache</span>->get("product_page"<span>);

    </span><span>if</span>(<span>$products</span> == <span>null</span><span>) {
        </span><span>$products</span> = YOUR DB QUERIES ||<span> GET_PRODUCTS_FUNCTION;
        </span><span>//</span><span> set products in to cache in 600 seconds = 10 minutes</span>
        <span>$cache</span>->set("product_page", <span>$products</span>,600<span>);
    }

    </span><span>//</span><span> Output Your Contents $products HERE</span>
Copy after login

 

提高cURL和API调用性能

<span>php
    </span><span>include</span>("phpfastcache/phpfastcache.php"<span>);

    </span><span>$cache</span> = phpFastCache("memcached"<span>);

    </span><span>//</span><span> try to get from Cache first.</span>
    <span>$results</span> = <span>$cache</span>->get("identity_keyword"<span>)

    </span><span>if</span>(<span>$results</span> == <span>null</span><span>) {
        </span><span>$results</span> = cURL->get("http://www.youtube.com/api/json/url/keyword/page"<span>);
        </span><span>//</span><span> Write to Cache Save API Calls next time</span>
        <span>$cache</span>->set("identity_keyword", <span>$results</span>, 3600*24<span>);
    }

    </span><span>foreach</span>(<span>$results</span> <span>as</span> <span>$video</span><span>) {
        </span><span>//</span><span> Output Your Contents HERE</span>
    }
Copy after login


全页缓存

<span>php
    </span><span>//</span><span> use Files Cache for Whole Page / Widget

    // keyword = Webpage_URL</span>
    <span>$keyword_webpage</span> = <span>md5</span>(<span>$_SERVER</span>['HTTP_HOST'].<span>$_SERVER</span>['REQUEST_URI'].<span>$_SERVER</span>['QUERY_STRING'<span>]);
    </span><span>$html</span> = __c("files")->get(<span>$keyword_webpage</span><span>);

    </span><span>if</span>(<span>$html</span> == <span>null</span><span>) {
        </span><span>ob_start</span><span>();
        </span><span>/*</span><span>
            ALL OF YOUR CODE GO HERE
            RENDER YOUR PAGE, DB QUERY, WHATEVER
        </span><span>*/</span>

        <span>//</span><span> GET HTML WEBPAGE</span>
        <span>$html</span> = <span>ob_get_contents</span><span>();
        </span><span>//</span><span> Save to Cache 30 minutes</span>
        __c("files")->set(<span>$keyword_webpage</span>,<span>$html</span>, 1800<span>);
    }

    </span><span>echo</span> <span>$html</span>;
Copy after login


挂件缓存

<span>php
    </span><span>//</span><span> use Files Cache for Whole Page / Widget</span>
    <span>$cache</span> = phpFastCache("files"<span>);

    </span><span>$html</span> = <span>$cache</span>-><span>widget_1;

    </span><span>if</span>(<span>$html</span> == <span>null</span><span>) {
        </span><span>$html</span> = Render Your Page || Widget || "Hello World"<span>;
        </span><span>//</span><span> Save to Cache 30 minutes</span>
        <span>$cache</span>->widget_1 = <span>array</span>(<span>$html</span>, 1800<span>);
    }

    </span><span>echo</span> or <span>return</span> your <span>$html</span>;
Copy after login


同时使用多种缓存

<span>php
    </span><span>//</span><span> in your config files</span>
    <span>include</span>("phpfastcache/phpfastcache.php"<span>);
    </span><span>//</span><span> auto | memcache | files ...etc. Will be default for $cache = __c();</span>
    phpFastCache::<span>$storage</span> = "auto"<span>;

    </span><span>$cache1</span> =<span> phpFastCache();

    </span><span>$cache2</span> = __c("memcache"<span>);
    </span><span>$server</span> = <span>array</span>(<span>array</span>("127.0.0.1",11211,100), <span>array</span>("128.5.1.3",11215,80<span>));
    </span><span>$cache2</span>->option("server", <span>$server</span><span>);

    </span><span>$cache3</span> = <span>new</span> phpFastCache("apc"<span>);

    </span><span>//</span><span> How to Write?</span>
    <span>$cache1</span>->set("keyword1", "string|number|array|object", 300<span>);
    </span><span>$cache2</span>->keyword2 = <span>array</span>("something here", 600<span>);
    __c()</span>->keyword3 = <span>array</span>("array|object", 3600*24<span>);

    </span><span>//</span><span> How to Read?</span>
    <span>$data</span> = <span>$cache1</span>->get("keyword1"<span>);
    </span><span>$data</span> = <span>$cache2</span>-><span>keyword2;
    </span><span>$data</span> = __c()-><span>keyword3;
    </span><span>$data</span> = __c()->get("keyword4"<span>);

    </span><span>//</span><span> Free to Travel between any caching methods</span>

    <span>$cache1</span> = phpFastCache("files"<span>);
    </span><span>$cache1</span>->set("keyword1", <span>$value</span>, <span>$time</span><span>);
    </span><span>$cache1</span>->memcache->set("keyword1", <span>$value</span>, <span>$time</span><span>);
    </span><span>$cache1</span>->apc->set("whatever", <span>$value</span>, 300<span>);

    </span><span>$cache2</span> = __c("apc"<span>);
    </span><span>$cache2</span>->keyword1 = <span>array</span>("so cool", 300<span>);
    </span><span>$cache2</span>->files->keyword1 = <span>array</span>("Oh yeah!", 600<span>);

    </span><span>$data</span> = __c("memcache")->get("keyword1"<span>);
    </span><span>$data</span> = __c("files")->get("keyword2"<span>);
    </span><span>$data</span> = __c()-><span>keyword3;

    </span><span>//</span><span> Multiple ? No Problem</span>

    <span>$list</span> = <span>$cache1</span>->getMulti(<span>array</span>("key1","key2","key3"<span>));
    </span><span>$cache2</span>->setMulti(<span>array</span>("key1","value1", 300),
                      <span>array</span>("key2","value2", 600),
                      <span>array</span>("key3","value3", 1800),<span>
                      );

    </span><span>$list</span> = <span>$cache1</span>->apc->getMulti(<span>array</span>("key1","key2","key3"<span>));
    __c()</span>->memcache->getMulti(<span>array</span>("a","b","c"<span>));

    </span><span>//</span><span> want more? Check out document in source code</span>
Copy after login

 

以上demo来自官网示例。


官网地址:http://www.phpfastcache.com/

 

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 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks 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

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

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.

See all articles