At this topic we will talk about how you can speed up your osCommerce store with existing solutions and you will learn a new upcoming caching system which was developing by osCommercex.
You will read:
- What is memcached?
- How memcached osCommerce works?
- Existing osCommerce Performans Module.
- Existing osCommerce Addons.
What is memcached?
Nowadays we are working on a new caching system for osCommerce. We named this module memcached osCommerce. Main problem about an e-commerce store is speed. One of my customer has a big book store. And he want to use good resolution images with over 2000 products. At this point I search some caching system. Then I realize a caching system that’s name is memcached.
memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.
Danga Interactive developed memcached to enhance the speed of LiveJournal.com, a site which was already doing 20 million+ dynamic page views per day for 1 million users with a bunch of webservers and a bunch of database servers. memcached dropped the database load to almost nothing, yielding faster page load times for users, better resource utilization, and faster access to the databases on a memcache miss.
First of all you need to install memcached to your server. To learn memcached you can visit:
- memcached Documentation (Google Code Pages)
- memcached Resources / Articles / Books
- http://download.tangent.org/talks/Memcached%20Study.pdf
- http://www.bytebot.net/blog/archives/2008/04/14/memcached-and-mysql-tutorial
- Memcached Functions for MySQL
- http://tangent.org/552/libmemcached.html
- Installing memcache on Windows for PHP
- Using Memcache with MySQL and PHP
- PHP memcahed
- Caching & Performance: Lessons from Facebook
- Using MySQL with memcached
- memcache.php stats like apc.php
How memcached osCommerce works?
After installing memcached you need to start it. Acording to your server operating system you can learn how you can start memcached server from documantations. For instance I am using Vista and I started it under services menu.
Let’s look how we will store pages. Our osCommerce page is consisting 5 part.
- Header (header.php)
- Left column (column_left.php)
- Middle part of site
- Right column (column_right.php)
- Footer (footer.php)
In order to use memcache first of all we will start memcache server osCommerce and will use PHP ob_start function. Also you can check Caching PHP Pages with Output Buffering. Now I am testing memcached osCommerce, so I will give brief information how memcached osCommerce works.
First we will build memcache connection.
1 2 | $memcache = new Memcache; $memcache->connect('localhost', 11211) or die ("Could not connect"); |
After success connection, we will look memcache server for existing key which was prepared for our store. I used osc_key for collecting results.
1 | $get_result = $memcache->get('osc_key'); |
Now we will check if memcache has osc_key.
1 2 3 4 5 6 7 8 9 10 11 | $start_memcache =false; if(!$memcache->get('osc_key')){ // if memcache has not osc_key, define start_memcache true. $osc_mem->author = "www.oscommercex.com"; $start_memcache = true; }else{ // if memcache has osc_key, get osc_key values to $osc_mem variable $osc_mem = $memcache->get('osc_key'); //echo "cache working: ".$osc_mem->author; //print_r($oscommerce_memcache); } |
We will put below code to application_top.php file. Find below line,
1 | define('PAGE_PARSE_START_TIME', microtime()); |
After this line add below code,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $memcache = new Memcache; $memcache->connect('localhost', 11211) or die ("Could not connect"); $get_result = $memcache->get('osc_key'); $start_memcache =false; if(!$memcache->get('osc_key')){ // if memcache has not osc_key, define start_memcache true. $osc_mem->author = "www.oscommercex.com"; $start_memcache = true; }else{ // if memcache has osc_key, get osc_key values to $osc_mem variable $osc_mem = $memcache->get('osc_key'); //echo "cache working: ".$osc_mem->author; //print_r($oscommerce_memcache); } |
In application_top.php I am generating cached files name. For example,
1 2 3 4 5 6 7 8 9 10 | // we will decide the cached page name according to request $request_array = array_merge( $_GET, $_POST, $HTTP_GET_VARS ? $HTTP_GET_VARS : array( ), $HTTP_POST_VARS ? $HTTP_POST_VARS : array( ) ); print_r($request); echo $request['cPath']; $left_memcache_name = (isset($request_array['cPath'])?$request_array['cPath']:'').(isset($language)?$language:''); $bottom_memcache_name = (isset($language)?$language:''); //$cache_name_builder = (isset($request['cPath'])?$request['cPath']:'').(isset($request['products_id'])?$request['products_id']:'').(isset($language)?$language:''); $leftblokname = isset($left_memcache_name)?'leftblock'.$left_memcache_name:'leftblock'; $bottomname = isset($bottom_memcache_name)?'bottom'.$bottom_memcache_name:'bottom'; |
As you see we generate file names client requests and site languages. Therefore we can cache all possible pages.
For instance I will cache column_leftp.php to memcached server.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | <?php /* $Id: column_left.php 1739 2007-12-20 00:52:16Z hpdl $ osCommerce, Open Source E-Commerce Solutions http://www.oscommerce.com Copyright (c) 2003 osCommerce Released under the GNU General Public License */ if(empty($osc_mem->$leftblokname)){ $start_memcache = true; } // if start_memcache is false we will use cached files from memcached otherwise we will cache the file if(!$start_memcache){ echo "cache"; // for testing //if you decide to use gzip you can uncomment below line //echo gzuncompress($osc_mem->$leftblokname); echo $osc_mem->$leftblokname; }else{ ob_start(); if ((USE_CACHE == 'true') && empty($SID)) { echo tep_cache_categories_box(); } else { include(DIR_WS_BOXES . 'categories.php'); } if ((USE_CACHE == 'true') && empty($SID)) { echo tep_cache_manufacturers_box(); } else { include(DIR_WS_BOXES . 'manufacturers.php'); } require(DIR_WS_BOXES . 'whats_new.php'); require(DIR_WS_BOXES . 'search.php'); require(DIR_WS_BOXES . 'information.php'); //if you want you can gzip webpage you can use below line, wonderful! //$osc_mem->$leftblokname = gzcompress(ob_get_contents(),9); $osc_mem->$leftblokname = ob_get_contents(); ob_end_flush(); ob_end_clean(); } ?> |
As you see you can do same procedure for other part of site. And the last step you have to do is about application_bottom.php.
Open application_bottom.php,
And last ?> tag add below lines.
1 2 3 | if($start_memcache){ $memcache->set('osc_key', $osc_mem, false, 3600) or die ("Failed to save data at the server"); } |
Above codes we are caching osc_key with $osc_mem values for an hour.
This is the main working procedure memcached osCommerce. Now I am try to finish full version of memcached osCommerce. And there are some other way to use memcache, for instance you can cache sql queries. There is a function packet for MySQL for managing sql caching. You can get more information from Using MySQL with memcached.
If you have huge traffic website you can use mail quene over memcached server. There are lots of way to use memcached. The best part of memcached osCommerce, you can apply same way to your existing store no matter huge modified.
You can join our memcached osCommerce works, as a programmer or tester.
Existing osCommerce Performans Module
Magneticone osCommerce performance module
I needed osCommerce Performans module for my customer. I searched and found Magneticone osCommerce Performance module.
It has same caching structure system as I prepared for memcached osCommerce, but it saves pages to your server directory. memcached osCommerce saves caches to your RAM. So memcached osCommerce is better than Magneticone osCommerce Performance module. Magneticone osCommerce performance module is good for standart osCommerce store, but if your store has huge modification like my customer you maybe have some problems. For instance my customer’s product description area has unique information according to the customer. Therefore Magneticone osCommerce performance module caches only same product description for every customers. Maybe Magneticone can produce a solution to unique cache for the customers.
Existing osCommerce Addons
The aim of this module is to utilize the browser’s cache, thus reduces the page load times a visitor experiences as he browses through your store. Bandwidth usage is dramatically reduced. Cached pages are loaded from the user’s browser hence the load times are subject to the local PC performance rather than the server response.
To maintain cart and personalized content integrity the module maintains a cached history per visitor with the session facility. The module also includes spider cacheing, cache reports and image cache.
There are 5 different processes handled by the Cache HTML
- Spiders cache
- Full page cache for normal visitors
- Parametric cache for normal visitors
- Flush cached pages for normal visitors
- Full image cache common for both spiders and visitors
The image cache uses the OTF module from here ‘On the Fly’ Auto Thumbnailer using GD Library
You can also visit these sites:
- Database Caching Class
- osCommerce A better tep_reset_cache_block
- osCommerce Faster Page Loads, Less DB queries
- osCommerce Page Cache v1.0 – MS2
- osC Advanced Cache Class
- Massively Speed up Site and Reduce Server Load, Remove this function
- Make your osCommerce Site up to 48% faster!
- Speed up load times by compressing pages (with or without gzip on)
- Optimize categories box
- osCommerce Email queue
- osCommerce cDynamic Meta Tags v1.0
I am strongly waiting your comments about how we can improve osCommerce.
You can also visit osCommerce Speed up (accelerator) service at http://www.holbi.co.uk/modules-for-oscommerce/product/item/oscommerce-speed-up-accelerator-service/
Reply
I read your blog for quite a long time and should tell you that your articles always prove to be of a high value and quality for readers.
Reply
admin
Reply:
April 15th, 2009 at 19:20
Thank you for your thoughtfulness.
Reply