Smarty使用Memcache进行缓存
默认的Smarty缓存是使用File进行缓存的。在高并发的时候,压力会在IO方面。为了避免在高并发的时候,IO占用高。我写了一个Smarty的插件,将缓存写入Memcache。曾经想过自己写一个模板的,感觉后期的维护比较麻烦,所以。。。。
- <?php
- /**
- * Smarty Cache Handler<br>
- * utilizing Memcache extension (http://pecl.php.net/package/memcache)<br>
- *
- * Name: mmcache_cache_handler<br>
- * Type: Cache Handler<br>
- * Purpose: Replacement for the file based cache handling of Smarty. mmcache_cache_handler() is
- * using Memcache extension to minimize disk usage.
- * File: memcache_cache_handler.php<br>
- * Date: May 20, 2009<br>
- *
- * Usage Example<br>
- * <pre>
- * $smarty = new Smarty;
- * $smarty->cache_handler_func = 'mmcache_cache_handler';
- * $smarty->caching = true;
- * $smarty->display('index.tpl');
- * </pre>
- *
- * @author Kim Chow
- * @version Beta
- *
- * @param string $action Cache operation to perform ( read | write | clear )
- * @param mixed $smarty Reference to an instance of Smarty
- * @param string $cache_content Reference to cached contents
- * @param string $tpl_file Template file name
- * @param string $cache_id Cache identifier
- * @param string $compile_id Compile identifier
- * @param integer $exp_time Expiration time
- * @return boolean TRUE on success, FALSE otherwise
- *
- * @link http://pecl.php.net/package/memcache
- * (pecl memcache homepage)
- * @link http://smarty.php.net/manual/en/section.template.cache.handler.func.php
- * (Smarty online manual)
- */
- function mmcache_cache_handler($action, &$smarty, &$cache_content, $tpl_file = null, $cache_id = null, $compile_id = null, $exp_time = null) {
- if (! function_exists ( "memcache_get" )) {
- $smarty->trigger_error ( "cache_handler: PHP Extension \"Memcache\" extension (http://pecl.php.net/package/memcache) not installed." );
- return false;
- }
- // Create unique cache id:
- // We are using smarty's internal functions here to be as compatible as possible.
- $memcache_id = "smarty_memcache_" . $cache_id . "_" . $tpl_file;
- $memcache = new memCacheClass ( );
- switch ($action) {
- case 'read' :
- // read cache from shared memory
- $cache_content = $memcache->get ( $memcache_id );
- if (! is_null ( $cache_content ) && _memcache_hasexpired ( $cache_content )) {
- // Cache has been expired so we clear it now by calling ourself with another parameter :)
- $cache_content = null;
- mmcache_cache_handler ( 'write', $smarty, $cache_content, $tpl_file, $cache_id, $compile_id );
- }
- $return = true;
- break;
- case 'write' :
- $result = $memcache->set ( $memcache_id, $cache_content, 0, $exp_time );
- if (! $result) {
- $smarty_obj->_trigger_error_msg ( 'cache_handler: query failed.' );
- }
- $return = $result;
- break;
- case 'clear' :
- if (empty ( $cache_id )) {
- $result = $memcache->delete ( $memcache_id );
- }
- if (! $result) {
- $smarty_obj->_trigger_error_msg ( 'cache_handler: query failed.' );
- }
- $return = $result;
- default :
- // error, unknown action
- $smarty->trigger_error ( "cache_handler: unknown action \"$action\"" );
- $return = false;
- break;
- }
- return $return;
- }
- /**
- * Helper function for mmcache_cache_handler()
- * Checks whether a cached content has been expired by reading the content's header.
- *
- * @access private
- * @param string $cache_content the cached content
- * @return boolean TRUE if cache has been expired, FALSE otherwise
- *
- * @see mmcache_cache_handler()
- */
- function _memcache_hasexpired(&$cache_content) {
- $split = explode ( "\n", $cache_content, 2 );
- $attributes = unserialize ( $split [0] );
- if ($attributes ['expires'] > 0 && time () > $attributes ['expires'])
- return true;
- else
- return false;
- }
- ?>
上面的例子只是为了做个说明罢了,建议将Memcache进行封装,便于管理。
2009-5-21 修改:
1、加入缓存过期时间