Home Projects jsCacher
"My New Year's resolution is 1600x1200. "
jsCacher - cache HTML content using Javascript.

Download the jsCacher
SMARTY plugin

ver. 1.0beta (2005-08-05)
  1. What is jsCacher ?

    jsCacher stands for "JavaScript Cache Manager of the HTML content".
    It will enable your PHP scripts to grab pieces of HTML code and cache them on the client side for future use.

    Wouldn't it be just great to have some parts of your website cached so that users don't need to download them countless times ? What about headers, footers, right and left columns - typical constant pieces of nowadays websites, do they need to be downloaded every time when user visits your site?

    Well, NO! The answer is just below :)

    jsCacher is the SMARTY plugin, easy to install and ready to use. It may be easily embeded inside a PHP script, though.


  2. How exactly does jsCacher work ?

    It's pretty straightforward:

    1. jsCacher takes a piece of HTML code that is embraced by the {jscacher}{/jscacher} tags.

    2. the piece of HTML is stored on the WWW server inside pregenerated Javascript file (ie: jsc_0000001.js.php)

    3. the piece of HTML code is replaced by the external Javascript code reference.
      HTTP headers make this external Javascript code remain in the client's cache for quite some time.

    4. Each piece of HTML code has it's unique identifier on which javascript filenames are based.
      The unique identifier is based on the content (crc32 or md5).
      Thus, every change to the piece of cached HTML code forces new cache file generation, keeping the website content seamlessly up-to-date for users.


  3. What are the benefits/drawbacks of using jsCacher ?

    • [+] You will save your server's bandwidth by significantly cutting the size of HTML that your server sends to users who visit your website more then once in a period of time.

    • [+] Users who use low-bandwidth connection will experience a major speed-up in page loading as they will not download cached parts of the HTML code.

    • [-] Keep in mind that most of search engines and web spiders are unable to download the cached parts of the HTML code as they igore content "injected" by the javascript. If search engines are your concern it's a good idea to enable jsCacher for, say, logged users only.



  4. Requirements

    All you need is:



  5. Installation

    • Download the jsCacher SMARTY plugin

    • unzip the block.jscacher.zip file, copy the block.jscacher.php file to the /smarty/plugins directory.

      -- OR --

      if you don't have access to the /smarty/plugins directory or you simply don't want to mess with it, use the following PHP code to explicitly enable the jsCacher plugin:
      <?
      include_once("block.jscacher.php");
      $smarty->register_block("jscacher","smarty_block_jscacher");
      ?>

    • create the /jscache directory inside the website's DOCUMENT_ROOT directory. Make sure that the WWW server's process has permission to create new files there.


  6. How to use it ?

    Once the jsCache SMARTY plugin is enabled you can use the {jscacher} {/jscacher} block function inside your SMARTY templates to cache the HTML content:
    my HEADER
    {jscacher}
    	my cacheable HTML content my cacheable HTML content
    {/jscacher}
    my FOOTER

    The final output produced by SMARTY would be:
    my HEADER
    <script src='/jscache/jsc_1826279287.js.php'></script>
    my FOOTER

    The /jscache directory on the WWW server will then contain the jsc_1826279287.js.php file with the following PHP code:
    <?
    header("Content-Type: application/x-javascript;");
    header("Cache-Control: max-age=604800");
    header("Expires: ".gmdate("D, d M Y H:i:s", time()+604800)." GMT");
    ?>
    //file generated by JS Cacher
    document.write("my cacheable HTML content my cacheable HTML content");


  7. Global configuration

    You may control the default behavior of the jsCacher system by setting one of the following PHP variables:
    <?
    //Enables/disables jsCacher
    $GLOBALS['JSCACHER_DISABLE']=false;
     
    //jsCacher default settings
    $GLOBALS['JSCACHER_DEFAULTS']['charsetConvert'] = "win2iso";
    $GLOBALS['JSCACHER_DEFAULTS']['charsetEncoding'] = "iso-8859-2";
    $GLOBALS['JSCACHER_DEFAULTS']['cacheMaxAgeTime'] = (3600*24*7);
    $GLOBALS['JSCACHER_DEFAULTS']['cacheBaseDir'] = "./jscache";
    $GLOBALS['JSCACHER_DEFAULTS']['cacheBaseUrl'] = "/jscache";
    ?>
    The default settings are initialized by default when the jsCacher PHP code is included (see the block.jscacher.php source for details).
    However, if you initialize any of them prior to the block.jscacher.php inclusion, your settings will be taken as defaults and will not be overwritten.


    • JSCACHER_DISABLE - enables/disables the jsCacher system.

    • charsetConvert - performs content charset conversion before HTML code is cached.

    • charsetEncoding - adds the "content-type charset" header to the JS cachefiles.

    • cacheMaxAgeTime - how long (in sec) the JS files are cached on the client side.

    • cacheBaseDir - base directory where cache files are stored.

    • cacheBaseUrl - base URL where cache files are accessible via HTTP/HTTPS.



  8. Advanced features

    You may use <# #> tags within the {jscacher} block to place a small content that changes but you want to cache the whole block anyways.

    Example:
    my HEADER
    {jscacher}
    	Hello World. There are <#loggedUsers#> logged in.
    {/jscacher}
    my FOOTER

    The loggedUsers is the name of a smarty variable.


  9. License and Terms of Use

    You are granted permission to freely use the code for noncommercial purposes only.

    If you like to use the code for commercial purposes to get the permission first.

    The code is provided "as is" without warranty of any kind.





© 2003-2012 by , powered by SICOMA CMS