Connecting Tech Pros Worldwide Forums | Help | Site Map

cache problem

yhd1997@gmail.com
Guest
 
Posts: n/a
#2: Jun 2 '08

re: cache problem


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?

Jerry Stuckle
Guest
 
Posts: n/a
#1: Jun 2 '08
yhd1997@gmail.com wrote:
Quote:
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.
jstucklex@attglobal.net
==================

Norman Peelman
Guest
 
Posts: n/a
#3: Jun 2 '08

re: cache problem


yhd1997@gmail.com wrote:
Quote:
OK. It's working now. I guess it's just a path issue.
>
On Apr 13, 11:07 am, yhd1...@gmail.com wrote:
Quote:
>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();
>?>
>>
>>
>>
Quote:
>>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?-
yhd1997@gmail.com
Guest
 
Posts: n/a
#4: Jun 2 '08

re: cache problem


OK. It's working now. I guess it's just a path issue.

On Apr 13, 11:07*am, yhd1...@gmail.com wrote:
Quote:
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();
?>
>
>
>
Quote:
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.
>
Quote:
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================- Hide quoted text-
>
Quote:
- Show quoted text -- Hide quoted text -
>
- Show quoted text -
yhd1997@gmail.com
Guest
 
Posts: n/a
#5: Jun 2 '08

re: cache problem


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();
?>



Quote:
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 -
C. (http://symcbean.blogspot.com/)
Guest
 
Posts: n/a
#6: Jun 2 '08

re: cache problem


On 13 Apr, 14:38, Jerry Stuckle <jstuck...@attglobal.netwrote:
Quote:
yhd1...@gmail.com wrote:
>
Quote:
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?
Quote:
>
Quote:
// Get from the cache
if(file_exists($cache_file) && time() - $cache_time <
filemtime($cache_file)) {
include($cache_file);
exit;
}
>
Quote:
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.
Closed Thread


Similar PHP bytes