PHP缓存集成库phpFastCache用法

这篇文章主要介绍了PHP缓存集成库phpFastCache用法,包括基本用法的分析与操作实例,在PHP项目开发中非常具有实用价值,需要的朋友可以参考下

本文实例讲述了PHP缓存集成库phpFastCache用法。分享给大家供大家参考。具体分析如下:

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

代码如下:

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

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

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

全页缓存,代码如下:

  1. <?php
  2. // use Files Cache for Whole Page / Widget
  3. // keyword = Webpage_URL
  4. $keyword_webpage = md5($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']);
  5. $html = __c("files")->get($keyword_webpage);
  6. if($html == null) {
  7. ob_start();
  8. /*
  9. ALL OF YOUR CODE GO HERE
  10. RENDER YOUR PAGE, DB QUERY, WHATEVER
  11. */
  12. // GET HTML WEBPAGE
  13. $html = ob_get_contents();
  14. // Save to Cache 30 minutes
  15. __c("files")->set($keyword_webpage,$html, 1800);
  16. }
  17. echo $html;

挂件缓存,代码如下:

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

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

  1. <?php
  2. // in your config files
  3. include("phpfastcache/phpfastcache.php");
  4. // auto | memcache | files ...etc. Will be default for $cache = __c();
  5. phpFastCache::$storage = "auto";
  6. $cache1 = phpFastCache();
  7. $cache2 = __c("memcache");
  8. $server = array(array("127.0.0.1",11211,100), array("128.5.1.3",11215,80));
  9. $cache2->option("server", $server);
  10. $cache3 = new phpFastCache("apc");
  11. // How to Write?
  12. $cache1->set("keyword1", "string|number|array|object", 300);
  13. $cache2->keyword2 = array("something here", 600);
  14. __c()->keyword3 = array("array|object", 3600*24);
  15. // How to Read?
  16. $data = $cache1->get("keyword1");
  17. $data = $cache2->keyword2;
  18. $data = __c()->keyword3;
  19. $data = __c()->get("keyword4");
  20. // Free to Travel between any caching methods
  21. $cache1 = phpFastCache("files");
  22. $cache1->set("keyword1", $value, $time);
  23. $cache1->memcache->set("keyword1", $value, $time);
  24. $cache1->apc->set("whatever", $value, 300);
  25. $cache2 = __c("apc");
  26. $cache2->keyword1 = array("so cool", 300);
  27. $cache2->files->keyword1 = array("Oh yeah!", 600);
  28. $data = __c("memcache")->get("keyword1");
  29. $data = __c("files")->get("keyword2");
  30. $data = __c()->keyword3;
  31. // Multiple ? No Problem
  32. $list = $cache1->getMulti(array("key1","key2","key3"));
  33. $cache2->setMulti(array("key1","value1", 300),
  34. array("key2","value2", 600),
  35. array("key3","value3", 1800),
  36. );
  37. $list = $cache1->apc->getMulti(array("key1","key2","key3"));
  38. __c()->memcache->getMulti(array("a","b","c"));
  39. // want more? Check out document in source code

希望本文所述对大家的PHP程序设计有所帮助。