By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,948 Members | 1,573 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,948 IT Pros & Developers. It's quick & easy.

cache problem

P: n/a
Can you please help me to solve this cache problem?

I have a php script that can save the content to a cache folder. But I
can not get it to work to display the cached page.

The script will generate dynamic content by calling: index.php?
operation=1&parameter=2&id=3

It will save the content to cache as operation=1&parameter=2&id=3

At the beginning of the index.php, it will check if the cached file
exist. if it does exist and time is less than cached configuration
time then include the cached file and exit. otherwise save the content
as cached file using the server query string as its name.

However the code to display cache file doesn't work. Here is the code
snipe:

// Get from the cache
if(file_exists($cache_file) && time() - $cache_time <
filemtime($cache_file)) {
include($cache_file);
exit;
}

Is there anything wrong with above code to retrieve the file with a
name like operation=1&parameter=2&id=3?
Jun 2 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
yh*****@gmail.com wrote:
Can you please help me to solve this cache problem?

I have a php script that can save the content to a cache folder. But I
can not get it to work to display the cached page.

The script will generate dynamic content by calling: index.php?
operation=1&parameter=2&id=3

It will save the content to cache as operation=1&parameter=2&id=3

At the beginning of the index.php, it will check if the cached file
exist. if it does exist and time is less than cached configuration
time then include the cached file and exit. otherwise save the content
as cached file using the server query string as its name.

However the code to display cache file doesn't work. Here is the code
snipe:

// Get from the cache
if(file_exists($cache_file) && time() - $cache_time <
filemtime($cache_file)) {
include($cache_file);
exit;
}

Is there anything wrong with above code to retrieve the file with a
name like operation=1&parameter=2&id=3?
There doesn't look to be a problem with the code you showed us. But not
having all the code, it's impossible to tell what might be wrong.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jun 2 '08 #2

P: n/a
Here is the script:

<?
$cache_dir = './cached/';
$cache_file = '';
$cache_param = $_SERVER["QUERY_STRING"];

$cache_time = 3660;

$cache_file = $cache_dir.$cache_file.$cache_param;

// Get from the cache
if(file_exists($cache_file) && time() - $cache_time <
filemtime($cache_file)) {
include($cache_file);
exit;
}
else {
chdir("script");
include("common.php");
}

ob_start();

//code to generate content start
... ...
//code to generate content end

$outputbuf = ob_get_contents();

ob_end_clean();

include("header.php");
echo $outputbuf;
include("footer.php");

chdir("../");

$fp = fopen($cache_file,'w+');
fwrite($fp, $outputbuf);
fclose($fp);
ob_end_flush();
?>

There doesn't look to be a problem with the code you showed us. *But not
having all the code, it's impossible to tell what might be wrong.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================- Hide quoted text -

- Show quoted text -
Jun 2 '08 #3

P: n/a
OK. It's working now. I guess it's just a path issue.

On Apr 13, 11:07*am, yhd1...@gmail.com wrote:
Here is the script:

<?
$cache_dir = './cached/';
$cache_file = '';
$cache_param = $_SERVER["QUERY_STRING"];

$cache_time = 3660;

$cache_file = $cache_dir.$cache_file.$cache_param;

// Get from the cache
if(file_exists($cache_file) && time() - $cache_time <
filemtime($cache_file)) {
* include($cache_file);
* exit;}

else {
* chdir("script");
* * * * include("common.php");

}

* * * * ob_start();

//code to generate content start
... ...
//code to generate content end

* * * * $outputbuf = ob_get_contents();

* * * * ob_end_clean();

* * * * include("header.php");
* * * * echo $outputbuf;
* * * * include("footer.php");

* * * * * chdir("../");

$fp = fopen($cache_file,'w+');
fwrite($fp, $outputbuf);
fclose($fp);
ob_end_flush();
?>
There doesn't look to be a problem with the code you showed us. *But not
having all the code, it's impossible to tell what might be wrong.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================- Hide quoted text-
- Show quoted text -- Hide quoted text -

- Show quoted text -
Jun 2 '08 #4

P: n/a
yh*****@gmail.com wrote:
OK. It's working now. I guess it's just a path issue.

On Apr 13, 11:07 am, yhd1...@gmail.com wrote:
>Here is the script:

<?
$cache_dir = './cached/';
$cache_file = '';
$cache_param = $_SERVER["QUERY_STRING"];

$cache_time = 3660;

$cache_file = $cache_dir.$cache_file.$cache_param;

// Get from the cache
if(file_exists($cache_file) && time() - $cache_time <
filemtime($cache_file)) {
include($cache_file);
exit;}

else {
chdir("script");
include("common.php");

}

ob_start();

//code to generate content start
... ...
//code to generate content end

$outputbuf = ob_get_contents();

ob_end_clean();

include("header.php");
echo $outputbuf;
include("footer.php");

chdir("../");

$fp = fopen($cache_file,'w+');
fwrite($fp, $outputbuf);
fclose($fp);
ob_end_flush();
?>
>>There doesn't look to be a problem with the code you showed us. But not
having all the code, it's impossible to tell what might be wrong.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
Why not install APC or memcache?

--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
Jun 2 '08 #5

P: n/a
On 13 Apr, 14:38, Jerry Stuckle <jstuck...@attglobal.netwrote:
yhd1...@gmail.com wrote:
However the code to display cache file doesn't work. Here is the code
snipe:
What do you mean it doesn't work? It doesn't think the cache file
exists? Is current? Fails to read it? Is there an error message? Have
you checked the file was written and is readable by the webserver uid?
Have you checked the contents of the cached file?
>
// Get from the cache
if(file_exists($cache_file) && time() - $cache_time <
filemtime($cache_file)) {
include($cache_file);
exit;
}
Is there anything wrong with above code to retrieve the file with a
name like operation=1&parameter=2&id=3?

There doesn't look to be a problem with the code you showed us. But not
having all the code, it's impossible to tell what might be wrong.
Error messages (or verification that there are none) would be good
too.

C.
Jun 2 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.