osC Advanced Cache Class v1.1 for osCommerce-MS2

by Chemo

What does this contribution do?

This contribution enables the osCommerce developer or advanced webmaster the ability to cache just about anything. Whether you need to cache a large array, template, or even [executable] PHP code this class can handle it.

Originally designed and coded to be a scalable alternative to filesystem cache for my Ultimate SEO URLs contribution it can be used for just about any cache need. This class can be the base for a database driven template system or global data cache on a per language basis. It is extremely flexible, robust, and fast.

As a forward, if you have no idea whether your store would benefit from cached data then this contribution is NOT for you. It is geared toward contribution developers and coders.

Examples of usage

First, the class must be included and an instance initiated in application_top.php. It must be initialized underneath of the language code since this contribution relies heavily on $languages_id to separate cache files for different languages. Initiate it like this:

    $language = $lng->language['directory'];
    
$languages_id = $lng->language['id'];
  }

// include the language translations
  
require(DIR_WS_LANGUAGES . $language . '.php');

# include the cache class
    
include('includes/classes/cache.class.php');
    
$cache = new cache($languages_id);
# include the Ultimate SEO URLs cache file
    
include('includes/seo_cache.php');
    
# Get the cache - no parameters will get all GLOBAL cache entries for this language
    
$cache->get_cache('GLOBAL');

Now that the cache class is initialized you can start caching just about anything. There are several options but which ones you choose will depend on what type of data you are caching and how you will want it returned.

SIMPLE DATA - no PHP parsing or array

<?php
// some simple data
    
$text = 'This is some simple data. Nothing special...just text that does not need to be parsed.';

// save the data to cache
// [ cache name (str), cache data (mixed), method (UPPER string), compressed (0/1), global (0/1), expires (interval/unit) ]
    
$cache->save_cache('text', $text, 'RETURN', 1, 0, $expires = '6/hours');
    
// This example sets the cache name to 'text' with $text value, needing a simplereturn, compress it, not global, and expires in 6 hours

Once the cache is saved you can pull it back out at your convenience like this:

// get the data back out of cache
// [ name (str), return method (str) ]
// will return false if cache not present OR is expired
$text2 = $cache->get_cache('text');

// work with the retuned data
if ($text2) echo $text2;
else echo
'$text2 is false';


/*#################################################################*\
                    with memory flag
\*#################################################################*/

// if you may need the data again on the page set the memory flag
$text2 = $cache->get_cache('text', true);

// work with the retuned data
if ($text2) echo $text2;
else echo
'$text2 is false';

//get the data back out of memory on another call / skip the database
$text3 = $cache->get_cache_memory('text');

// work with the retuned text3 data
if ($text3) echo $text3;
else echo
'$text3 is false';

?>

SAVING AND USING AN ARRAY

<?php
// a simple array
$array = array('one', 'two', 'three');

// save the array data to cache
// [ cache name (str), cache data (mixed), method (UPPER string), compressed (0/1), global (0/1), expires (interval/unit) ]
$cache->save_cache('array', $array, 'ARRAY', 1, 1, '1/h');

This example sets the cache name to 'array' with $array value, identifies it as an array, compress it, not global, and expires in 1 day.

Once the cache is saved you can pull it back out at your convenience like this:

// get the data back out of cache
// [ name (str), return method (str) ]
// will return false if cache not present OR is expired
$array2 = $cache->get_cache('array', 'ARRAY');

// work with the retuned data
if ($array2) print_r( $array2 );
else echo
'$array2 is false';
?>

SAVING AND USING PHP CODE - real world example

<?php
1. Original code
      
// set the application parameters
      
$configuration_query = tep_db_query('select configuration_key as cfgKey, configuration_value as cfgValue from ' . TABLE_CONFIGURATION);
          while (
$configuration = tep_db_fetch_array($configuration_query)) {
          
define($configuration['cfgKey'], $configuration['cfgValue']);
      }
      
2. Modification to also cache the data
      
// set the application parameters
      
$configuration_query = tep_db_query('select configuration_key as cfgKey, configuration_value as cfgValue from ' . TABLE_CONFIGURATION);
      while (
$configuration = tep_db_fetch_array($configuration_query)) {
          
define($configuration['cfgKey'], $configuration['cfgValue']);
          
$cache_output .= 'define(\'' . $configuration['cfgKey'] . '\', \'' . $configuration['cfgValue'] . '\');' . "\n";
      }
      
//save the cache data
      
$cache->save_cache('config', $cache_output, 'EVAL', 1, 1, '1/months');
      
This code above says to save the $cache_output as 'config', compress it, it IS global, and it expires in 1 month.
      
3. Further modification to first check for cached data before querying
      
// check to see if it's already cached
      
$cache->is_cached('config', $is_cached, $is_expired); // Notice that $is_cached and $is_expired is passed by reference!
      
      
if ( !is_cached || $is_expired ){ // must not be cached or is expired
      // set the application parameters
      
$configuration_query = tep_db_query('select configuration_key as cfgKey, configuration_value as cfgValue from ' . TABLE_CONFIGURATION);
      while (
$configuration = tep_db_fetch_array($configuration_query)) {
          
define($configuration['cfgKey'], $configuration['cfgValue']);
          
$cache_output .= 'define(\'' . $configuration['cfgKey'] . '\', \'' . $configuration['cfgValue'] . '\');' . "\n";
      }
      
//save the cache data
        
$cache->save_cache('config', $cache_output, 'EVAL', 1, 1, '1/months');
      } else {
// data is cached and is not expired
          
$cache->get_cache('config'); // get the cached data and eval it
      
}
      
In the real example above it would save a potentially large table scan on each page. Instead, it pulls all the configuration values in one very fast and efficient query then evaluates it on the fly.
?>

How do I install this contribution?

Upload 2 new files, execute install file, done. Install should take less than 30 seconds.

STEP 1 - Upload the included files

The files should be located in the upload directory of this contribution.

STEP 2 - Call install-cache.php in your browser

The install script is straight forward...either install or uninstall.

Notes

I have created many contributions that rely on flesystem cached data and this has led to exhausting support from the basic path misconfiguration errors. I was looking for an alternative that would be easy to use, scalable, robust, and of course fast. This is the answer for me...database cached files.

Over the years I have seen a great many contributions that needed some serious optimization. The queries weren't written efficiently or they used VERY large table scans to get data. This will give those contribution coders a viable alternative since this class is extremely versatile when one becomes familiar with the simple logic.

Support

osCommerce is a community driven organization and as such the support base will fall entirely on the forum members. This contribution is geared toward developers and not store owners that cut-n-paste contributions.

If you use this contribution and find it useful a small donation can be made via PayPal. This will enable me to offer one-on-one support and also fund releasing other contributions. In addition, if you make a donation it'll make you eligible for other performance optimization contributions that are not released!

After all the contributions I've coded and released I'm beginning to think nobody reads this far down...

Enter Donation amount: $

Credits

By me. Enjoy!




eCommerce-Directory.org

If you're interested in SEO for your store be sure to submit your site to my directory! It has a high PR and clean links






Blatant Footer Advertisement

If you need reliable, extremely fast, and expert webhosting for your store I am opening my server to accounts.  Each account gets unlimited everything and trust my server is setup to run osCommerce at top speed.

osCommerce Hosting and Technical Support - by Chemo