What are the caching techniques that can be implem

2019-08-17 07:36发布

I need to know all the caching techniques that can be implemented in a website, so that I could speed up the performance. I never needed caching before, so I am totally unaware of how to do it or even what to do, and anything about it.

I am looking for some tutorials, articles or anything helpful.

5条回答
三岁会撩人
2楼-- · 2019-08-17 08:04

I would start with this: http://www.mnot.net/cache_docs/

It is very in-depth and by the time you finish reading it you'll be caching in no time flat. There are several things you may want to consider before delving into the cache abyss, though. Such things as placing all of your tags at the bottom of your page. Compressing both your CSS and your JS files is great to do as well.

At any rate, if you really want to start using cache then I suggest setting HTTP headers first.

Here is one way:

$ cd /path/to/webroot
$ vi .htaccess

Then within your .htaccess file, place the following lines:

<IfModule mod_expires.c>
    Header set Cache-Control "max-age=7200, public"
</IfModule>

This is just a quick and dirty example, which can also be done using header() in PHP. This is for browser cache, though, keep in mind. If you want to do page caching (like storing the page output in memory on the server) then I recommend checking out this page:

http://php.net/manual/en/function.ob-start.php

Good luck!

查看更多
3楼-- · 2019-08-17 08:09

For caching techniques INSIDE your website code, you could take a look at the Symfony project HTTP cache component. They also provide an extensive chapter in their documentation explaining why they choose for HTTP caching, and also explain all the caching options in detail. You can find the chapter here: http://symfony.com/doc/current/book/http_cache.html It is definitely a good read.

Another easy option is to install an opcode cache / PHP accelerator, but this requires access to your webserver.

查看更多
\"骚年 ilove
4楼-- · 2019-08-17 08:09

A kind of soft answer that offers an overview. There are two types of caching:

  1. Server-side
  2. Client-side

Server-side caching involves saving the result of expensive or time-consuming operations (like a call to the DB or to an external web-service) someplace on the server side. Typical storage includes memory-based cache like Memcached or simple filesystem storage.

I usually create a repository or service object that composes my data access and a cache mechanism. For example, a Zend_Cache instance can be created using Zend_Cache::factory($frontend, $backend), where $frontend and $backend are associative arrays describing the caching mechanism, much of which is usually specified in config/application.ini. The repository/service class then checks the cache before initiating the expensive operation. If the cache does not contain the stored data, then the expensive operation is executed and the result is stored in the cache. Full details of the caching itself can be found in the Zend_Cache section of the manual.

Client-side caching involves telling the browser - and intermediaries like proxies - to store certain resources like images, javascripts, and stylesheets - on the client-side. This typically involves sending the correct cache headers describing the desired behavior. This can be implemented on a per folder using .htaccess directives or on a per-request basis by putting the right headers into the $response object.

Employing both server-side and client-side can produce significant performance gains for your application.

Like I said, kind of a soft answer. Happy to fatten with additional specifics on request.

查看更多
迷人小祖宗
5楼-- · 2019-08-17 08:16

You can use php Header() function for caching a single file.

$expire_time = 3600 * 24 * 30; //30 days
header('Expires: ' . gmdate('D, d M Y H:i:s ', time() + $expire_time) . 'GMT');

Alternatively in some cases it's better use directives in .htaccess file.

<IfModule mod_headers.c>
     # 3 month
     <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
         Header set Cache-Control "max-age=7257600, public"
     </FilesMatch>

     # 2 DAYS
     <FilesMatch "\.(xml|txt)$">
         Header set Cache-Control "max-age=172800, public, must-revalidate"
     </FilesMatch>

     # 45 MIN
     <FilesMatch "\.(html|htm)$">
         Header set Cache-Control "max-age=2700, public, must-revalidate"
     </FilesMatch>

     # No cache
     <FilesMatch "\.(pl|php|cgi|spl)$">
         Header unset Cache-Control
         Header unset Expires
         Header unset Last-Modified
         FileETag None
         Header unset Pragma
     </FilesMatch>
</IfModule>

For a in-depth examination you can read this Speed Up Sites with htaccess Caching.

查看更多
别忘想泡老子
6楼-- · 2019-08-17 08:20

You can look into memcached if you got multiple servers, its based on key-value pairs to access the data quickly from a distributed memory (sort of).

You can also store the less frequently changed pages on disk and serve them without creating them everytime. This is simplest caching system but works.

There are many caching solutions available like: Zend, APC etc.

Also see, if you can tune-up your dbms first. As its the main bottle-neck mostly.

查看更多
登录 后发表回答