-2
<?php
define('CACHE_PATH', $_SERVER["DOCUMENT_ROOT"]."/cachefiles/");
// how long to keep the cache files (hours)
define('CACHE_TIME', 12); 
// return location and name for cache file 
function cache_file() 
{ 
    return CACHE_PATH . md5($_SERVER['REQUEST_URI']);
}

// display cached file if present and not expired 
function cache_display() 
{ 
    $file = cache_file(); 
    // check that cache file exists and is not too old 
    if(!file_exists($file)) return; 
    if(filemtime($file) < time() - CACHE_TIME * 3600) return; 
    // if so, display cache file and stop processing
    readfile($file);
    exit; 
} 

// write to cache file 
function cache_page($content) 
{ 
    if(false !== ($f = @fopen(cache_file(), 'w'))) { 
        fwrite($f, $content);
        fclose($f); 
    } 
    return $content; 
} 

// execution stops here if valid cache file found
cache_display(); 

// enable output buffering and create cache file 
ob_start('cache_page');
?>

上記のコードは、ランダムな名前でキャッシュを作成します

例えば

4c556ca729a88177e72946a4c3732f62

a87aef8e1d11cee944a8854ab8377ac6

85f2e557d6b483fc06db804d35e6580f

次のように、実際のページ名でキャッシュページを保存するにはどうすればよいですか

ホーム-4c556ca729a88177e72946a4c3732f62

約 a87aef8e1d11cee944a8854ab8377ac6

私-85f2e557d6b483fc06db804d35e6580f

4

1 に答える 1

1

OPの問題はcache_file()メソッドにあります。このメソッドは、新しいファイルを検索または作成するときに、キャッシュされたファイルの名前の構築を処理します。

$_SERVER['REQUEST_URI']

ページ全体と変数 (例/index.php?var=1) を返します。

では、要求のハッシュを取得し、新しいファイルへのフル パスをcache_file()構築することで、名前を構築します。MD5

CACHE_PATH . md5($_SERVER['REQUEST_URI']);

OP の要求は、キャッシュされたファイル名に home/about/me を表示するように要求を変更することでした。これは、名前の URI を解析するか、単にファイルの名前を使用することで実行できます。http://domain.tld/index.php例としてno rewire を使用します。

basename(__FILE__, ".php"); //gives index on index.php

これを使用して、次に変更します

CACHE_PATH .basename(__FILE__, ".php") . "-" . md5($_SERVER['REQUEST_URI']);

以下は与えるでしょう

../path/to/cache/index-{md5hash}
于 2013-03-05T19:35:23.917 に答える