|
|
|
"My New Year's resolution is 1600x1200.
"
|
|
|
jsCacher - cache HTML content using Javascript.
-
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.
-
How exactly does jsCacher work ?
It's pretty straightforward:
- jsCacher takes a piece of HTML code that is embraced by the {jscacher}{/jscacher} tags.
- the piece of HTML is stored on the WWW server inside pregenerated Javascript file (ie: jsc_0000001.js.php)
- 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.
- 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.
-
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.
-
Requirements
All you need is:
-
Installation
-
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");
-
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.
-
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.
-
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.
|