4

私はPHPコードでSmartyを使用しています.Webサイトページのいくつかをキャッシュしたいので、次のコードを使用しました:

// TOP of script
ob_start();   // start the output buffer
$cachefile ="cache/cachefile.html";
// normal PHP script 
$smarty->display('somefile.tpl.html') ;
$fp = fopen($cachefile, 'w'); // open the cache file for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close the file
ob_end_flush(); // Send the output to the browser

しかし、phpファイルの最後にob_get_contents()を出力すると、空です! 実際には、作成されたキャッシュ ファイルも空です。それで、smartyを使用しているときにphpでファイルをキャッシュするにはどうすればよいですか?smartyキャッシュを使用できることはわかっていますが、何らかの理由で機能しません。

またAPCキャッシュについて教えてください。それの使い方?私の場合、使用する価値はありますか、データベースクエリをキャッシュするためだけだと思います。それについてのphpマニュアルを読みましたが、何も取得できません:)タンク。

4

2 に答える 2

1

ob_end_flush() を呼び出した後、再度 ob_get_contents() を呼び出しているということですか? その場合、ファイルに書き込んだ内容は PHP のメモリから「削除」されています。

それでも HTML を出力したい場合は、最初に ob_end_flush を変数に保存してから、それを fwrite に渡します。この変数は、ページの後半で使用できます。

于 2009-07-08T16:27:25.613 に答える
1

smarty キャッシュのより完全な例として、ドキュメント (ここにあります) のコードの一部をマッシュアップしました。また、例で何を使用していたのかわかりませんが、smarty のメソッドを使用してキャッシュを操作する必要があります。

    require('Smarty.class.php');
    $smarty = new Smarty;

    // 1 Means use the cache time defined in this file, 
    // 2 means use cache time defined in the cache itself
    $smarty->caching = 2; 

    // set the cache_lifetime for index.tpl to 5 minutes
    $smarty->cache_lifetime = 300;

    // Check if a cache exists for this file, if one doesn't exist assign variables etc
    if(!$smarty->is_cached('index.tpl')) {
        $contents = get_database_contents();
        $smarty->assign($contents);
    }

    // Display the output of index.tpl, will be from cache if one exists
    $smarty->display('index.tpl');

    // set the cache_lifetime for home.tpl to 1 hour
    $smarty->cache_lifetime = 3600;

    // Check if a cache exists for this file, if one doesn't exist assign variables etc
    if(!$smarty->is_cached('home.tpl')) {
        $contents = get_database_contents();
        $smarty->assign($contents);
    }

    // Display the output of index.tpl, will be from cache if one exists
    $smarty->display('home.tpl');

APC キャッシュに関しては、smarty と同じように機能します。どちらも、特定の期間、データをファイルに保存します。データにアクセスするたびに、キャッシュが有効かどうかを確認し、有効な場合はキャッシュ値を返します。

ただし、smarty を使用しない場合は、APC をそのまま使用できます。
この例では、DB クエリの結果をキャッシュに保存します。同様に、これを変更して、代わりにページ出力全体を保存することもできるため、高価な実行を行う必要はありません。 PHP は頻繁に機能します。

// A class to make APC management easier
class CacheManager  
{  
     public function get($key)  
     {  
          return apc_fetch($key);  
     }  

     public function store($key, $data, $ttl)  
     {  
          return apc_store($key, $data, $ttl);  
     }  

     public function delete($key)  
     {  
          return apc_delete($key);  
     }  
}  

いくつかのロジックと組み合わせると、

function getNews()  
{  
     $query_string = 'SELECT * FROM news ORDER BY date_created DESC limit 5';  

     // see if this is cached first...  
     if($data = CacheManager::get(md5($query_string)))  
     {  
             // It was stored, return the value
          $result = $data;  
     }  
     else  
     {  
             // It wasn't stored, so run the query
          $result = mysql_query($query_string, $link);  
          $resultsArray = array();  

          while($line = mysql_fetch_object($result))  
          {  
               $resultsArray[] = $line;  
          }  

             // Save the result inside the cache for 3600 seconds
          CacheManager::set(md5($query_string), $resultsArray, 3600);  
     }  

     // Continue on with more functions if necessary 
}  

この例は、 hereからわずかに変更されています。

于 2009-07-08T14:35:24.563 に答える