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

New Plugin: MogileFS Cache Handler (Smarty 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 -> Plugins
View previous topic :: View next topic  
Author Message
buegle
Smarty n00b


Joined: 01 Aug 2011
Posts: 1

PostPosted: Mon Aug 01, 2011 4:04 pm    Post subject: New Plugin: MogileFS Cache Handler (Smarty 3) Reply with quote

Hi,

I've hacked up a quick mofileFS cache handler for Smarty 3 (below). Posting here in case there is anything I can do to improve it or if anyone is looking at a base class to use and tweak. (Hacked from APC cache handler resource class I found here)

I quite like the idea of storing it in this file system as opposed to NAS / NFS as should scale quite well and also allow single point to administrate and let file system sort out replication to nodes and also flush cache at one central point. Be great to here if anyone else is using this or another DFS for Smarty cache.

Uses mogilefs PHP class from here;

http://code.google.com/p/mogilefs-php-client/

Code:

<?php

/**
* Smarty Plugin CacheResource MogileFS
*
* Implements MogileFS resource for the HTML cache
*
* @package Smarty
* @subpackage Cacher
* @author Mark Gerrard
* @credits Monte Ohrt (APC cache handler)
*/

/**
* This class does contain all necessary methods for the HTML cache with MogileFS (PHP class)
*/
class Smarty_CacheResource_MogileFS {
    function __construct($smarty)
    {
        $this->smarty = $smarty;
 
        $domain = DOMAIN;
       $class = CLASS;
      $tracker = TRACKER;
       
      $this->mogilefs = new MogileFS($domain, $class, $tracker);
         
      if(!$link = $this->mogilefs->getConnection())
           throw new Exception('Mogile cannot connect to tracker');
       
    }
    /**
    * Returns the filepath of the cached template output
    *
    * @param object $_template current template
    * @return string the cache filepath
    */
    public function getCachedFilepath($_template)
    {
        return md5($_template->getTemplateFilepath().$_template->cache_id.$template->compile_id);
    }

    /**
    * Returns the timeestamp of the cached template output
    *
    * @param object $_template current template
    * @return integer |booelan the template timestamp or false if the file does not exist
    */
    public function getCachedTimestamp($_template)
    {
          $exists = $this->mogilefs->exists('/'.$this->getCachedFilepath($_template));
        return ($exists) ? time() : false;
    }

    /**
    * Returns the cached template output
    *
    * @param object $_template current template
    * @return string |booelan the template content or false if the file does not exist
    */
    public function getCachedContents($_template)
    {
       $cache_content = $this->mogilefs->get('/'.$this->getCachedFilepath($_template));
        $_smarty_tpl = $_template;       
        ob_start();
        eval("?>" . $cache_content);
        return ob_get_clean();
    }

    /**
    * Writes the rendered template output to cache file
    *
    * @param object $_template current template
    * @return boolean status
    */
    public function writeCachedContent($_template, $content)
    {       
       $this->mogilefs->set('/'.$this->getCachedFilepath($_template), $content);
    }

    /**
    * Empty cache folder
    *
    * @param integer $exp_time expiration time
    * @return integer number of cache files deleted
    */
    public function clearAll($exp_time = null)
    {
        //return apc_clear_cache('user');
        $keys_arr = $this->mogilefs->listKeys('/');       
        foreach ($keys_arr as $key => $value)
        {
           if ($this->mogilefs->exists($value)) $this->mogilefs->delete($value);
        }
    }
    /**
    * Empty cache for a specific template
    *
    * @param string $resource_name template name
    * @param string $cache_id cache id
    * @param string $compile_id compile id
    * @param integer $exp_time expiration time
    * @return integer number of cache files deleted
    */
    public function clear($resource_name, $cache_id, $compile_id, $exp_time)
    {
       if ($this->mogilefs->exists($cache_id)) $this->mogilefs->delete($cache_id);
    }
}

?>


Mark
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 -> Plugins 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