Simple way keshirovanija pages
Site dynamic. The part of pages varies seldom, and is hit on them much. A familiar situation? Still. I hope, this clause{article} will help you to speed up a little loading of such pages and to remove{take off} superfluous loading from the server.
We shall consider a simple variant keshirovanija when contents kesha are updated through the certain time intervals. In most cases it suffices. Certainly, in the future we shall consider also more complex cases.
We shall for the beginning be defined{determined}, how will look kesh. I think, that the most simple (and not the most bad) the output{exit} is simply the catalogue with the files named definitely. Names of files will consist from a little bit modified query_string and the rights of the user. From query_string we cut out a mention of session (at different users she different, and page, most likely, same). Rights of the user - the important thing. In fact, for example, the manager often sees page not how usual users. In the given fragment of a code we count, that there is a global variable $user where the information on the user is stored{kept}, and function user_rights which returns rights of the user.
function make_cache_fname () {
global $user;
return ' cache / '.
md5 (preg_replace (" / ^ (. *?) and? ".session_name (). ". * $/si ",
"1",
getenv ('query_string'))).
'.'.
md5 (user_rights ($user));
}
Now we can create function which will read the data from kesha. If she will not find the necessary information, she will return an empty line.
function page_from_cache () {
global $cache_hits, $cache_expirations, $timelimit;
$fname = make_cache_fname ();
if ((file_exists ($fname)) and
(($time_diff = (time () - filemtime ($fname))) <$timelimit)) {
$fchk = fopen ($fname, 'r');
$output = fread ($fchk, filesize ($fname));
fclose ($fchk);
$cache_hits ++;
$tm = $timelimit - $time_diff;
$cache_expirations. = ((strlen ($cache_expirations)> 0)? ',': ").
((strlen ($tm)> 0)? $tm: '0');
return $output;
} // cache hit
else {
return ";
}
}
Well and, certainly, in any way it is impossible to do without addition in kesh. Here all is simple.
function add_to_cache ($txt) {
$fname = make_cache_fname ();
$fchk = fopen ($fname, 'w');
fwrite ($fchk, $txt);
fflush ($fchk);
fclose ($fchk);
}
Well and now - it is used. We declare three global variables - $cache_hits, $cache_expirations, $timelimit. The first will store{keep} quantity{amount} of the pages taken from kesha, the second - the list of times through which the period of storage of the data taken from kesha expires, the third stores{keeps} time through which kesh it is necessary to update.
global $cache_hits, $cache_expirations, $timelimit;
$cache_hits = 0;
$cache_expirations = ";
$timelimit = 150;
//// use cache - begin ////
$cached_page. = page_from_cache ();
if ($cached_page! = ") {
$output. = $cached_page;
} // cache hit
else {
// We establish{install} value $to_cache
add_to_cache ($to_cache);
$output. = $to_cache;
} // no cache hit
//// use cache - end ////
// We use result - $output
Somewhere in the bottom of page we can deduce{remove} statistics:
echo ' cache hit (s): '. $ cache_hits.
'('. ((strlen (trim ($cache_expirations))> 0)? $cache_expirations: '0'). ')';

|