There are a number of different caching options in Office SharePoint Server 2007. A number of them are aimed at improving throughput in the rendering pipeline—from the time a request is received at the server until the time a response begins to stream back to the client computer. Although this is an important aspect of your overall site performance, this section focuses on caching as it relates to the following:
The BLOB cache is a mechanism that is only available with Office SharePoint Server 2007 publishing features. This makes it an ideal candidate for corporate portal sites that are based on the Collaboration Portal site template and Internet-facing sites that are based on the Publishing Portal site template. The BLOB cache enables you to configure caching directives that are associated with items served from publishing site lists, for example, the Pages library and Site Collection Images. When the browser on the client computer encounters a caching directive, it detects that the item it is retrieving can be saved locally and does not need to be requested again until the caching directive expires. In a geographically distributed environment, this is critically important because it reduces the number of items requested and sent over the network.
When the BLOB cache in Office SharePoint Server 2007 is turned on, a couple of different things happen. First, each time a cacheable item is requested, Office SharePoint Server 2007 searches the hard disk drive of the Web server that received the request to see if a copy exists locally. If it does, the file is streamed directly from the local disk to the user. If it isn't on the local disk yet, a copy of the item is made from the SQL database where it is stored, and then the item is sent to the user making the request. From that point forward, all requests for the item can be served directly from the Web server until the item's cacheability value indicates that it has expired. That results in better performance in the server farm by reducing contention on the database server.
The other thing it does is append a cacheability header to the item when the item is sent to the client. This header instructs the browser how long the item should be cached. For example, if a picture had a cacheability value of three days, the browser uses the copy of the image it has in its local cache if the picture is requested again within the next three days; it does not request it from the server again.
When testing your site to see what items are cached and how items are being cached, you can use Fiddler (http://www.fiddlertool.com). The following screenshot shows a Fiddler capture on a simple SharePoint site that is used for publishing. The site was created by using the default Collaboration Portal site template. Some additional text content was added to the page, and several images were added to the master page.
There are several important pieces of information contained in the Fiddler application.
-
The # column on the left indicates that there were a total of 44 HTTP requests made from the browser to the server to render the page.
-
The Result column shows the HTTP result code that was returned from the request for the item; a 200 result means the item was successfully retrieved.
-
The URL column indicates what specific item was being requested.
-
The Body column indicates the size of each item.
-
The Caching column shows the caching directive that was associated with each item. The data in the Caching column shows that several items have a caching directive associated with them; that is, they have a max-age attribute that is greater than 0. Caching directives are expressed in seconds. This mean that for the page illustrated several items are configured to be cached for 365 days (60 seconds in a minute, 60 minutes in an hour, 24 hours in a day = 60x60x24 = 86,400x365 = 31,536,000).
Notice that the items with that cache directive all reside in the _layouts directory. The reason they have that cache setting is because of the way the _layouts/images virtual directory is configured in IIS. When you create a new Web application, Office SharePoint Server 2007 automatically creates several virtual directories that map to folders on the Web server physical disks. When it creates the _layouts/images virtual directory, it adds a caching directive that applies to the entire directory. The following screenshot shows the configuration for the directory in the IIS Manager snap-in.
Because those items all have a non-zero caching directive associated with them, the next time the page is requested, the browser uses the copy of the item from its local browser cache instead of requesting it from the server again. The following screenshot shows a snapshot of Fiddler when the page is requested a second time.
As the Fiddler data shows, the number of items requested has reduced significantly—from 44 to 11. An important point to note is that the number of requests made may vary depending on how the page is requested. If you use the Refresh button in the browser, all of the items will likely be requested again, whether a local cached version of the item exists or not. Conversely, if the page is requested by navigating to it by using a link or shortcut, only the uncached items are requested.
The other thing shown in the Fiddler data is that the browser is making a request to the server for the other images on the master page that it already has in its local cache; the 304 response code indicates this. It means that the browser has made a conditional request for an item. The 304 response means that the version on the server has not been modified from the version on the client, so it doesn't need to be downloaded again. Even though the file is not downloaded across the network, it still has generated a round trip to the server to determine that the local copy is current. In a geographically dispersed environment, server round trips are costly, so the goal is to reduce them as much as possible. If a non-zero caching directive is added to each of the remaining items (other than the page, which is always returned by the server), then this goal can be achieved. The BLOB cache feature is what adds this caching directive.
You configure the BLOB cache by using the Web.config file for the Web application in which the cache will be used. Open the Web.config file in a text editor such as Notepad and search for the BlobCache entry. By default it will be:
<BlobCache location="C:\blobcache" path="\.(gif|jpg|png|css|js)$ " maxSize="10" enabled="false"/>
The attributes used in the BlobCache element have the following meanings:
-
location
Refers to the location on the Web server hard disk drive where cached items will be stored.
-
path A regex expression of the types of files that should be cached.
-
maxSize
The size, in GB, that the cache is allowed to use.
-
enabled
Set to true to enable the BLOB cache.
The following additional attribute—not included by default—is necessary to set a caching expiration value on individual items:
By setting the max-age attribute to a non-zero value, cacheable items have a cache expiration value associated with them so that a browser no longer needs to download the item, or even verify if it has the most current version. For example, assume you wanted to enable caching and allocate up to 100 MB on the Web server to store items. They should expire once a day, and in addition to the predefined types that are cached, .htc files should be cached as well. To support those requirements, specify the following BlobCache attributes:
<BlobCache location="C:\blobcache" path="\.(gif|jpg|png|css|js|htc)$ " maxSize="100" max-age="86400" enabled="true"/>
Note that this change to the Web.config file needs to be made on every Web server in the farm. In most cases, the BLOB cache will begin working immediately, but it's safest to use the iisreset command when you implement the changes. The following screenshot shows Fiddler data for the same page request shown previously, only with the BLOB cache enabled as described.
Notice that all of the items in the /SiteCollectionImages library now have an HTTP status code of 200, indicating that they have been successfully downloaded. In addition, they all now have a Caching directive associated with them that specifies they will not expire for one day (8,400 seconds). If the page is requested again, Fiddler shows that none of the images are requested again; the total number of requests to service that page has thus gone from 44 down to three, and two of those are just from the NTLM authentication negotiation that occurs between the Web server and client application. The following figure shows Fiddler data when the page is requested again.
Additionally, consider the following when working with the BLOB cache:
-
It requires some additional effort to configure because you have to update the Web.config file on each Web server. However, the benefits are worth the effort.
-
Survey your site contents and determine if there are any other file types that should also be served from cache. A good example is .htc files. Because .htc files are used in most publishing sites, you should add that file type to the list of file types being cached.
-
The BLOB cache only works on items stored in SharePoint libraries; it cannot be used to cache content from other sources.
-
Some lists don't work by default for anonymous users. If there are anonymous users accessing the site, permissions need to be manually configured for the following lists in order to have items within them cached:
-
Master Page Gallery
-
Style Library
There are two other configuration options to be aware of when working with the BLOB cache. The first has to do with clearing out the BLOB cache. If the cache needs to be cleared out for particular site, navigate to that site collection and click the Site Actions…Site Settings…Modify All Site Settings menu. In the list of Site Collection Administration tasks. Click the Site collection object cache link. In the Disk Based Cache Reset section, check the box that says Force this server to reset its disk-based cache and then click the OK button.
If you are considering using Web gardens in a SharePoint farm, you also need to be aware that doing so will result in the BLOB cache to operating in a manner that seems inconsistent. When more than one Web garden is configured in a server farm, it poses a problem because only one of them can acquire the lock necessary to manage the BLOB cache. As a result, it may appear that the BLOB cache is only working intermittently. Successful use of the BLOB cache instead becomes dependent upon which thread services a request.
Unlike previous versions of SharePoint Products and Technologies, IIS compression is now automatically turned on when you install SharePoint Products and Technologies. After a site has been hit by a few users, you can verify that compression is working by viewing the %WINDIR%\IIS Temporary Compressed Files directory on a Web server. It should contain multiple files, which indicates that static files have been requested and IIS has compressed a copy of them and stored them on the local drive. When that file is requested again, whether it's the same user or not, the compressed version of the file is served directly from this folder. Dynamic files can be compressed as well, but they are always compressed on the fly; copies are not kept on the local Web server.
Compression can result in significant bandwidth savings. For example, the core.js file is included on every SharePoint page. When it's uncompressed, it is 257 KB; after compression, the file is only 54 KB without performing additional tuning to IIS compression. Core.js should be cached after the user first visits the site, but this example illustrates how significantly compression can help in low-bandwidth scenarios.
When SharePoint Products and Technologies are installed, setup configures IIS to compress the static file types .htm, .html and .txt; it compresses the dynamic file types .asp and .exe. By surveying the file types that are widely used in your implementation, it may be advantageous to compress additional file types. For example, it probably makes sense to also compress the static file types .css and .js; it may also make sense to compress the dynamic file types .axd and .aspx.
To add a static or dynamic file type to the list of types that will be compressed, use the adsutil.vbs file that is in the %SystemDrive%\Inetpub\AdminScripts folder by default on each Web server. The following example shows Microsoft Windows Server 2003, including css and js files in the list of compressed static file types:
-
CSCRIPT.EXE ADSUTIL.VBS SET W3Svc/Filters/Compression/DEFLATE/HcFileExtensions "htm" "html" "txt" "css" "js"
-
CSCRIPT.EXE ADSUTIL.VBS SET W3Svc/Filters/Compression/GZIP/HcFileExtensions "htm" "html" "txt" "css" "js"
The following example shows the inclusion of .aspx and .axd files in the list of dynamic file types:
-
CSCRIPT.EXE ADSUTIL.VBS SET W3Svc/Filters/Compression/DEFLATE/HcScriptFileExtensions "asp" "exe" "axd" "aspx"
-
CSCRIPT.EXE ADSUTIL.VBS SET W3Svc/Filters/Compression/GZIP/HcScriptFileExtensions "asp" "exe" "axd" "aspx"
When changing the file types that are compressed, ensure that you only include files that are well-suited to being compressed. For example, .jpg files are not a good candidate for compression because the file format is inherently compressed already. Also, 2007 Microsoft Office system file types such as docx, xlsx and pptx are not a good choice for compression because the files are not served directly from the server; instead, they are routed through the different ISAPI filters that are used to manage the rich integrated end user experience for Microsoft Office content. In addition, in the 2007 Microsoft Office system, these file types are inherently compressed.
In addition to controlling the types of files that are compressed, it's also possible to control the level of compression used on dynamic file types. The amount of compression used is based on a scale of 0 to 10. The higher the compression level, the smaller files will be. However, the tradeoff is that higher compression levels also consume more CPU, so you will use greater CPU utilization on compression to create smaller files. When IIS compression is enabled, it configures it to use a compression level of 0. Historically, the ideal compression level with SharePoint Products and Technologies has been 9. To change the compression level, use the adsutil.vbs script file previously described in this article. The following example shows changing the compression level to 9.
For more information, see Using HTTP Compression (IIS 6.0) (http://go.microsoft.com/fwlink/?LinkId=108705&clcid=0x409).