Project

General

Profile

Docs ModCML » History » Revision 15

Revision 14 (Anonymous, 2008-04-16 09:22) → Revision 15/19 (stbuehler, 2009-02-17 13:23)

h1. Module mod_cml - [[TracNav(DocsToc)]] 

 <pre> 

 #!rst 

 ========================= 
 CML (Cache Meta Language) 
 ========================= 


 --------------- 
 Module: mod_cml 
 --------------- 

 h2. WARNING 

 * This module is deprecated in favor .. contents:: Table of [[Docs::ModMagnet|mod_magnet]] which provides similar functionality. 
 * you have to return 1 instead of CACHE_MISS and 0 instead of CACHE_HIT (#533) Contents 

 {{>toc}} 

 h2. Description 
 =========== 

 CML (Cache Meta Language) wants to solves several problems: 
 

  * dynamic content needs caching to perform 
 
  * checking if the content is dirty inside of the application is usually more expensive than sending out the cached data 
 
  * a dynamic page is usually fragmented and the fragments have different lifetimes 
 
  * the different fragments can be cached independently 

 h2. Note that mod_cml is deprecated from 1.4.12 onwards in favor of :trac:`Docs:ModMagnet mod_magnet` which provides similar functionality.  

 Cache Decision 
 -------------- 

 A simple example should show how to a content caching the very simple way in PHP.  

 jan.kneschke.de has a very simple design: 
 

  * the layout is taken from a template in templates/jk.tmpl 
 
  * the menu is generated from a menu.csv file 
 
  * the content is coming from files on the local directory named content-1, content-2 and so on 

 The page content is static as long non of the those tree items changes. A change in the layout  
 is affecting all pages, a change of menu.csv too, a change of content-x file only affects the 
 cached page itself. 

 If we model this in PHP we get: 

 <pre> 
 :: 

   <?php 

 

   ## ... fetch all content-* files into $content 
 
   $cachefile = "/cache/dir/to/cached-content"; 

 

   function is_cachable($content, $cachefile) { 
   
     if (!file_exists($cachefile)) { 
     
       return 0; 
   
     } else { 
     
       $cachemtime = filemtime($cachefile); 
   
     } 

   

     foreach($content as $k => $v) { 
     
       if (isset($v["file"]) &&  
          
           filemtime($v["file"]) > $cachemtime) { 
       
         return 0; 
       } 
     } 
   } 

   

  if (filemtime("/menu/menu.csv") > $cachemtime) { 
     
       return 0; 
   
     } 
   
     if (filemtime("/templates/jk.tmpl") > $cachemtime) { 
     
       return 0; 
     } 
   } 
 } 
  
 
  
   if (is_cachable(...), $cachefile) { 
   
     readfile($cachefile); 
     exit(); 
   exit(); 
 } else { 
   
     # generate content and write it to $cachefile 
 
   } 
 
   ?> 
 </pre> 

 Quite simple. No magic involved. If the one of the files is new than the cached 
 content, the content is dirty and has to be regenerated.  

 Now let take a look at the numbers: 
  

  * 150 req/s for a Cache-Hit 
  * 100 req/s for a Cache-Miss 

 As you can see the increase is not as good as it could be. The main reason as the overhead 
 of the PHP interpreter to start up (a byte-code cache has been used here). 

 Moving these decisions out of the PHP script into a server module will remove the need 
 to start PHP for a cache-hit.  

 To transform this example into a CML you need 'index.cml' in the list of indexfiles  
 and the following index.cml file: 

 <pre> 
 :: 

   output_contenttype = "text/html" 

 

   b = request["DOCUMENT_ROOT"] 
 
   cwd = request["CWD"] 

 

   output_include = { b .. "_cache.html" } 

 

   trigger_handler = "index.php" 

 

   if file_mtime(b .. "../lib/php/menu.csv") > file_mtime(cwd .. "_cache.html") or 
    
      file_mtime(b .. "templates/jk.tmpl")     > file_mtime(cwd .. "_cache.html") or 
    
      file_mtime(b .. "content.html")          > file_mtime(cwd .. "_cache.html") then 
    
      return CACHE_MISS 
 
   else  
     
      return CACHE_HIT 
 
   end 
 </pre> 

 Numbers again: 
 

  * 4900 req/s for Cache-Hit 
 
  *    100 req/s for Cache-Miss 

 h2. Content Assembling 
 ------------------ 

 Sometimes the different fragment are already generated externally. You have to cat them together: 

 <pre> 
 :: 

  <?php 
  
   readfile("head.html"); 
  
   readfile("menu.html"); 
  
   readfile("spacer.html"); 
  
   readfile("db-content.html"); 
  
   readfile("spacer2.html"); 
  
   readfile("news.html"); 
  
   readfile("footer.html"); 
 
   ?>  
 </pre> 

  

 We can do the same several times faster directly in the webserver.  

 Don't forget: Webserver are built to send out static content, that is what they can do best. 

 The index.cml for this looks like: 

 <pre> 
 :: 

   output_contenttype = "text/html" 

 
  
   cwd = request["CWD"] 

 
   
   output_include = { cwd .. "head.html",  
                     
                      cwd .. "menu.html", 
                    
                      cwd .. "spacer.html", 
                    
                      cwd .. "db-content.html", 
                    
                      cwd .. "spacer2.html", 
                    
                      cwd .. "news.html", 
                    
                      cwd .. "footer.html" } 

 
  
   return CACHE_HIT 
 </pre> 

 Now we get about 10000 req/s instead of 600 req/s. 

 h2. Power Magnet 
 ------------ 

 Next to all the features about Cache Decisions CML can do more. Starting 
 with lighttpd 1.4.9 a power-magnet was added which attracts each request 
 and allows you to manipulate the request for your needs.   

 We want to display a maintenance page by putting a file in a specified 
 place: 

 We enable the power magnet: 

    :: 

   cml.power-magnet    = "/home/www/power-magnet.cml" 

 and create /home/www/power-magnet.cml with: 

 <pre> 
 :: 

   dr = request["DOCUMENT_ROOT"] 

 

   if file_isreg(dr .. 'maintenance.html') then 
   
     output_include = { dr .. 'maintenance.html' } 
   
     return CACHE_HIT 
 
   end 

 

   return CACHE_MISS 
 </pre> 

   >>>> Please note: mod_cml is deprecated and in 1.4.12 you have to return 1 instead of CACHE_MISS and 0 instead of CACHE_HIT -- luehr@milon.de <<<< 

 For each requested file the /home/www/power-magnet.cml is executed which 
 checks if maintenance.html exists in the docroot and displays it 
 instead of handling the usual request.  

 Another example, create thumbnail for requested image and serve it instead 
 of sending the big image: 

 <pre> 
 :: 

   ## image-url is /album/baltic_winter_2005.jpg 
 
   ## no params -> 640x480 is served 
 
   ## /album/baltic_winter_2005.jpg/orig for full size 
 
   ## /album/baltic_winter_2005.jpg/thumb for thumbnail 

 

   dr = request["DOCUMENT_ROOT"] 
 
   sn = request["SCRIPT_NAME"] 

 

   ## to be continued :) ...  

  

   trigger_handler = '/gen_image.php' 
 
   return CACHE_MISS 


 Installation 
 </pre> ============ 

 h2. Installation 

 You need "lua":http://www.lua.org/ `lua <http://www.lua.org/>`_ and should install "libmemcache-1.3.x":http://people.freebsd.org/~seanc/libmemcache/ `libmemcache-1.3.x <http://people.freebsd.org/~seanc/libmemcache/>`_ and have to configure lighttpd with: 

    :: 

   ./configure ... --with-lua --with-memcache 

 To use the plugin you have to load it: :: 

    server.modules = ( ..., "mod_cml", ... ) 

 h2. Options 
 ======= 

 h3. cml.extension 

 :cml.extension:  
   the file extension that is bound to the cml-module 

 h3. cml.memcache-hosts 

 
 :cml.memcache-hosts: 
   hosts for the memcache.* functions 

 h3. cml.memcache-namespace 

 
 :cml.memcache-namespace: 
   (not used yet) 

 h3. cml.power-magnet 

 
 :cml.power-magnet: 
   a cml file that is executed for each request 

 h2. Language 
 ======== 

 The language used for CML is provided by "LUA":http://www.lua.org/ 

 `LUA <http://www.lua.org/>`_.  

 Additionally to the functions provided by lua mod_cml provides: 

 :: 

   tables: 
 * 

   request 
 ** 
     - REQUEST_URI 
 ** 
     - SCRIPT_NAME 
 ** 
     - SCRIPT_FILENAME 
 ** 
     - DOCUMENT_ROOT 
 ** 
     - PATH_INFO 
 ** 
     - CWD 
 ** 
     - BASEURI 
 * 

   get 
 ** 
     - parameters from the query-string 
 * functions 
 ** 

   functions: 
   string md5(string) 
 ** 
   number file_mtime(string) 
 ** 
   string memcache_get_string(string) 
 ** 
   number memcache_get_long(string) 
 ** 
   boolean memcache_exists(string)  

  


 What ever your script does, it has to return either CACHE_HIT or CACHE_MISS.  
 If an error occurs, check the error-log. The user will get a error 500. If you don't like  
 the standard error-page use @server.errorfile-prefix@. 
 ``server.errorfile-prefix``. 

 Please note: mod_cml is deprecated and in 1.4.12 you have to return 1 instead of CACHE_MISS and 0 instead of CACHE_HIT -- luehr@milon.de 


 </pre>