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

Need help for gzip cache file handler

 
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
bluejester
Smarty Regular


Joined: 26 Apr 2012
Posts: 55

PostPosted: Thu Feb 06, 2014 10:50 am    Post subject: Need help for gzip cache file handler Reply with quote

Hello everyone,

I recently posted a Smarty 3 Cache plugin enabling Cache handling in a database through PDO support and optionnal Gzip compression :
http://www.smarty.net/forums/viewtopic.php?t=24716

It works well on my dev server, but for several reasons, I still can't test it on a production environment, and since my production db is heavily loaded, the probability to crash it isn't insignificant.

So, I went out with the idea to use File cache handling, but with Gzip support. My website running millions of Urls (I have way too much, we're working on it), my file cache size was Gigabytes huge, so I first gzdeflate() what I assigned to Smarty, so as to reduce the cache size.

The problem is, this way, Smarty creates a .php cache file, mixing PHP code and gzip-encoded data in the same file. As it is not an issue in most cases, sometimes the gzinflate() works badly, and the rendering is crashed.

As a consequence, I decided to write another cache plugin, that would extends the File Cache Handler, but which would write a smarty .php cache file fully gzipped (and I would no longer gzdeflate() what I assign to Smarty, since the whole file would be gzipped by Smarty).

Here's my code :

File Smarty.cacheresource.file.gzip.php :
Code:

class Smarty_CacheResource_File_Gzip extends Smarty_Internal_CacheResource_File {

    /**
   * Read the cached template and process its header
   *
   * @param Smarty_Internal_Template $_template template object
   * @param Smarty_Template_Cached $cached cached object
   * @return booelan true or false if the cached content does not exist
   */
   public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached=null)
   {
      
      $_smarty_tpl = $_template;
      
      $content = gzinflate(file_get_contents($_template->cached->filepath));

        return $content;
   }
   
   /**
   * Write the rendered template output to cache
   *
   * @param Smarty_Internal_Template $_template template object
   * @param string                   $content   content to cache
   * @return boolean success
   */
   public function writeCachedContent(Smarty_Internal_Template $_template, $content)
   {
      return parent::writeCachedContent($_template, gzdeflate($content));
   }

}


Usage :
Code:

$Smarty->SetCaching(Smarty::CACHING_LIFETIME_CURRENT);
$Smarty->SetCacheLifetime(3600);
$Smarty->SetCachingType('file_gzip');
$Smarty->Assign('MyCode', 'Sample text');
$Smarty->Display('string:{$MyCode}');


On the first Display() call (cache doesn't exist), Smarty correctly writes a gzip-encoded file into the cache folder.
However, on the next calls, Smarty displays the gzip content hieroglyphs, without decoding it.

Can you help me to figure this out ?

Thanks,
Ben
Back to top
View user's profile Send private message Visit poster's website
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Thu Feb 06, 2014 7:21 pm    Post subject: Reply with quote

As you don't include the PHP any longer. The content must be parsed by eval.

Code:
 public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached=null)
   {
       
      $_smarty_tpl = $_template;
       
      $content = gzinflate(file_get_contents($_template->cached->filepath));

         if (isset($content))

            eval("?>" . $content);

            return true;
        }

        return false;
   }
Back to top
View user's profile Send private message
bluejester
Smarty Regular


Joined: 26 Apr 2012
Posts: 55

PostPosted: Fri Feb 07, 2014 11:16 am    Post subject: Reply with quote

Uwe, it works amazingly well ! Smile
Thanks a lot ! Cool
Back to top
View user's profile Send private message Visit poster's website
bluejester
Smarty Regular


Joined: 26 Apr 2012
Posts: 55

PostPosted: Tue Mar 04, 2014 10:25 am    Post subject: Reply with quote

It works on the production environment of a high traffic website for more than 3 weeks. Performances are great. Cool
Back to top
View user's profile Send private message Visit poster's website
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue Mar 04, 2014 3:34 pm    Post subject: Reply with quote

Thanks for the feefback
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
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