Return to TBS Demonstration Home          
TBS file caching of merged sub-templates

This little project started out as a way to provide random-order advertising thumbnails with as little PHP or DB processing as possible for moderate or higher traffic sites. I supposed that a short interval cache might help performance. First one thinks a cached db query, then next, when you consider a cached script you realize the possibility of using that cached script by any other pages on the site - nice!

So what's different about the elemets being merged? Well, they are populate them with the current database info and then use the merged & cached file on many other pages on the website - without those other pages needing to do any PHP, any db queries, or TBS block processing to show those merged subtemplates!
In the "Our Contact" illustration below there are three sections that are loaded as subtemplates - straight HTML files that are slurped directly into the main template by TBS. They are the pink "Alphabetically" table, the pink "By Category" section, and the right-side "Favorites" column of random graphics.

Let's walk through the steps...
  • the un-merged subtemplate is an HTML file that contains the TBS Block definitions as a TBS template
  • the script populates the pre-merge template file using the current database info via MergeBlock()
  • the resulting Source, now updated with current db info, is cached to a new & separate file - this is the merged version of the template and is now pure HTML with the database info included as of the time of the merging
  • with TBS cache plugin it is easy for each of the subtemplates to have different lifetimes before rebuilding - check the bottom of this page to see the chache status of each file. You might have noticed that in the "Favorites" column, the order of graphics keeps changing. That's because the query used to build that post-merge file does an MySQL "ORDER BY RAND()" to get a random sequence of the adverts for display. This file is cached more frequently than the other sections which change less frequently.
  • finally, the merged template file is available to load in any other page on the website
This approach reasonably approximates doing full PHP/TBS merging of a sub-script for every page view of the pages that use/display the column of "Favotites" advertising.

For this demonstration, the TbsSQL debugging Console has been turned ON so that we can observe when the queries take place that indicate a cache is being refreshed.

When you click on a category or letter you will see in the popup CONSOLE window that only needed queries are being made. The console window will show queries as they are needed to refresh the merge-template cache files as they expire.
Our Contacts
Alphabetically: All A's B's C's G's H's I's J's M's P's S's W's
By category:
    • Aliens
    • Politicians
    • Geeks
Contact NameEmail AddressPhone
Alexander, Dr Richard  Alien   (978) 745-1200
Andre, DDS, Gregory  Alien   (978) 658-8400
Archambault, Joe and Kathy  Politician   Bev 978-927-0848
Archambault, Joe Jr  Alien P71SOHC@aol.com 978-979-7762
Backyard Boats,  Alien   410-867-4800
Boulter Plywood,  Alien   617-666-1340
Byrne, Frank & Gertrude  Alien    
Cimon, Ronald  Alien   978-809-7846
Clark, Henry  Alien teecharley@onetel.com  
Cobb, Duane  Politician duane@marathonmarine.com 305-393-2178
Conley, Mike & Debra  Geek blkrkbch@comcast.net 781-581-1066
Gossard, Craig  Geek cb4x@comcast.net 305-546-5677
Graczyk, Elaine M/G & Thaddeus Stanley  Politician rebatelady@comcast.net 941-378-1614
Harry, Seth C  Alien scharry@aol.com 410-913-0856
Henry, Jessica A.  Politician j.henry@cbpr.org 781-581-5087
Henry, Michael & Sheila  Alien metsci@mikeandsheilahenry.com 508-309-6158
Henry, Thomas J. & Lucille M.  Politician tom@tomhenry.us 978-745-6559
Henry, Brian  Alien brian.henry@wanadoo.fr 003-323-889-0341
Henry, Walter, Jr  Alien    
Inman, Sue  Alien susaninman@comcast.net 978-561-1001
Jalbert, Plumbing & Heating  Alien   978-531-8006
McGroary, Josephine Henry  Alien    
McGroary, James  Alien    
Millette, Lorainne  Alien   978-692-8178
PENTAX WG-1 Camera, Support  Alien   800 234 0276
Purleys, Marina and Repair  Alien   978-948-2812
Shahin, Shahin  Alien   617-257-2147
Shanning, Brian and Susan  Alien    
Slacter, Jane Munkenbeck  Alien jslacter@gmail.com 301-530-3564
SPOT, Findmespot  Alien   866-651-7768
State Farm, Insurance  Politician   800-526-8210
Suburban Florist, Bethesda  Alien   (800)860-3297
Weiner, Mr & Mrs Kenny & Jessica  Alien   781-581-5087
Weiner, Larry & Susan  Alien   781-322-7969
Wolfe, Jennifer & Stefan  Geek jennifer.h.wolfe@gmail.com 202-543-2556
Wolfe, John & Susanna  Alien   978-283-4479
 
Favorites
Let's look at how we take advantage of merged templates in other pages...

Simply by adding [onshow;file='./merged_adverts_table.html';htmlconv=no;] to any other TBS HTML template page on the website! TBS reads that now HTML-only file with no merging or variable substitution required.

When any user selects the main page on the site, a short refresh time for the Favorites display of advert logo's is merged to produce another randomized sequence for the adverts.

Yes it is that simple to make 'reasonably' dynamic, even randon content, available throughout the site by creating it on only one page and displaying where needed.

Yet none of the other users on any other subpages are waiting for that subtemplate to be built, or even aiting for PHP to check the filesystem for the age of the file which they would be obliged to do if all pages were loading the Favorites display as a sub-script instead of a sub-template.

You can view the individual cached versions of the merged subtemplate files:
merged favorites (adverts) table
merged categories table
merged letters table

View the actual PHP/TBS source code that is running this page -- and view the HTML HTML Template code for this page.

Status of cached subtemplates files:
Letters merged table: retain cache for 3 minutes. Currently -22291377.9 minutes remaining to expiration.
Categs merged table: retain cache for 8 minutes. Currently -22291372.9 minutes remaining to expiration.
Adverts merged table: retain cache for 1.5 minutes. Currently -22291379.4 minutes remaining to expiration.
TbsSQL v.3.2 for MySQL-ext_tomh running under TinyButStrong version 3.8.0-RC1 for PHP 5
Installed plug-ins: 2
- plug-in [clsTbsPlugInHtml] version 1.0.7
- plug-in [clsTbsCacheSytem] version 1.0.6