国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP緩存集成庫phpFastCache學習教程

2024-05-04 21:49:48
字體:
來源:轉載
供稿:網友

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來定義緩存的有效時間,代碼如下:

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

提高cURL和API調用性能,代碼如下:

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

全頁緩存,代碼如下:

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

掛件緩存,代碼如下:

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

同時使用多種緩存,代碼如下:

  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.  
  7. $cache1 = phpFastCache(); 
  8.  
  9. $cache2 = __c("memcache"); 
  10. $server = array(array("127.0.0.1",11211,100), array("128.5.1.3",11215,80)); 
  11. $cache2->option("server"$server); 
  12.  
  13. $cache3 = new phpFastCache("apc"); 
  14.  
  15. // How to Write? 
  16. $cache1->set("keyword1""string|number|array|object", 300); 
  17. $cache2->keyword2 = array("something here", 600); 
  18. __c()->keyword3 = array("array|object", 3600*24); 
  19.  
  20. // How to Read? 
  21. $data = $cache1->get("keyword1"); 
  22. $data = $cache2->keyword2; 
  23. $data = __c()->keyword3; 
  24. $data = __c()->get("keyword4"); 
  25.  
  26. // Free to Travel between any caching methods 
  27.  
  28. $cache1 = phpFastCache("files"); 
  29. $cache1->set("keyword1"$value$time); 
  30. $cache1->memcache->set("keyword1"$value$time); 
  31. $cache1->apc->set("whatever"$value, 300); 
  32.  
  33. $cache2 = __c("apc"); 
  34. $cache2->keyword1 = array("so cool", 300); 
  35. $cache2->files->keyword1 = array("Oh yeah!", 600); 
  36.  
  37. $data = __c("memcache")->get("keyword1"); 
  38. $data = __c("files")->get("keyword2"); 
  39. $data = __c()->keyword3; 
  40.  
  41. // Multiple ? No Problem 
  42.  
  43. $list = $cache1->getMulti(array("key1","key2","key3")); 
  44. $cache2->setMulti(array("key1","value1", 300), 
  45.                   array("key2","value2", 600), 
  46.                   array("key3","value3", 1800), 
  47.                   ); 
  48.  
  49. $list = $cache1->apc->getMulti(array("key1","key2","key3")); 
  50. __c()->memcache->getMulti(array("a","b","c")); 
  51.  
  52. // want more? Check out document in source code 
  53. ?>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 加查县| 德保县| 吴桥县| 日喀则市| 德昌县| 普宁市| 南乐县| 绥滨县| 延川县| 高雄县| 沁阳市| 峨眉山市| 克东县| 临清市| 建瓯市| 临安市| 江陵县| 淄博市| 五原县| 正安县| 太谷县| 安乡县| 鸡泽县| 涪陵区| 柏乡县| 武城县| 商城县| 蓬安县| 尼勒克县| 洪泽县| 莱阳市| 肃北| 宜君县| 博爱县| 阜康市| 株洲县| 江达县| 阳曲县| 双峰县| 铜陵市| 清新县|