LiteSpeed Technologies
Download Download     Blog Blog     Wiki Wiki     Forum Forum     Store     Contact Contact    

Go Back   LiteSpeed Support Forums > LiteSpeed Web Server > LiteSpeed Cache > General Guideline to use LiteSpeed Cache

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 02-17-2011, 12:12 PM
webizen webizen is offline
LiteSpeed Staff
 
Join Date: Oct 2010
Posts: 2,337
Default General Guideline to use LiteSpeed Cache

LiteSpeed has made a built-in cache functionality (an advanced feature for Enterprise 2+CPU license) available since version 4.0 (March, 2009), and keeps improving throughout the later 4.0.x releases and 4.1RC releases. As of 4.1.1 (May, 2011), private cache support is added.

LiteSpeed cache has similar features as Apache mod_cache but implemented in a more efficient way, and works like Varnish. It is an output cache for dynamic contents, so the usage does not limit to PHP pages. Unlike Varnish, it is built into LiteSpeed web server, eliminating one layer of reverse proxy. Hence more efficient for static contents. The uniqueness of LiteSpeed cache is that it uses rewrite rules (either in configuration files or .htaccess) to control its behavior for maximum flexibility.

A general way to enable LiteSpeed Cache through rewrite rules is to designate a signature token (such as cookie) in a page (or any page) in most cases to tell LSWS that cache can be enabled and TTL(Time To Live) of the cache freshness however acceptable. Generating cookie usually requires application code modification. Refer to http://www.litespeedtech.com/support...ead.php?t=4679 for example. It is worth noting that the cache function SHOULD ONLY be used for the pages that are supposed to be cached or cache friendly. Abuse use of this feature would result in performance degradation and/or other unexpectancies.

Since 4.1.1 (May, 2011), private cache support is added. Private cache is for caching resources for individual specifically instead of public shared. The purposes are:
  1. maintain a persistent cache for applications that do not have a shared cache.
  2. maintain a private persistent cache for specific groups of documents that are not to be shared among other applications.

Here are the steps to enable LSWS built-in cache:

1. Enable cache function at Server level in LSWS
  1. LSWS Web Admin Console->Server->Cache
  2. Cache Storage Settings->Storage Path: set to a fast local disk, e.g. /tmp/diskcache (set it to a ramdisk such as /dev/shm/lsdiskcache if enough extra memory can be spared to speed up further)
  3. Manually create the directory from shell, and give write permission for litespeed/lshttpd processes (assume running as "nobody" here):
    Code:
    # mkdir /tmp/diskcache
	
    # chown nobody:nobody /tmp/diskcache
	      
    # chmod 700 /tmp/diskcache
2. Set Default cache policy at LSWS Server level
LSWS Web Admin Console->Virtual Hosts->mytest->Cache -> Cache Policy
Quote:
Enable Cache:No
Cache Request with Query String:Yes
Cache Request with Cookie:No
Cache Response with Cookie:No
Ignore Request Cache-Control:No

Ignore Response Cache-Control:No
Enable Private Cache: Yes*
Private Cache Expire Time (seconds): 120
Note:
  1. Even though Cache is disabled globally at Server Level, it will be enabled through rewrite rules. That's the way supposed to be.
  2. Cache Request with Cookie can be set to Yes as long as cookie does NOT affect the generated page. Use it carefully.
  3. *Set "Enable Private Cache" to Yes to turn on private cache. Private Cache is pertinent to each individual user/browser. This is to speed up performance even for logged-in user or cache for individual instead of public.
3. For imported Apache vhost:
Cache can be turned on/off with "CacheEnable" , "CacheDisable" directives in Apache config file. Nothing needs to be done for the cache policy. It will assume the same default settings at LSWS Server level. Rewrite rules can be placed in httpd.conf or in-directory .htaccess file as shown in step 5 below.
Note: Apache mod_cache directives CacheIgnoreCacheControl, CacheMaxExpire also can be used in the Apache config file as well as .htaccess to fine tune cache policy.
4. For native LSWS vhost:
Setup Cache Policy (LSWS Web Admin Console->Virtual Hosts->mytest->Cache -> Cache Policy)
Quote:
Enable Cache:No
Cache Request with Query String:yes
Cache Request with Cookie:Not Set
Cache Response with Cookie:Not Set
Ignore Request Cache-Control:No
t Set
Ignore Response Cache-Control:Not Set
Enable Private Cache: Not Set
Private Cache Expire Time (seconds): Not Set
Note: those with not set, settings at LSWS Server Level will be assumed.
5. Create/Add rewrite rules for caching %
Code:
RewriteEngine On
## cache should be available for HEAD or GET requests

RewriteCond %{REQUEST_METHOD} ^HEAD|GET$
## select which pages to cache
RewriteCond %{HTTP_COOKIE} page_contain_cachetoken=yes
# with other condition

RewriteCond %{QUERY_STRING} !s=[a-fA-F0-9]{32}

# excluding certain URLs
RewriteCond %{REQUEST_URI} !/(login|register|usercp|private|profile|cron|image) \.php$

# cache for 2 mins for php pages only
RewriteRule /(.*\.php)?$ – [L,E=Cache-Control:max-age=120]

# for those not met above condition, enable private cache.
RewriteCond %{REQUEST_METHOD} ^HEAD|GET$
## select which pages to serve from private cache
RewriteCond %{HTTP_COOKIE} !page_contain_cachetoken=yes
# with other condition

RewriteCond %{QUERY_STRING} !s=[a-fA-F0-9]{32}

# excluding certain URLs
RewriteCond %{REQUEST_URI} !/(login|register|usercp|private|profile|cron|image) \.php$

# private cache for however long set in cache policy for php pages only
RewriteRule /(.*\.php)?$ – [L,E=Cache-Control:private]
These rules can be placed in either of the 3 locations below:
  1. .htaccess (last line needs to changed as follows.)
    Code:
    RewriteRule (.*\.php)?$ – [L,E=Cache-Control:max-age=120]
    RewriteRule (.*\.php)?$ – [L,E=Cache-Control:private]
  2. VirutalHost secion in Apache config file
  3. LiteSpeed native configuration: Web Admin Console -> Configurations -> Virtual Hosts -> Rewrite
6. Restart LSWS to make changes effective.
Admin Console -> Actions -> Graceful Restart or issue '/path/to/lsws/bin/lswsctrl restart' from command line.
7. Verify if pages are served from LSWS cache
Since 4.0.19 release, LiteSpeed outputs a response header “X-LiteSpeed-Cache: hit” if a request is served from cache.
Since 4.1.1 release, LiteSpeed outputs a response header “X-LiteSpeed-Cache: hit,private” if a request is served from private cache.
8. Cache clean up (Optional)
Setup a cronjob to purge outdated cache file on disk and add the following entry in /etc/crontab
Code:
*/10 * * * * root find /tmp/diskcache -type f -mmin +8 -delete 2>/dev/null
Note: The cronjob deletes cached files that are more than 8 minutes old every 10 minutes. Since the cache TTL is set at 120 seconds (2 minutes), it's safe to delete those old files that are way passed TTL.


Rewrite Rule Examples:
  1. Cache everything for 2 mins (Simple!)
    Code:
    RewriteEngine On
    RewriteRule cacheable/(.*\.php)?$ - [L,E=cache-control:max-age=120]
    Note: Only cache *.php files in "cacheable" directory. As pointed out previously, it is a good practice to cache only the files that are supposed to be cached.

  2. Only cache the page with certain signature
    Code:
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/index.php
    RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$  [NC]
    RewriteRule (.*) index.php 
    
    # this part is for public cache.
    RewriteCond %{REQUEST_METHOD} ^HEAD|GET$
    RewriteCond %{HTTP_COOKIE} !loginuser
    RewriteCond %{ORG_REQ_URI} !^/index.php$
    RewriteCond %{ORG_REQ_URI} !^/administrator/
    RewriteCond %{ORG_REQ_URI} (\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$  [NC]
    RewriteRule .* - [E=Cache-Control:max-age=300,L]
    Note: This example demonstrate how LSWS cache rewrite rules fit in application's rewrite rules. The first part is from application (such as joomla). Essentially everything goes through index.php (joomla framework) to process.

    Second part of rulesets indicates that LSWS only cache
    1. HEAD and GET type request AND
    2. The request doesn't contain loginuser in cookie AND
    3. %{ORG_REQ_URI} i.e. original request (Note: %{ORG_REQ_URI} is LiteSpeed specific variable. In this case, it keeps the value of %{REQUEST_URI} prior to rewrite to index.php in the first part) does not start with /index.php and not start with /administrator/ AND
    4. %{ORG_REQ_URI} ends with .php or .html or .htm or etc.

      The TTL of cache is 300 seconds (5 minutes).
  3. This example shows private cache GET|HEAD for logged-in user
    Code:
    ...
    
    # this part is for private cache, note that HTTP_COOKIE is for loginuser
    RewriteCond %{REQUEST_METHOD} ^HEAD|GET$
    RewriteCond %{HTTP_COOKIE} loginuser
    RewriteCond %{ORG_REQ_URI} !^/index.php$
    # admin area cache be cached for admin user. hence remove this rule
    # RewriteCond %{ORG_REQ_URI} !^/administrator/
    RewriteCond %{ORG_REQ_URI} (\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$  [NC]
    RewriteRule .* - [E=Cache-Control:private,L]
    Note: The rulesets indicate that LSWS only private cache
    1. HEAD and GET type request AND
    2. The request DOES contain loginuser in cookie AND
    3. %{ORG_REQ_URI} i.e. original request (Note: %{ORG_REQ_URI} is LiteSpeed specific variable. In this case, it keeps the value of %{REQUEST_URI} prior to rewrite to index.php in the first part) does not start with /index.php and not start with /administrator/ AND
    4. %{ORG_REQ_URI} ends with .php or .html or .htm or etc.

      The TTL of cache is however long set in cache policy for private cache.

Last edited by webizen; 05-23-2011 at 04:59 PM.. Reason: private cache support added.
Reply With Quote
 

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 01:21 PM.



- Archive - Top
© Copyright 2003-2011 LiteSpeed Technologies, Inc. All rights reserved. Privacy Policy.