Php cache old script 7.2 - php

6php version it's works, on 7.2php it's works too but not currently what can be there wrong? don't offer use another like phpfastcache i need this method.
<?php
//settings
$cache_ext = '.html'; //file extension
$cache_time = 10; //Cache file expires afere these seconds (1 hour = 3600 sec)
$cache_folder = 'cache/'; //folder to store Cache files
$ignore_pages = array('', '');
$dynamic_url = 'http://'.$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . $_SERVER['QUERY_STRING']; // requested dynamic page (full url)
$cache_file = $cache_folder.md5($dynamic_url).$cache_ext; // construct a cache file
$ignore = (in_array($dynamic_url,$ignore_pages))?true:false; //check if url is in ignore list
if (!$ignore && file_exists($cache_file) && time() - $cache_time < filemtime($cache_file)) { //check Cache exist and it's not expired.
ob_start('ob_gzhandler'); //Turn on output buffering, "ob_gzhandler" for the compressed page with gzip.
readfile($cache_file); //read Cache file
echo '<!-- cached page - '.date('l jS \of F Y h:i:s A', filemtime($cache_file)).', Page : '.$dynamic_url.' -->';
ob_end_flush(); //Flush and turn off output buffering
exit(); //no need to proceed further, exit the flow.
}
//Turn on output buffering with gzip compression.
ob_start('ob_gzhandler');
######## Your Website Content Starts Below #########
?>
<?php
######## Your Website Content Ends here #########
if (!is_dir($cache_folder)) { //create a new folder if we need to
mkdir($cache_folder);
}
if(!$ignore){
$fp = fopen($cache_file, 'w'); //open file for writing
fwrite($fp, ob_get_contents()); //write contents of the output buffer in Cache file
fclose($fp); //Close file pointer
}
ob_end_flush(); //Flush and turn off output buffering
?>

Related

php script cache , cache also a admin content

i want increase the speed of my website build in php, i tested to use this script :
top page :
<?php
$url = $_SERVER["SCRIPT_NAME"];
$break = Explode('/', $url);
$file = $break[count($break) - 1];
$cachefile = 'cached-'.substr_replace($file ,"",-4).'.html';
$cachetime = 18000;
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
echo "<!-- Cached copy, generated ".date('H:i', filemtime($cachefile))." -->\n";
include($cachefile);
exit;
}
ob_start(); // Start the output buffer
?>
bottom page :
<?php
// Cache the contents to a file
$cached = fopen($cachefile, 'w');
fwrite($cached, ob_get_contents());
fclose($cached);
ob_end_flush(); // Send the output to the browser
?>
now the script work fine ,but id you are logged in admin area, this script cache also a content that view only when you are logged.
now i want this script cache only when never user is logged.
in my site, the condition if a user are registered or not is this:
if($_info_["partner_loggato"] OR ($_info_["is_superadmin"] OR $_info_["is_admin_autorizzato"]))
now how work this script only when this user is not logged ?, and also this is a good method or it's possible use other alternative?
i have used this code example in the top but compare white page:
<?
if($_info_["partner_loggato"] OR ($_info_["is_superadmin"] OR $_info_["is_admin_autorizzato"]))
{}
else
{
$url = $_SERVER["SCRIPT_NAME"];
$break = Explode('/', $url);
$file = $break[count($break) - 1];
$cachefile = 'cached-'.substr_replace($file ,"",-4).'.html';
$cachetime = 18000;
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
echo "<!-- Cached copy, generated ".date('H:i', filemtime($cachefile))." -->\n";
include($cachefile);
exit;
}
ob_start(); // Start the output buffer
}
?>

PHP ob_start() for file caching

when this sample web page loads, it checks cached file from /cache folder. If there is no cached file, it calls ob_start() and creates a .html cache file. Problem is this caching process is working everytime even I have not called ob_start(). Please advise me. Thank you.
// class file
function check_cache($dynamic_url) {
$cache_file = $this->cache_folder.md5($dynamic_url).$this->cache_ext;
if ((file_exists($cache_file)) && (time() - $this->cache_time < filemtime($cache_file))) {
// ob_start('ob_gzhandler');
readfile($cache_file);
ob_end_flush();
exit();
}
else {
//ob_start('ob_gzhandler');
}
}
function create_cache($dynamic_url) {
$cache_file = $this->cache_folder.md5($dynamic_url).$this->cache_ext;
$fp = fopen($cache_file, 'w');
fwrite($fp, ob_get_contents());
fclose($fp);
ob_end_flush();
}
and I call this class from
$cache->check_cache(get_full_url());
<h1>Today is <?php echo date('Y-m-d h:i:s'); ?></h1>
$cache->create_cache(get_full_url());
I think that your cache is still created because of your create_cache calling fopen no matter what the output buffer is started or not. Therefore, should check your output buffer status before do fopen, as follows:
function create_cache($dynamic_url) {
if (ob_get_level() > 0) {
$cache_file = $this->cache_folder.md5($dynamic_url).$this->cache_ext;
$fp = fopen($cache_file, 'w');
fwrite($fp, ob_get_contents());
fclose($fp);
ob_end_flush();
}
}
You can use ob_get_level or ob_get_status: http://php.net/manual/en/function.ob-get-status.php

webpage caching

I am looking for a best solution for caching my webpages example http:/www.website.com/test.php?d=2011-11-01 which has url rewrite rule to become http:/www.website.com/testd-2011-11-01.html
the scripts below does not work for dynamic web page it give the same page regardless of the query.
<?php
$cachefile = "cache/".$reqfilename.".html";
$cachetime = 240 * 60; // 5 minutes
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && (time() - $cachetime
< filemtime($cachefile)))
{
include($cachefile);
echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))."
-->";
exit;
}
ob_start(); // start the output buffer?>
my website content here
<?php
// open the cache file for writing
$fp = fopen($cachefile, 'w');
// save the contents of output buffer to the file
fwrite($fp, ob_get_contents());
// close the file
fclose($fp);
// Send the output to the browser
ob_end_flush(); ?>
If your URL looks like testd-2011-11-01.html, you have two possible solutions :
Use some RewriteRule, so that URL is rewritten to test.php?d=2011-11-01 ; and, then, your test.php script can deal with the cache generation / invalidation
Or use a cronjob, that will regenerate the testd-2011-11-01.html static file every X minutes.
The first solution is the one that's generally used, as it only requires you to setup a RewriteRule (and those are often available, even on cheap hosting services).
The second solution might be a bit better for performances (no PHP code is ever executed, except when the cronjob runs) ; but the difference is probably not that important, except if you have a very big website with an awful lot of users.
Something like this could work:
class SimpleCache {
private $_cacheDir = '/path/to/cache';
private $_cacheTime = 240*60;
public function isCached( $id ) {
$cacheFilename = $this->_cache . "/" . $id . "_*";
$files = glob($cacheFilename, GLOB_NOSORT);
if( $files && !empty($files) ) {
// There should always be one file in the array
$filename = $files[0];
$params = explode("_", $filename);
$cacheTime = strtok($params[1], '.');
// Check if the cached file is too old
if( time() - $params[1] > $this->_cacheTime ) {
#unlink($filename);
}
else {
return $filename;
}
}
return false;
}
public function cache( $id, $data ) {
$filename = $this->_cache . "/" . $id. "_" . time() . ".cache";
if( !($fp = #fopen($filename, "w")) ) {
return false;
}
if( !#fwrite($fp, $data) ) {
#fclose($fp);
return false;
}
#fclose($fp);
return true;
}
}
$cache = new SimpleCache();
if( !($buffer = $cache->isCached($reqfilename)) ) {
// Produce the contects of the file and save them in the $buffer variable
$cache->cache($reqfilename, $buffer);
}
echo $buffer;
But you could use memcached, APC, and more advanced caching techniques if you are up to it
You can use HTTP caching. You send headers telling the client (browser) to cache the whole page for a certain period of time.
// 5 minutes
header('Cache-Control: max-age=300');
If you have control over your hosting environment, you can also add a reverse proxy like varnish or nginx in front of your webserver. This proxy will then cache these requests for you, making the cached version shared between all visitors of your site.
See also the HTTP/1.1 specification.

Please help to improve my pHp cache ?

i have read a bit around in the internet about the php cache.
at the moment i am using, this system to cache my pages:
This is putted on the start of the page
<?php
// Settings
$cachedir = 'cache/'; // Directory to cache files in (keep outside web root)
$cachetime = 600; // Seconds to cache files for
$cacheext = 'html'; // Extension to give cached files (usually cache, htm, txt)
// Ignore List
$ignore_list = array(
'addedbytes.com/rss.php',
'addedbytes.com/search/'
);
// Script
$page = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; // Requested page
$cachefile = $cachedir . md5($page) . '.' . $cacheext; // Cache file to either load or create
$ignore_page = false;
for ($i = 0; $i < count($ignore_list); $i++) {
$ignore_page = (strpos($page, $ignore_list[$i]) !== false) ? true : $ignore_page;
}
$cachefile_created = ((#file_exists($cachefile)) and ($ignore_page === false)) ? #filemtime($cachefile) : 0;
#clearstatcache();
// Show file from cache if still valid
if (time() - $cachetime < $cachefile_created) {
//ob_start('ob_gzhandler');
#readfile($cachefile);
//ob_end_flush();
exit();
}
// If we're still here, we need to generate a cache file
ob_start();
?>
MY HTML CODE Goes here .............
and the code below is at the footer of my page.
<?php
// Now the script has run, generate a new cache file
$fp = #fopen($cachefile, 'w');
// save the contents of output buffer to the file
#fwrite($fp, ob_get_contents());
#fclose($fp);
ob_end_flush();
?>
There are some things that i need and this code dont have them :
gzip
the expired cache is not autodeleted after it expire.
Also wanted to ask, if this code is secure to use , if some one can suggest a better one or something to improve the current code it will be just great
Thank you fro reading this post.
Best Regards
Meo
….
// Show file from cache if still valid
if (time() - $cachetime < $cachefile_created) {
//ob_start('ob_gzhandler');
echo gzuncompress(file_get_contents($cachefile));
//ob_end_flush();
exit();
} else {
if(file_exists($cachefile) && is_writable($cachefile)) unlink($cachefile)
}
….
and
// Now the script has run, generate a new cache file
$fp = #fopen($cachefile, 'w');
// save the contents of output buffer to the file
#fwrite($fp, gzcompress(ob_get_contents(), 9));
#fclose($fp);
ob_end_flush();
?>
Use ob_start("ob_gzhandler"); to initiate gzipped buffering (it'll take care of determining if the client can actually accept/wants gzipped data and adjust things accordingly).
To delete the cached files:
if (time() - $cachetime < $cachefile_created) {
#readfile($cachefile);
//ob_end_flush();
exit();
} else {
unlink($cachefile);
exit();
}
But there can be delay or maybe error when the file is being written and someone requests for that page. You should use flock to overcome such problems as mentioned at Error during file write in simple PHP caching
Something like this at the end of page
<?php
$fp = #fopen($cachefile, 'w');
if (flock($fp, LOCK_EX | LOCK_NB)) {
fwrite($fp, gzcompress(ob_get_contents(), 9));
flock($fp, LOCK_UN);
fclose($fp);
}
ob_end_flush(); ?>

How to cache dynamic PHP page

How to cache PHP page which has mysql query. Any example will be great and helpful.
I am using phpFastCache ( for shared hosting, if you don't want to touch php.ini and root to setup memcached). Check out the Example Menu. They have full detail example, and very easy.
First you set with phpFastCache::set and then get with phpFastCache::get - DONE!
Example: Reduce Database Calls
Your website have 10,000 visitors who are online, and your dynamic page have to send 10,000 same queries to database on every page load. With phpFastCache, your page only send 1 query to DB, and use the cache to serve 9,999 other visitors.
<?php
// In your config file
include("php_fast_cache.php");
phpFastCache::$storage = "auto";
// you can set it to files, apc, memcache, memcached, pdo, or wincache
// I like auto
// In your Class, Functions, PHP Pages
// try to get from Cache first.
$products = phpFastCache::get("products_page");
if($products == null) {
$products = YOUR DB QUERIES || GET_PRODUCTS_FUNCTION;
// set products in to cache in 600 seconds = 5 minutes
phpFastCache::set("products_page",$products,600);
}
OUTPUT or RETURN your $products
?>
My preference is to use a caching reverse proxy, like Varnish.
As far as a pure PHP solution, you could have some code at the end of your script that caches the final output, and code at the beginning that checks to see if the page is cached. If the page was found in cache, send it and exit rather than running the queries again.
<?php
function cache_file() {
// something to (hopefully) uniquely identify the resource
$cache_key = md5($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . $_SERVER['QUERY_STRING']);
$cache_dir = '/tmp/phpcache';
return $cache_dir . '/' . $cache_key;
}
// if we have a cache file, deliver it
if( is_file( $cache_file = cache_file() ) ) {
readfile( $cache_file );
exit;
}
// cache via output buffering, with callback
ob_start( 'cache_output' );
//
// expensive processing happens here, along with page output.
//
function cache_output( $content ) {
file_put_contents( cache_file(), $content );
return $content;
}
Obviously this needs lots of customization for your setup, including cache expiration, a $cache_key that meets your needs, and error detection so bad pages aren't cached.
memcache your html out and then do something like this:
$memcache = memcache_connect('localhost', 11211);
$page = $memcache->get('homepage');
if($page == ""){
$mtime = microtime();
$page = get_home();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
memcache_set($memcache, 'homepage', $page, 0, 30);
$page .= "\n<!-- Duly stored ($totaltime) -->";
}
else{
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
$page .= "\n<!-- served from memcache ($totaltime) -->";
}
die($page);
<?php
//settings
$cache_ext = '.html'; //file extension
$cache_time = 3600; //Cache file expires afere these seconds (1 hour = 3600 sec)
$cache_folder = 'cache/'; //folder to store Cache files
$ignore_pages = array('', '');
$dynamic_url = 'http://'.$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . $_SERVER['QUERY_STRING']; // requested dynamic page (full url)
$cache_file = $cache_folder.md5($dynamic_url).$cache_ext; // construct a cache file
$ignore = (in_array($dynamic_url,$ignore_pages))?true:false; //check if url is in ignore list
if (!$ignore && file_exists($cache_file) && time() - $cache_time < filemtime($cache_file)) { //check Cache exist and it's not expired.
ob_start('ob_gzhandler'); //Turn on output buffering, "ob_gzhandler" for the compressed page with gzip.
readfile($cache_file); //read Cache file
echo '<!-- cached page - '.date('l jS \of F Y h:i:s A', filemtime($cache_file)).', Page : '.$dynamic_url.' -->';
ob_end_flush(); //Flush and turn off output buffering
exit(); //no need to proceed further, exit the flow.
}
//Turn on output buffering with gzip compression.
ob_start('ob_gzhandler');
######## Your Website Content Starts Below #########
?>
<!DOCTYPE html>
<html>
<head>
<title>Page to Cache</title>
</head>
<body>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer ut tellus libero.
</body>
</html>
<?php
######## Your Website Content Ends here #########
if (!is_dir($cache_folder)) { //create a new folder if we need to
mkdir($cache_folder);
}
if(!$ignore){
$fp = fopen($cache_file, 'w'); //open file for writing
fwrite($fp, ob_get_contents()); //write contents of the output buffer in Cache file
fclose($fp); //Close file pointer
}
ob_end_flush(); //Flush and turn off output buffering
?>
The important thing, often overlooked when there is discussion about caching, is process synchronization to avoid thread race (see: https://en.wikipedia.org/wiki/Race_condition).
Typical caching scenario in PHP without synchronization looks like this: if you don't have resource in the cache, or resource is expired, it must be created and put into cache. First thread/process that happens to encounter such condition is trying to create resource, and during that time, other threads will also create the resource, which leads to thread race, cache slamming and performance downspike.
Problem is magnified by number of concurrent threads and workload created by resource creation task. On busy systems it may lead to serious problems.
There is very few caching systems for PHP that takes it into consideration synchronization.
One of them is php-no-slam-cache: https://github.com/tztztztz/php-no-slam-cache

Categories