PDA

View Full Version : Cache آدرس (عکس و...) داینامیک در php



abolfazl-z
چهارشنبه 30 مرداد 1392, 22:51 عصر
سلام دوستان

دوستان چطوری میتونیم آدرس عکس های داینامیک مثلا http://localhost/?id=1 را کش کنیم ؟
اکسپایر میزارم نمیشه
Etag نمیشه !
دوستان کدی سراغ ندارید(هدر)?؟

AliRezaPro
پنج شنبه 31 مرداد 1392, 00:55 صبح
ببینید :

http://stackoverflow.com/questions/3787125/how-to-cache-dynamic-php-page

abolfazl-z
پنج شنبه 31 مرداد 1392, 08:50 صبح
کمکی نکرد:ناراحت:

AliRezaPro
پنج شنبه 31 مرداد 1392, 11:16 صبح
این کد را بررسی کنید :

$cachefile = 'cache.html';
$cachetime = 4 * 60;
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
include($cachefile);
echo "<!-- Cached copy, generated ".date('H:i', filemtime($cachefile))." -->\n";
exit;
}
ob_start(); // Start the output buffer

/* Heres where you put your page content */

// 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
و
همینطور اینجا :

http://www.phpfastcache.com/

abolfazl-z
پنج شنبه 31 مرداد 1392, 11:45 صبح
من یک تابع دارم بصورت زیر :

function resizePic($path,$height,$width)
{
list($w, $h,$type) = getimagesize($path);
$type = image_type_to_mime_type($type);
$expires = 60*60*24*14;
$etag = md5_file($path);
header("Content-Type: $type");
header("Pragma: public");
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($path)) . ' GMT');
header("Cache-Control: maxage=".$expires);
header("Etag: $etag");
header('Expires: ' . gmdate('D, d M Y H:i:s', strtotime('+1 year')) . ' GMT'); $newwidth = $height;
$newheight =$width;
$thumb = imagecreatetruecolor($newwidth, $newheight);
switch($type)
{
case 'image/gif':
$source = imagecreatefromgif($path);
break;
case 'image/png':
$source = imagecreatefrompng($path);
break;
case 'image/jpeg':
$source = imagecreatefromjpeg($path);
break;
case 'image/bmp':
$source = imagecreatefromwbmp($path);
break;
}
imagealphablending($thumb, false);
imagesavealpha($thumb, true);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $w, $h);

switch($type)
{
case 'image/gif':
imagegif($thumb);
break;
case 'image/png':
imagepng($thumb,null,9);
break;
case 'image/jpeg':
imagejpeg($thumb,null, 99);
break;
case 'image/bmp':
imagewbmp($thumb);
break;
}
imagedestroy($thumb);
imagedestroy($source);
unset($thumb);
unset($source);
}

حالا من این رو توی یک صفحه استفاده می کنم.
خوب کش نمیشه ؟
ببینید برای شما میشه (بنده روی لوکال تست کردم)

abolfazl-z
پنج شنبه 31 مرداد 1392, 15:56 عصر
علیرضا جان نمیدونم ولی کش شد.دلیل اش شاید برای آپاچی بوده.
باز هم ممنون