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

Pear::Cache as cache_handler_func

 
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 -> Tips and Tricks
View previous topic :: View next topic  
Author Message
toma
Smarty Regular


Joined: 25 Apr 2003
Posts: 62

PostPosted: Sat Sep 30, 2006 1:34 am    Post subject: Pear::Cache as cache_handler_func Reply with quote

This cache handler uses the now defunct pear::Cache 1.5.4 written by guru Ulf and lead developer Christian Stocker.... The cache is quite fast and easy to implement.

I needed a different caching mechanism because a directory with 35k cached files becomes impossible to work with. A clear_cache(null, 'group'); locked the server for several minutes while 'smarty' toiled away at indexing the cache dir and deleting the matching files. Certainly this is a combination of file system and smarty's cache implementation, but any file system will have issues with that many files in the same dir.

My solution was to implement Pear's Cache with a few changes to the file.php container to put cache groups into subdirectories. A $smarty->clear_cache('template.ihtml', 'group'); will delete all subdirectories under 'group'. template.ihtml will be it's own directory followed by subdirs for each group tokenized by |

This script assumes your include_path is intelligently built. Below is my core.cache_handler.php script:

Code:

<?
/**
* Cache handler using Pear::Cache
* The benefits of this include quicker cache searching with directories.
* This can be expanded to use shm for specific pages but the ::Cache
*     author isn't optimistic about real speed improvements.
*
* @author Tom Anderson toma@etree.org
*/
require_once('Cache.php');
function smarty_cache_handler($action, &$t, &$content, $template = null, $cache_id = null, $compile_id = null, $expire = null) {

    // Create the file cache object
    $cache = new Cache('file',
        array(
            'cache_dir' => $t->cache_dir . $template . '/',
            'file_chmod' => 0777
        )
    );
    $cache->setCaching(true);
    $id = $cache->generateID("$cache_id|$compile_id");

    switch ($action) {
    case 'read':
        $content = $cache->get($id, $cache_id);
        return (bool)$content;
    case 'write':
        return (bool)$cache->save($id, $content, $expire, $cache_id);
    case 'clear':
        return (bool)$cache->flush($cache_id);
    default:
        $t->trigger_error( 'cache_handler: Unknown cache action '. $action  );
        return false;
    }
}

?>
[/php]

Changes to the file.php are addition of file_chmod to local properties and allowed options and a significant change to getFileName.
[php]
// mod stat for all file creation
var $file_chmod = 0777;

# Replacement for mkdir and clearstatcache:
       // Allow for | to tokenize subgroups
       $subgroups = explode('|', $group);
       $dir = $this->cache_dir;
       foreach ((array)$subgroups as $sub) {
               $dir .= "$sub/";
               if (!file_exists($dir)) {
                       mkdir($dir, $this->file_chmod);
                       $clear_stat = true;
               }
       }
       if ($clear_stat) clearstatcache();
Back to top
View user's profile Send private message Visit poster's website
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 -> Tips and Tricks All times are GMT
Page 1 of 1

 
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