Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Caching on multiple servers
Goto page Previous  1, 2, 3
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
alexb_uk
Smarty n00b


Joined: 01 Apr 2008
Posts: 4

PostPosted: Tue Apr 01, 2008 9:26 am    Post subject: Reply with quote

I also need to implement this as we currently have two apache server load balanced. This may increase in the future so would rather not try to use an NFS solution for the reasons mentioned above.

We currently use the PHP ADOdb library for database managed sessions and although the load of the db server is fairly high it does work well.

With that in mind I was thinking of extending smarty to use database managed page caches.

Do people think this is a good plan / bad plan or anything I have over looked?

Cheers

Alex


*EDIT

Ok I see there is actually some example code on how to implement this Embarassed Will have a play with that!


**EDIT

Tried it and it works, however doesnt the example doesnt support cache groups, will endeavour to implement this. If I succeed will let you guys know.
Back to top
View user's profile Send private message
alexb_uk
Smarty n00b


Joined: 01 Apr 2008
Posts: 4

PostPosted: Tue Apr 01, 2008 2:04 pm    Post subject: Reply with quote

I have updated the example in the manual, appears to work ok:

Code:

<?php
/**************************************************
example usage:

include('Smarty.class.php');
include('mysql_cache_handler.php');

$smarty = new Smarty;
$smarty->cache_handler_func = 'mysql_cache_handler';

$smarty->display('index.tpl');


mysql database is expected in this format:

create database SMARTY_CACHE;

CREATE TABLE IF NOT EXISTS `cache_pages` (
  `CacheID` varchar(32) NOT NULL,
  `CacheContents` mediumtext NOT NULL,
  `TemplateFile` varchar(255) NOT NULL,
  `GroupCache` varchar(255) NOT NULL,
  PRIMARY KEY  (`CacheID`)
);


**************************************************/

function mysql_cache_handler($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null, $exp_time=null)
{
    // set db host, user and pass here
    $db_host = 'localhost';
    $db_user = 'root';
    $db_pass = 'xxxxx';
    $db_name = 'SMARTY_CACHE';
    $use_gzip = true;         //Disable / Enable GZip compression
    $gzip_level   = 1;         //0 for no compression up to 9 for maximum compression

    // create unique cache id
    $CacheID = md5($tpl_file.$cache_id.$compile_id);

    if(! $link = mysql_pconnect($db_host, $db_user, $db_pass)) {
        $smarty_obj->_trigger_error_msg('cache_handler: could not connect to database');
        return false;
    }
    mysql_select_db($db_name);

    switch ($action) {
        case 'read':
            // read cache from database
            $results = mysql_query("select CacheContents from CACHE_PAGES where CacheID='$CacheID'");
            if(!$results) {
                $smarty_obj->_trigger_error_msg('cache_handler: query failed.');
            }
            $row = mysql_fetch_array($results,MYSQL_ASSOC);

            if($use_gzip && function_exists('gzuncompress')) {
                $cache_content = gzinflate($row['CacheContents']);
            } else {
                $cache_content = $row['CacheContents'];
            }
            $return = $results;
            break;
        case 'write':
            // save cache to database

            if($use_gzip && function_exists("gzcompress")) {
                // compress the contents for storage efficiency
                $contents = gzdeflate($cache_content,$gzip_level);
            } else {
                $contents = $cache_content;
            }
           
            $results = mysql_query("replace into CACHE_PAGES values(
                            '$CacheID',
                            '".addslashes($contents)."',
                            '$tpl_file',
                            '$cache_id')
                        ");
            if(!$results) {
                $smarty_obj->_trigger_error_msg('cache_handler: query failed.');
            }
            $return = $results;
            break;
        case 'clear':
            // clear cache info
            if(empty($cache_id) && empty($compile_id) && empty($tpl_file)) {
                // clear them all
                $results = mysql_query('delete from CACHE_PAGES');
            } else {
                if(strpos($cache_id, '|') !== false) {
                   if(!empty($tpl_file)) {
                      $results = mysql_query("delete from CACHE_PAGES where TemplateFile='" .$tpl_file ."' AND GroupCache LIKE '$cache_id%'");
                   }else {
                      $results = mysql_query("delete from CACHE_PAGES where GroupCache LIKE '$cache_id%'");
                   }
                }else {
                   $results = mysql_query("delete from CACHE_PAGES where CacheID='$CacheID'");
                }
            }
            if(!$results) {
                $smarty_obj->_trigger_error_msg('cache_handler: query failed.');
            }
            $return = $results;
            break;
        default:
            // error, unknown action
            $smarty_obj->_trigger_error_msg("cache_handler: unknown action \"$action\"");
            $return = false;
            break;
    }
    mysql_close($link);
    return $return;

}
?>
Back to top
View user's profile Send private message
t3hAndy
Smarty Rookie


Joined: 10 Sep 2008
Posts: 12

PostPosted: Tue Sep 30, 2008 9:30 am    Post subject: Reply with quote

Possible http://blog.rarecore.eu/nfs-the-unperformed-network-file-system.html helps you... Wink
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Goto page Previous  1, 2, 3
Page 3 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP