Squid Cache Settings-control the cache time of the page

Source: Internet
Author: User
Tags current time min php file regular expression time limit

The role of Refresh_pattern:
Used to determine how long a page stays in the cache after it enters the cache.

Refresh_pattern only works on a page where the backend does not have a expires expiration, such as a forum page, and does not work on pages similar to Apache Mod_expires.

Grammar:
Refresh_pattern [-i] regexp min percent max [options]

Several concepts:
Resource age = Time the object entered the cache-the last_modified of the object
Response Age = Current time-The time the object entered the cache
Lm-factor= (response Age)/(Resource age)


For example, only percent is considered here, regardless of Min and Max

Example: Refresh_pattern 20%

Suppose the source server www.aaa.com/index.htm-----lastmodified is 2007-04-10 02:00:00
Squid on proxy.aaa.com/index.htm index.htm Enter cache time 2007-04-10 03:00:00

1) If the current time 2007-04-10 03:00:00
Resource age = 3 points-2 points = 60 Minutes
Response age = 0 minutes
Index.htm can also stay at cache time (resource age) *20%=12 minutes
In other words, Index.htm enters the cache, can stay for 12 minutes before being re-confirmed.


2) If the current time 2007-04-10 03:05:00
Resource age = 3 points-2 points = 60 Minutes
Response age = 5 minutes
Index.htm can also stay at cache time (resource age) *20%=12 minutes -5=7
lm-factor=5/60=8.3%<20%

Until 2007-04-10 03:12:00 lm-factor=12/60=20%, the page in the cache index.htm finally stale.
If there is no index.htm request, index.htm will always be in the cache, if there is a index.htm request, Squid received the request, because it has expired, squid will send a index.htm to the source server whether there is a change in the request, if the source server received, if index.htm No update, squid will not have to update the cache, directly put the cached content back to the client, while resetting the object to enter the cache time for the source server to confirm the time, such as 2007-04-10 03:13:00, if the page is re-confirmed after this. After resetting, the resource age becomes longer and the corresponding time to survive in the cache becomes longer.

If there is a change, return the latest index.htm to Squid,squid to update the cache, and then return the new index.htm to the client, and recalculate resource age based on the last_modified and time of page fetch in the new page. Further calculate the survival time.

In fact, after a page enters the cache, his survival time is determined, i.e. (resource age) * percent, until it is re-confirmed.

After understanding the percentages, Min Max understands

When squid receives a page request:
1. Calculate the age of response,
2, if the response age<min fresh if response Age>max then stale
3, if response age between, if response time < survival time, fresh, otherwise stale

The meaning of several commonly used parameters

Override-expire
This option causes Squid to check the Min value before checking the expires head. Thus, a non-zero min time allows squid to return an unconfirmed cache hit, even if the response is ready to expire.

Override-lastmod
The re-election causes Squid to check the Min value before checking the percentage of lm-factor.

Reload-into-ims
This option allows squid to send a request with the no-cache instruction in the confirmation request. In other words, Squid adds a if-modified-since header to the request before forwarding the request. Note that this only works when the target has a last-modified timestamp. The request that comes in outside keeps the no-cache instruction so that it reaches the original server.
Reload-into-ims can be used for general conditions. It is actually the time-out of the Force control object, which violates the spirit of the HTTP protocol, but in the case of narrow bandwidth, it can improve the apparent system corresponding time.
Example:
refresh_pattern-i \.css$ 1440 50% 129600 reload-into-ims
refresh_pattern-i \.xml$ 1440 50% 129600 Reloa D-into-ims
refresh_pattern-i \.html$ 1440 90% 129600 reload-into-ims-
refresh_pattern-i \.shtml$ 1440 90% 129600 Reload-into-ims
refresh_pattern-i \.hml$ 1440 90% 129600 reload-into-ims
refresh_pattern-i \.jpg$ 1440 90% 1296 XX reload-into-ims
refresh_pattern-i \.png$ 1440 90% 129600 reload-into-ims
refresh_pattern-i \.gif$ 1440 90% 12 9600 ignore-reload
refresh_pattern-i \.bmp$ 1440 90% 129600 reload-into-ims
refresh_pattern-i \.js$ 1440 90% 129 Reload-into-ims

Ignore-reload
This option causes Squid to ignore any no-cache instructions in the request.
So. If you want the content to enter the cache will not be deleted, until the active purge off, you can add the ignore-reload option, which we commonly used in mp3,wma,wmv,gif and the like.
Examples:

Refresh_pattern-i \.mp3$ 1440 50% 2880 ignore-reload
Refresh_pattern-i \.wmv$ 1440 50% 2880 ignore-reload
Refresh_pattern-i \.rm$ 1440 50% 2880 ignore-reload
Refresh_pattern-i \.swf$ 1440 50% 2880 ignore-reload
Refresh_pattern-i \.mpeg$ 1440 50% 2880 ignore-reload
Refresh_pattern-i \.wma$ 1440 50% 2880 ignore-reload

-------------------------------------------------------------------------------------------

First figure out 2 things:
1, last-modified: This is the last time the page was modified. Can be set in the head. Squid when the page is cached, Squid automatically sets the last-modified to the access time if the property is not set.

2, Expires: The time when the webpage expires. If the page expires, Squid will visit webserver to reread the page.

Then, for dynamic pages, add the following code (PHP) to the page:
Set the last modified time to the current time
Header ("last-modified:".) Gmdate ("D, D M Y h:i:s"). "GMT");
Set the expiration time to 1 days (in seconds)
Header ("Expires:". Gmdate ("D, D M Y h:i:s", time () + 3600 * 24). " GMT ");

And the above content should be placed at the front of the PHP file.

For some files, such as images or forums, where last-modified and expires cannot be set or not easy, we can set the Refresh_pattern in squid.conf to control the cache time:

The syntax of Refresh_pattern is

Refresh_pattern [-i] regexp min percent max [options]

RegExp is a file type.
MIN, Max's unit is minutes, percent is percentage.

The Refresh_pattern algorithm is as follows:
1) If (current time-last modified time) < min), the cache is fresh
2) Else if (current time-last modified time) < (min + (max-min) *percent), cache is fresh
3) Else cache is out of date
Cache expiration requires fresh content from the server behind.

Example: Set HTM file expiration time is 10 minutes
Refresh_pattern. htm 0 25% 8
As for the parameters, please study them yourself.

---------------------------------------------------------------------------------------------

The Refresh_pattern directive controls the disk cache indirectly. It helps squid decide whether a given request is a cache hit, or is treated as a cache loss. The loose setting increases your cache hit rate, but it also increases the chance that the user will receive an outdated response. On the other hand, conservative settings reduce the cache hit rate and the stale response.

The Refresh_pattern rule applies only to responses that do not have a definite expiration date. The original server can use the Expires header, or the Cache-control:max-age directive, to specify the expiration date.

You can place any number of refresh_pattern lines in the configuration file. Squid finds them sequentially in order to match regular expressions. When Squid finds a match, it uses the corresponding value to determine whether a cached response is alive or expired. The Refresh_pattern syntax is as follows:

Refresh_pattern [-i] regexp min percent max [options]
For example:

Refresh_pattern-i \.jpg$ 50% 4320 reload-into-ims

Refresh_pattern-i \.png$ 50% 4320 reload-into-ims

Refresh_pattern-i \.htm$ 0 20% 1440

Refresh_pattern-i \.html$ 0 20% 1440

Refresh_pattern-i. 5 25% 2880
The RegExp parameter is a casing-sensitive regular expression. You can use the-I option to make them case insensitive. Squid checks refresh_pattern rows sequentially; when one of the regular expressions matches a URI, it stops searching.

The min parameter is the number of minutes. It is the minimum time limit for outdated responses. If a response resides in the cache for no longer than the minimum limit, it does not expire. Similarly, the max parameter is the maximum time limit for surviving responses. If a response resides in the cache longer than the maximum limit, it must be refreshed.

The response between the minimum and maximum time limits will face the last modified coefficient (lm-factor) algorithm of squid. For such a response, Squid calculates the age of the response and the last modification factor, and then compares it as a percentage value. The response age is simply the amount of time that is generated from the original server, or after the last validation response. The source age is different between the last-modified and the date header. Lm-factor is the ratio of response age to source age.

Figure 7-2 demonstrates the lm-factor algorithm. Squid caches a target for 3 hours (based on the date and last-modified header). The value of Lm-factor is 50%, and the response is alive for the next 1.5 hours, after which the target expires and is treated as obsolete. If the user requests the cache target during survival, SQUID returns an unconfirmed cache hit. If a request occurs during an outdated period, squid forwards the acknowledgement request to the original server


Reference: http://stame.blog.51cto.com/92914/611434

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.