Revision [6581]

This is an old revision of WikkaOptimization made by IanAndolina on 2005-03-08 01:58:01.

 

How to optimize Wikka?


And if we serve Css and Javascript files with content-encoding = gzip?

To save bandwidth, we may use gzip content encoding with text files, like Css and Javascript. I exploited the file mime_types.txt distributed with Wikka but css files are served as application/x-ilinc-pointplus, 'coz css extension is registered with this content-type. I need advices.
elseif (preg_match('/\.css$/', $this->method))
{
    #header('Location: css/' . $this->method); We replace this with :
    $filename = "css/{$this->method}.gz";
    if (file_exists($filename))
    {
        $content_length = filesize($filename);
        $etag = md5($filename . filemtime($filename) . filesize($filename)); #If the file wasn't modified, we will get the same etag.
        $expiry = gmdate("D, j M Y G:i:s", time()+28512000); #expires after 11 months
        header("Etag: $etag");
        if (strstr($_SERVER['HTTP_IF_NONE_MATCH'], $etag))
        {
            header('HTTP/1.1 304 Not Modified');
            die();
        }
        header('Content-Encoding: gzip');
        header("Content-Length: $content_length");
        header("Expires: $expiry GMT");
        header("Cache-Control: public, must-revalidate");
        header("Content-Type: text/css");  #Very important, because php scripts will be served as text/html by default
        $data = implode('', file($filename));
        die ($data);
    }
    else
    {
        header('HTTP/1.1 404 Not Found');
        die();
    }
}



Wikka's ETag is meaningless

See the code below (found in ./wikka.php) :
$etag =  md5($content);
header('ETag: '. $etag);

$content is the content of the page, including header (action header.php) and footer (action footer.php). But you see that in footer.php, the phrase 'Generated in x,xxxx seconds' is very rarely the same. Thus, a wiki page loaded at time (t) and reloaded at time (t+1) will have two different values for the header ETag.

I think the header and the footer should be excluded when calculating ETag. Ie, implement the method Run like this :
            print($this->Header());
   $content = $this->Method($this->method);
   echo $content;
   $GLOBALS['ETag'] = md5($content);
   print ($this->Footer());
        }
    }
}

and send the ETag header like this :
header("ETag: {$GLOBALS['ETag']}");


Another simple way is to use md5 of the date of latest change of the page instead of the content.

$etag = md5("$page_tag : $date_last_change : $date_last_comment"); --DotMG


Question : How does a webserver handle the If-Match, If-None-Match and If-Range request lines? Because Wikka sets manually the header ETag, I think it has also to handle manually these type of request-line.


A Potential Solution for Wikka's Meaningless ETag - Flexible and fast cacheing!


OK, So based on DotMG's valid critique of the current meaningless ETag output, and wanting to speed up Wikka by only sending pages that have changed, here is some beta code to play with:

Add this to $wakka->Run
    // THE BIG EVIL NASTY ONE!
    function Run($tag, $method = "")
    {
        // do our stuff!
        if (!$this->method = trim($method)) $this->method = "show";
        if (!$this->tag = trim($tag)) $this->Redirect($this->Href("", $this->config["root_page"]));
        if ((!$this->GetUser() && isset($_COOKIE["wikka_user_name"])) && ($user = $this->LoadUser($_COOKIE["wikka_user_name"], $_COOKIE["wikka_pass"]))) $this->SetUser($user);
        $this->SetPage($this->LoadPage($tag, (isset($_REQUEST["time"]) ? $_REQUEST["time"] :'')));
        //This is the new cache mechnaism-------------------------------------------------------------
        $etag = md5($this->page["time"].$this->page["user"]);
        $expires = $this->config["cache_age"]; //number of seconds to stay in cache, 0 means check validity each time
        header("Content-Type: text/html; charset=utf-8");
        header("Cache-Control: cache, max-age=".$expires."");
        header('Expires: '.gmdate('D, d M Y H:i:s',time()+$expires).' GMT');
        header("Pragma: cache");
        header("Etag: $etag");
        if (strstr($_SERVER['HTTP_IF_NONE_MATCH'], $etag) && $this->method == "show" && !preg_match($this->config["no_cache"],$tag))
        {
            header('HTTP/1.1 304 Not Modified');
            ob_end_clean();
            die();
        }
        //Cache mechanism END-------------------------------------------------------------------------

Added to wikka.config.php so an admin can configure this:
"no_cache" => "/(RecentChanges|RecentlyCommented|RecentComments)/",
"cache_age" => "0",


As you see a page will only ever return a 304 not modified IF: the page date and user hasn't changed, it is using the show method AND it doesn't match a RegEx of pages that should always be served fresh.

cache_age enables setting the cache validity time in seconds. So 600 would allow the client to not have to revalidate its cache for 10 minutes. When set to 0, the browser must all send a conditional GET, and only if the server sends a 304 response will it show the cached content.

One needs to remove the junk at the end of the main wikka.php with the current broken headers and one should have a simple client-based cache mechanism which serves fresh content when needed. Tested on Opera V8.0build7483 and FireFox V1.01 — someone needs to test it for IE (where Angels fear to tread) — IE may do something wrong as it has substantial numbers of cacheing bugs… After testing it superficially, IE 6.0 seems to be working as the other browsers!

See it in action here:

http://nontroppo.dreamhosters.com/wikka/HomePage

Problem
The major problem is that if a page is commented on, the cache will not fetch a new page. As DotMG suggested above, one needs a $date_last_comment for a page, and this is then used when first computing the ETag. For that, the easiest way would be to make a table field in wikka_pages for each page, and when a comment is added, update that field with the date. That should cause the cache to always update on the latest page change or comment added to that page. One could do a database query using the comments table, but that is a little more overhead and thus will be slightly slower. I prefer using a new table field...


(Google:rfc2616 for Documentation about Etag ...)

3.11 Entity Tags
Entity tags are used for comparing two or more entities from the same
requested resource. HTTP/1.1 uses entity tags in the ETag (section
14.19), If-Match (Section 14.24), If-None-Match (Section 14.26), and
If-Range (Section 14.27) header fields. The definition of how they
are used and compared as cache validators is in Section 13.3.3. An
entity tag consists of an opaque quoted string, possibly prefixed by
a weakness indicator.

entity-tag = [ weak ] opaque-tag
weak = "W/"
opaque-tag = quoted-string

A "strong entity tag" MAY be shared by two entities of a resource
only if they are equivalent by octet equality.

A "weak entity tag," indicated by the "W/" prefix, MAY be shared by
two entities of a resource only if the entities are equivalent and
could be substituted for each other with no significant change in
semantics. A weak entity tag can only be used for weak comparison.

An entity tag MUST be unique across all versions of all entities
associated with a particular resource. A given entity tag value MAY
be used for entities obtained by requests on different URIs. The use
of the same entity tag value in conjunction with entities obtained by
requests on different URIs does not imply the equivalence of those
entities.

 

Category
CategoryDevelopment
There is one comment on this page. [Display comment]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki