TURBOCHARGING
    YOUR
  EXTENSION
  © Klas Berlič & BzZzZ 2010
HOW IT ALL CAME ABOUT
    // How I got involved with cache //
Quite an amazing story
          of a willpower
and turning a failure into a sucess
        with contributions to common g...
In 2007 approached by a client to build travel portal for
a client.

No clearly defined business strategy, exact site scop...
NOT AT ALL AN EASY TASK!
 especially if you look unimportant in the eyes of big players
2
Took 2 years
Only after then




development finally began.
Vacations, Hotels, Travel and Airtickets
                              booking management system                          ...
And then..
Development subcontractor left with
only 10% done




                                           actual   plan




..when ...
If your partners fail, you have to do the job yourself.




                                  // Problem was I knew php on...
3
                                    // compare that to Joomla 1.6. :))




Learned php & OOP & brought it to
ß level in ...
End result
Probably one of the most complex and technologically advanced
sites ever built on Joomla (it is simple..but in a Google wa...
Combined power of Joomla php framework

and ExtJS javascript framework used on a presentation layer
Results from own database merged with results from multiple
different on-demand XML data sources




                     ...
Initially it took on average 45-65 seconds to perform a search &
render results.



Site badly needed a speed boost.
Speed?
Speed?




         // No, nothing illegal :)
Found an an answer in




  CACHING
JOOMLA! CACHE
    // explained //
How simple solutions
can speed up things
     10 times
      or more
CACHE
Temporary store an unit of information
to be reused later
// a kind of digital recycling //
Multiple layers of web caching:

Network (content delivery networks)
Ÿ
Server (proxy servers, load distribution..)
Ÿ
Daemo...
Web application caching

creating temporary static copies of otherwise dynamic
pages (or smaller data units).
Typical web site is displaying the same content over and
over again

No cache:
everything has to be generated for each and...
JOOMLA! CACHING TYPOLOGY
       & when to use each of them
template



component



modules



functions



smaller
units

            page cache   component    module   function/ca...
PAGE CACHE
Takes snapshots of entire pages including everything -
component, modules, plugins and a template.

The widest and the lea...
MODULE & COMPONENT
    VIEW CACHE
A group as they they both create a static copy of complete
output of a component or a module

J1.5.
Records based on the c...
Important difference                  // J1.5., changed in J1.6! //


module caching can only be set on/off for all instan...
Most widespread cache type, sometimes equaled with J
caching in general.


       Performs well in the speed terms


     ...
Side effect
Cached copy includes only modules or components own
output -> any external file that is called by using
method...
FUNCTION / CALLBACK
       CACHE
The first of flexible caching types that enable us to
differentiate between various parts of the extension and
cache only ...
Caches results of function calls

Records (cacheID) based on the arguments passed to the
function
Often useful to cache model methods and keep views
uncached.

Example use
Model performing expensive queries or similar op...
OUTPUT CACHE
Caches output of some part od the script

Records based on id passed
Basically output buffering with caching
RAW CACHE
Caches any data units

Records based on id passed
Fully controlled by the coder – what, when to store, how
to classify stored units (cache id).

Highly useful when we are d...
Example use
High number of possible combinations of relatively small
number of units – e.g. products in online store. No p...
Also useful to pass large amounts of data between
different parts of application (e.g. steps in click flow)

Used in Jooml...
CASE STUDY
 travel booking engine
PROBLEM

It took on average 45 to 65 seconds to display a
search result
DIAGNOSIS

Largest portion of time spent on remote XML requests,
waiting for and receiving replies.

Other two performance...
Descriptions & images 1




                                                                                           Des...
Descriptions & images 1




                                                                                           Des...
SOLUTION
Cache data on multiple levels:

Ÿ pages that were fully rendered - a lot of users
Cache
 search with default para...
ALSO..

Add more indexes to Mysql tables

Query and render images and descriptions per page:
primary source queried once, ...
RESULT

Site runs 10 times faster
Average first page loads in 5-6 seconds
Subpages (paginated) load in 0.5 - 1 seconds

Th...
JOOMLA! 1.6. CACHE CHANGES
          // firsthand //
A very long story made short:
      "It finally works"
FRAMEWORK LEVEL

Almost everyhing was redone under the hood.

Cache library has been completely refactored.
Most important framework changes

Cache handlers are now known as cache controllers (page,
view, output, callback)

Parent...
New cachelite and wincache storage handlers (drivers).
File cache handler heavily optimised
All other handlers fixed with ...
Changed component view caching (replaced unsafe
cacheID creation method)

Module cache completely reconceptualized
CMS FRAMEWORK LEVEL CHANGES

Caching implemented in all components and modules that
can potentially gain from using it

Ca...
USER LEVEL FUNCTIONAL CHANGES

Cache administration (Clean cache, Purge cache) now
works with all storage handlers

New st...
API changes
// and other changes important for developers //
COMPONENT VIEW CACHE

Takes an array of url parameters and their types to create
Cacheid.

A replacement for a previous un...
Com_contact controler usage example:
$safeurlparams = array('id'=>'INT', 'catid'=>'INT', 'limit'=>'INT',
'limitstart'=>'IN...
MODULE CACHE

5 different modes of operation, 3 of them are to be set
from module XML file, while 2 are meant to be used f...
MODES TO BE SET IN XML

Static - one cache file for all pages with the same module
parameters

Oldstatic - 1.5. definition...
In addition to cache field that was required in 1.5 there is
now new hidden field called cachemode that sets any of
the ab...
MODES TO BE CALLED FROM INSIDE THE MODULE:

Safeuri - id is created from URL params array, as in
component view cache

Id ...
An example that uses safeuri mode and replaces uncached
$list = modRelatedItemsHelper::getList($params) :


$cacheparams->...
RAW CACHE

Raw cache get and store are easily accesed by passing ''
(empty string) as cache controller to JFactory::getCac...
AND HERE THE STORY ENDS
       // ok..a little more //
If you still remember how it all started..

I hope you like where it ended.
               // tried to do my best //
Turbocharging your extension // Joomla //
Turbocharging your extension // Joomla //
Turbocharging your extension // Joomla //
Upcoming SlideShare
Loading in...5
×

Turbocharging your extension // Joomla //

2,851

Published on

Published in: Technology
1 Comment
0 Likes
Statistics
Notes
  • Joomla caching typology: page 30

    Joomla! 1.6. cache changes - page 59
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

No Downloads
Views
Total Views
2,851
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
39
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

Turbocharging your extension // Joomla //

  1. 1. TURBOCHARGING YOUR EXTENSION © Klas Berlič & BzZzZ 2010
  2. 2. HOW IT ALL CAME ABOUT // How I got involved with cache //
  3. 3. Quite an amazing story of a willpower and turning a failure into a sucess with contributions to common good as a final consequence
  4. 4. In 2007 approached by a client to build travel portal for a client. No clearly defined business strategy, exact site scope was unknown Joint venture starting with visibility study
  5. 5. NOT AT ALL AN EASY TASK! especially if you look unimportant in the eyes of big players
  6. 6. 2 Took 2 years
  7. 7. Only after then development finally began.
  8. 8. Vacations, Hotels, Travel and Airtickets booking management system html integration Already made airticket booking engine Website search box XML search CORE XML results Routing searches suppliers German Traffics Combining results Rendering results Tibet temporary booking Routing booking requests XML over XML static XML Booking request form XML Other SLO suppliers Traffics Tibet ADMINISTRATION Cosmo Palma Own inventory management Manual booking Bookings management
  9. 9. And then..
  10. 10. Development subcontractor left with only 10% done actual plan ..when we should have been at beta stage
  11. 11. If your partners fail, you have to do the job yourself. // Problem was I knew php only briefly //
  12. 12. 3 // compare that to Joomla 1.6. :)) Learned php & OOP & brought it to ß level in 3 months.
  13. 13. End result
  14. 14. Probably one of the most complex and technologically advanced sites ever built on Joomla (it is simple..but in a Google way)
  15. 15. Combined power of Joomla php framework and ExtJS javascript framework used on a presentation layer
  16. 16. Results from own database merged with results from multiple different on-demand XML data sources Descriptions & images 1 Descriptions & images 2 Website search box XML search CORE suppliers German Traffics XML results Routing searches Tibet Combining results XML Rendering results temporary booking Routing booking requests over XML Own database Booking requests
  17. 17. Initially it took on average 45-65 seconds to perform a search & render results. Site badly needed a speed boost.
  18. 18. Speed?
  19. 19. Speed? // No, nothing illegal :)
  20. 20. Found an an answer in CACHING
  21. 21. JOOMLA! CACHE // explained //
  22. 22. How simple solutions can speed up things 10 times or more
  23. 23. CACHE Temporary store an unit of information to be reused later // a kind of digital recycling //
  24. 24. Multiple layers of web caching: Network (content delivery networks) Ÿ Server (proxy servers, load distribution..) Ÿ Daemons and interpreters – Apache mod_cache, Php – Ÿ opcode caching, Mysql query cache Ÿ application (Joomla cache) Web Customer (browser cache) - used by Joomla Page cache Ÿ plugin
  25. 25. Web application caching creating temporary static copies of otherwise dynamic pages (or smaller data units).
  26. 26. Typical web site is displaying the same content over and over again No cache: everything has to be generated for each and every page view Caching: Ÿ some or all of the information your code generates store in a cache object Ÿ it when next user requests the same page or serve particular piece of information.
  27. 27. JOOMLA! CACHING TYPOLOGY & when to use each of them
  28. 28. template component modules functions smaller units page cache component module function/callback output cache view cache cache cache raw cache
  29. 29. PAGE CACHE
  30. 30. Takes snapshots of entire pages including everything - component, modules, plugins and a template. The widest and the least flexible approach of all caching options. // enabled by core system plugin -> site administrators choice //
  31. 31. MODULE & COMPONENT VIEW CACHE
  32. 32. A group as they they both create a static copy of complete output of a component or a module J1.5. Records based on the calling URL // UNSAFE, DOS // J1.6. Changes // more later //
  33. 33. Important difference // J1.5., changed in J1.6! // module caching can only be set on/off for all instances, can’t be controlled from within the module
  34. 34. Most widespread cache type, sometimes equaled with J caching in general. Performs well in the speed terms Disables any user<->extension<->framework interaction until a cached copy has expired Not suitable for any components or modules that react to user actions or render frequently changing content.
  35. 35. Side effect Cached copy includes only modules or components own output -> any external file that is called by using methods like $document->addStyleSheet() won't get included Workarounds performed to come around this limitations Catch22 Workarounds require computing time -> diminish the effect
  36. 36. FUNCTION / CALLBACK CACHE
  37. 37. The first of flexible caching types that enable us to differentiate between various parts of the extension and cache only those parts that are cacheable, while keeping dynamic parts uncached.
  38. 38. Caches results of function calls Records (cacheID) based on the arguments passed to the function
  39. 39. Often useful to cache model methods and keep views uncached. Example use Model performing expensive queries or similar operations is run only once to create a larger pool of data which is then further manipulated inside the view (sorting, pagination, calculations etc.)
  40. 40. OUTPUT CACHE
  41. 41. Caches output of some part od the script Records based on id passed
  42. 42. Basically output buffering with caching
  43. 43. RAW CACHE
  44. 44. Caches any data units Records based on id passed
  45. 45. Fully controlled by the coder – what, when to store, how to classify stored units (cache id). Highly useful when we are dealing with finite number of reusable data units
  46. 46. Example use High number of possible combinations of relatively small number of units – e.g. products in online store. No point to cache multiple parameter searches, cache each product separately. Other examples Expensive queries, remote XML, thumbnails, reusable texts or any reusable data set.
  47. 47. Also useful to pass large amounts of data between different parts of application (e.g. steps in click flow) Used in Joomla core: list of components, list of modules, menu tree, available languages, user groups, html renderers etc.
  48. 48. CASE STUDY travel booking engine
  49. 49. PROBLEM It took on average 45 to 65 seconds to display a search result
  50. 50. DIAGNOSIS Largest portion of time spent on remote XML requests, waiting for and receiving replies. Other two performance hogs were resizing images and internal database queries. 2nd and other pages of paginated results displayed instantly
  51. 51. Descriptions & images 1 Descriptions & images 2 Each result from badly designed primary XML paired with description and image that come from separate XML sources (one by one) Website search box XML search CORE suppliers German Traffics XML results Routing searches Tibet Combining results XML Rendering results temporary booking Routing booking requests over XML Own database Booking requests
  52. 52. Descriptions & images 1 Descriptions & images 2 SLOW Website search box XML search CORE suppliers German Traffics XML results Routing searches Tibet Combining results XML Rendering results temporary booking Routing booking requests over XML Own database Booking requests
  53. 53. SOLUTION Cache data on multiple levels: Ÿ pages that were fully rendered - a lot of users Cache search with default parameters or click lastminute links, most just check one or two pages of paginated results Ÿ XML queries (with longer lifetime). Pass data Cache from short results to detail pages Long-term caching of images and descriptions -> most Ÿ important one
  54. 54. ALSO.. Add more indexes to Mysql tables Query and render images and descriptions per page: primary source queried once, secondary separately for each subpage (pagination).
  55. 55. RESULT Site runs 10 times faster Average first page loads in 5-6 seconds Subpages (paginated) load in 0.5 - 1 seconds Those timings further drop dramaticaly (50% or more) on peak hours, when most popular searches are returned from the cache. The bigger number of users on a site, the faster it goes. // within the hosting limits //
  56. 56. JOOMLA! 1.6. CACHE CHANGES // firsthand //
  57. 57. A very long story made short: "It finally works"
  58. 58. FRAMEWORK LEVEL Almost everyhing was redone under the hood. Cache library has been completely refactored.
  59. 59. Most important framework changes Cache handlers are now known as cache controllers (page, view, output, callback) Parent JCacheController was added - among other things it controls raw get and store calls.
  60. 60. New cachelite and wincache storage handlers (drivers). File cache handler heavily optimised All other handlers fixed with missing functions (gc, clean) added, their code cleaned and tested and should now be working properly. Semaphore locking was added for reliability and improved performance
  61. 61. Changed component view caching (replaced unsafe cacheID creation method) Module cache completely reconceptualized
  62. 62. CMS FRAMEWORK LEVEL CHANGES Caching implemented in all components and modules that can potentially gain from using it Caching added to some most expensive and frequent framework calls: JComponentHelper::_load(), JModuleHelper::_load(), JMenuSite::load(), JDocumentHTML::getBuffer()..
  63. 63. USER LEVEL FUNCTIONAL CHANGES Cache administration (Clean cache, Purge cache) now works with all storage handlers New standalone garbage collect script to be run from a crontab added libraries/joomla/utilities/garbagecron.php
  64. 64. API changes // and other changes important for developers //
  65. 65. COMPONENT VIEW CACHE Takes an array of url parameters and their types to create Cacheid. A replacement for a previous unsafe way which took the whole URL and so opened the doors for DOS attacks via random parameters/values added to request // Old cacheid created from URL retained for backwards compatibility if there are no $safeurlparams (to be removed in 1.7) //
  66. 66. Com_contact controler usage example: $safeurlparams = array('id'=>'INT', 'catid'=>'INT', 'limit'=>'INT', 'limitstart'=>'INT', 'filter_order'=>'CMD', 'filter_order_Dir'=>'CMD’, 'lang'=>'CMD'); parent::display($cachable,$safeurlparams);
  67. 67. MODULE CACHE 5 different modes of operation, 3 of them are to be set from module XML file, while 2 are meant to be used from within the module itself. Default is backwards compatible oldstatic mode that requires no changes to a module.
  68. 68. MODES TO BE SET IN XML Static - one cache file for all pages with the same module parameters Oldstatic - 1.5. definition of module caching, one cache file for all pages with the same module id and user aid. Default for backwards compatibility Itemid - changes on itemid change
  69. 69. In addition to cache field that was required in 1.5 there is now new hidden field called cachemode that sets any of the above modes. <field name="cachemode" type="hidden" default="static"> <option value="static"></option>
  70. 70. MODES TO BE CALLED FROM INSIDE THE MODULE: Safeuri - id is created from URL params array, as in component view cache Id - module sets own cache id's To use this modes rename 'cache' field in xml to 'owncache' field and call JModuleHelper::ModuleCache from within // actually a shortcut to cache callback to avoid code duplication in every module //
  71. 71. An example that uses safeuri mode and replaces uncached $list = modRelatedItemsHelper::getList($params) : $cacheparams->modeparams = array('id'=>'int','Itemid'=>'int'); $cacheparams->methodparams = $params; $cacheparams->method = 'getList'; $cacheparams->class = 'modRelatedItemsHelper'; $cacheparams->cachemode = 'safeuri'; $cacheparams = new stdClass; $list = JModuleHelper::ModuleCache ($module, $params, $cacheparams);
  72. 72. RAW CACHE Raw cache get and store are easily accesed by passing '' (empty string) as cache controller to JFactory::getCache Data is auto serialized / deserialized Locking & unlocking are performed automaticaly
  73. 73. AND HERE THE STORY ENDS // ok..a little more //
  74. 74. If you still remember how it all started.. I hope you like where it ended. // tried to do my best //
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×