>백엔드 개발 >PHP 튜토리얼 >PHP 캐싱 라이브러리 phpFastCache 사용에 대해

PHP 캐싱 라이브러리 phpFastCache 사용에 대해

WBOY
WBOY원래의
2016-08-08 09:33:131263검색

phpfastcache는 동적 웹 애플리케이션의 속도를 높이고 데이터베이스 로드를 줄이는 데 사용할 수 있는 다용도의 고성능 분산 개체 캐싱 시스템입니다.
phpfastcache는 데이터베이스 로드를 거의 0으로 줄여 사용자의 페이지 로드 시간을 단축하고 리소스 활용도를 향상시킵니다. 간단하면서도 강력합니다.

데이터베이스 쿼리 감소

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

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


전체 페이지 캐시

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


위젯 캐시

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


동시에 여러 캐시 사용

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

위의 데모는 공식 웹사이트 예시에서 가져온 것입니다.


공식 홈페이지 주소: http://www.phpfastcache.com/

위 내용은 phpFastCache의 내용을 포함하여 PHP 캐시 라이브러리인 phpFastCache의 사용법을 소개하고 있으며, PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

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