Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 是否可以缓存网站的一部分(背景、导航栏)而不是整个文件?_Php_Css_Caching_Web_Bandwidth - Fatal编程技术网

Php 是否可以缓存网站的一部分(背景、导航栏)而不是整个文件?

Php 是否可以缓存网站的一部分(背景、导航栏)而不是整个文件?,php,css,caching,web,bandwidth,Php,Css,Caching,Web,Bandwidth,我只想缓存一个元素,因为它用于我的每个网页。来看看这张图片。这里有解释 不是你想的那样。这将是服务器端缓存。不是客户端。客户端只缓存页面的元素,如图像、视频等。除非整个页面本身告诉客户端缓存它,并且它不会改变以前缓存的版本,否则客户端不会缓存实际的HTML 因此,在您的示例中,所有这些部分中的图像都被缓存,而不是HTML。您所能做的最好的事情就是将这些部分分解成它们自己的文件并缓存这些服务器端 最大的问题是为什么要缓存它们?如果是为了节省带宽,那么服务器端缓存显然不会给您带来任何帮助 您可以将以

我只想缓存一个元素,因为它用于我的每个网页。来看看这张图片。这里有解释

不是你想的那样。这将是服务器端缓存。不是客户端。客户端只缓存页面的元素,如图像、视频等。除非整个页面本身告诉客户端缓存它,并且它不会改变以前缓存的版本,否则客户端不会缓存实际的HTML

因此,在您的示例中,所有这些部分中的图像都被缓存,而不是HTML。您所能做的最好的事情就是将这些部分分解成它们自己的文件并缓存这些服务器端

最大的问题是为什么要缓存它们?如果是为了节省带宽,那么服务器端缓存显然不会给您带来任何帮助

您可以将以下内容添加到.htaccess文件:

<FilesMatch "\.(js)$">
Header set Cache-Control "max-age=29030400, public"
</FilesMatch>

<FilesMatch "\.(css)$">
Header set Cache-Control "max-age=7257600, public"
</FilesMatch>

<FilesMatch "\.(jpg)$">
Header set Cache-Control "max-age=29030400, public"
</FilesMatch>

<FilesMatch "\.(gif)$">
Header set Cache-Control "max-age=29030400, public"
</FilesMatch>

<FilesMatch "\.(png)$">
Header set Cache-Control "max-age=29030400, public"
</FilesMatch>

<FilesMatch "\.(mp4)$">
Header set Cache-Control "max-age=29030400, public"
</FilesMatch>

<FilesMatch "\.(flv)$">
Header set Cache-Control "max-age=29030400, public"
</FilesMatch>

标题集缓存控制“最大年龄=29030400,公共”
标题集缓存控制“最大年龄=7257600,公共”
标题集缓存控制“最大年龄=29030400,公共”
标题集缓存控制“最大年龄=29030400,公共”
标题集缓存控制“最大年龄=29030400,公共”
标题集缓存控制“最大年龄=29030400,公共”
标题集缓存控制“最大年龄=29030400,公共”
这将缓存页面上可由客户端缓存的所有元素

如果您试图解决服务器负载问题,可以使用不同的技术缓存元素的不同部分。例如,Memcache可以缓存MySQL请求,然后使用PHP内置的操作码缓存,这样就不会在每次运行文件时编译PHP。在这种情况下,最好将这些部分分解成它们自己的文件,并将它们“包含”到索引页中

对于使用Memcached的MySQL缓存,您可以将代码包装在请求中,以查看缓存是否存在,然后在代码末尾保存缓存的MySQL结果以启动缓存。看起来是这样的:

// this little portion is standard if you have Memcache compiled into PHP.

$memcache_obj = new Memcache;
$memcache_obj->connect('127.0.0.1', 11211);

// We try to load the memcache result if it exists
$profile = $memcache_obj->get("YouPickThisNameOnYourOwn");

// Did it exist? If NOT get it from MySQL
if (!is_array($profile)) {

$mysqli = new MySQLi("localhost", "MYSQL_UserName","MYSQL_Password","MYSQL_Database");
    $sqlquery = $mysqli->query("SELECT whatever you are selecting");

// Run through and make the array you want normally...
    for ($x=0;$x < $sqlquery->num_rows;$x++) {
        $rowprofiles = $sqlquery->fetch_assoc();

    // Here I am creating an array with the results and I had selected id, text_description, subdir, crc, url from MYSQL
    $profile[$x] = array ("id" => $rowprofiles['id'], "text" => $rowprofiles['text_description'], "pic" => "http://www.domain.com/{$rowprofiles["subdir"]}/{$rowprofiles["crc"]}.jpg", "url" => $rowprofiles['url']);
    }
    // freeing up $sqlquery resource
    $sqlquery->free();

    // Here I am saving the result of the query as an associative array into memcache with a short time limite of 300 seconds. You might want longer for non changing data
    $memcache_obj->add('YouPickThisNameOnYourOwn', $profile, false, 300);

    // if I ended up using a query instead of memcache, close out the connection
    if (isset($mysqli)) { $mysqli->close(); }
}

// end if the if... had it been memcache'd the script would bypass the query and just come here with the array and spit it out
foreach ($profile as $piece) {
    echo '<div class="gallery"><img  src="'.$piece['pic'].'" alt="'.$piece['text'].'"  width="300" height="225"  border="1" /></div>';
    echo '<div class="gallery">'.substr($piece['text'],0,192).'</div>';
}

// hope this helps!
// You can run a file with just <? phpinfo(); ?> inside it to see if your host already has memcache compiled into your PHP installation.
//如果将Memcache编译成PHP,那么这一小部分是标准的。
$memcache_obj=新的memcache;
$memcache_obj->connect('127.0.0.1',11211);
//我们尝试加载memcache结果(如果存在)
$profile=$memcache_obj->get(“YouPickThisNameOnYourOwn”);
//它存在吗?如果没有,请从MySQL获取
如果(!is_数组($profile)){
$mysqli=newmysqli(“localhost”、“MYSQL\u用户名”、“MYSQL\u密码”、“MYSQL\u数据库”);
$sqlquery=$mysqli->query(“选择您正在选择的内容”);
//运行并正常生成所需的阵列。。。
对于($x=0;$x<$sqlquery->num_行;$x++){
$rowprofiles=$sqlquery->fetch_assoc();
//在这里,我用结果创建了一个数组,我从MYSQL中选择了id、text_description、subdir、crc和url
$profile[$x]=数组(“id”=>$ROWSPROFILES['id'],“text”=>$ROWSPROFILES['text\u description'],“pic”=>”http://www.domain.com/{$rowprofiles[“subdir”]}/{$rowprofiles[“crc”]}.jpg,“url”=>$rowprofiles['url']);
}
//释放$sqlquery资源
$sqlquery->free();
//在这里,我将查询结果作为关联数组保存到memcache中,时间限制很短,为300秒。对于不更改的数据,您可能需要更长的时间
$memcache_obj->add('YouPickThisNameOnYourOwn',$profile,false,300);
//如果我最终使用了查询而不是memcache,请关闭连接
if(isset($mysqli)){$mysqli->close();}
}
//如果。。。如果是memcache的话,脚本将绕过查询,带着数组来到这里并将其吐出
foreach($profile作为$piece){
回声';
回显'.substr($piece['text'],0192)。'';
}
//希望这有帮助!
//您可以运行一个文件,看看您的主机是否已经在PHP安装中编译了memcache。

在过去,可能有人会建议使用帧,但现在不应该只重新加载这些位,这真的不应该成为问题。任何图像都会被浏览器缓存,html的大小应该很小。为了理解这个概念,您可能需要考虑使用React或AngularJSThx之类的工具编写更多的单一页面类型的web应用程序!wew。大多数人不明白这一点!U问“最大的问题是为什么要缓存它们?如果是为了节省带宽,那么服务器端缓存显然不会提供任何帮助。”但如果我可以缓存红色矩形框中的元素,那么确实可以节省带宽。无论是客户端(html/Js/CSS/Jquery)还是服务器端(PHP、MySql),数据都必须传输给用户!图片、文字是每次打开我的网站时都会被特别调用的数据。这将节省大量带宽b/c元素可以从本地浏览器存储中调用!您能解释一下您在.htaccess上做了什么吗?是的,.htaccess告诉客户端浏览器他们应该将这些特定对象缓存一年。29030400是秒,等于336天。所以不是一整年。只有在特定内容没有更改的情况下,您才会希望这样做。比如,如果你每个月都要为一次促销而更改头文件。它需要指定自己的头,否则不会更新。那么.htaccess代码会缓存您编写的每个文件扩展名吗?