Smarty Caching mit Xcache

Wer schon einmal mit der Template Engine “Smarty” gearbeitet hat, wird sich spätestens, wenn es um den Einsatz dieser Lösung in High Traffic Umgebungen geht, darüber ärgern dass Smarty den Cache der Templates auf der Festplatte realisiert. Aus diesem Grunde habe ich bereits vor 2 Jahren eine Lösung entworfen, welche sich nun nach langem “Test” als durchaus brauchbar erwiesen hat und ich sie somit hier mal vorstellen möchte.
Grundlage ist die Tatsache, dass sich Smarty recht einfach mittels Plugins erweitern lässt, und sich somit auch ein anderer Cache Handler unterjubeln lässt.
Ich benutze bevorzugt für “Single Server Lösungen” Xcache mit lokal aktiviertem variablen Cache

<?php
/**
 * Smarty Xcache Caching Handler
 *
 * @author Alexander Over <alexander.over@koeln.de>
 * @copyright 2008
 */
function xcache_cache_handler($action, &$smarty_obj, &$cache_content, $tpl_file = null, $cache_id = null, $compile_id = null)
{
  if (function_exists('xcache_isset'))
  {
    $cacheid = 'smarty.'.$tpl_file.'@'.$cache_id.'@'.$compile_id;
    switch ($action)
    {
      case 'read':
        if (xcache_isset($cacheid))
        {
          return xcache_get($cacheid);
        }
        else
        {
          return false;
        }
      case 'write':
        return xcache_set($cacheid, $cache_content, $smarty_obj->cache_lifetime);
      case 'clear':
        if (empty($cache_id) and empty($compile_id) and empty($tpl_file))
        {
      	 return xcache_clear_cache(1, 0);
        }
        else
        {
          if (xcache_isset($cacheid))
          {
            return xcache_unset($cacheid);
          }
          else
          {
            return false;
          }
        }
      default:
      	$smarty_obj->trigger_error('cache_handler: unknown action "'.$action.'"' );
        return false;
    }
  }
  else
  {
    $smarty_obj->trigger_error('cache_handler: Xcache not found!');
    return false;
  }
}

geladen wird das ganze am einfachsten folgendermaßen:

<?php
require_once 'Smarty.class.php';
require_once 'xcache_cache_handler.php';

$smarty = new Smarty();
if (function_exists('xcache_isset'))
{
  $smarty->cache_handler_func = 'xcache_cache_handler';
}

Donwload der Datei: xcache_cache_handler.php

del.icio.us:Smarty Caching mit Xcache digg:Smarty Caching mit Xcache spurl:Smarty Caching mit Xcache wists:Smarty Caching mit Xcache simpy:Smarty Caching mit Xcache newsvine:Smarty Caching mit Xcache blinklist:Smarty Caching mit Xcache furl:Smarty Caching mit Xcache reddit:Smarty Caching mit Xcache fark:Smarty Caching mit Xcache blogmarks:Smarty Caching mit Xcache Y!:Smarty Caching mit Xcache smarking:Smarty Caching mit Xcache magnolia:Smarty Caching mit Xcache segnalo:Smarty Caching mit Xcache

Kommentar schreiben

:-< (@) (D) (~) (ap) (I) (Z) (8) :-s (mp) (||) (a) :-) (K) :-O :> (w) (&) (u) (so) (f) :-[ (p) (N) :p (H) (tu) (x) (T) :@ (xx) (r) |-) (}) (G) (c) (%) :-* (o) (#) (?) <:o) (L) :-$ *-) (st) (Y) (S) ({) (yn) (*) (co) :-| (B) (M) (li) (h5) (sn) (bah) (um) :-# :'( (^) 8-) (ci) ;) 8o| (ip) (mo) +o( (E) (pl) 8-| (6) (au) :^) (brb) ^o) (pi)