模块

Fragment

View fragment caching. This is primarily used to cache small parts of a view that rarely change. For instance, you may want to cache the footer of your template because it has very little dynamic content. Or you could cache a user profile page && delete the fragment when the user updates.

For obvious reasons, fragment caching should not be applied to any content that contains forms.

Multiple language (I18n) support was added in v3.0.4.

package
BootPHP
category
辅助类
author
Tinsh
copyright
© 2005-2016 Kilofox Studio
uses
BootPHP::cache

该类在 SYSPATH/classes/fragment.php 第 23 行声明。

属性

public static boolean $i18n

use multilingual fragment support?

bool false

public static integer $lifetime

default number of seconds to cache for

integer 30

protected static array $_caches

list of buffer => cache key

array(0) 

方法

public static delete( string $name [, boolean $i18n = NULL ] ) (在 Fragment 中定义)

Delete a cached fragment.

Fragment::delete($key);

参数

  • string $name required - Fragment name
  • boolean $i18n = NULL - Multilingual fragment support

返回值

  • void

源代码

public static function delete($name, $i18n = NULL)
{
	// Invalid the cache
	BootPHP::cache(Fragment::_cache_key($name, $i18n), NULL, -3600);
}

public static load( string $name [, integer $lifetime = NULL , boolean $i18n = NULL ] ) (在 Fragment 中定义)

Load a fragment from cache && display it. Multiple fragments can be nested with different life times.

if ( !Fragment::load('footer') ) {
     // Anything that is echo'ed here will be saved
     Fragment::save();
}

参数

  • string $name required - Fragment name
  • integer $lifetime = NULL - Fragment cache lifetime
  • boolean $i18n = NULL - Multilingual fragment support

返回值

  • boolean

源代码

public static function load($name, $lifetime = NULL, $i18n = NULL)
{
	// Set the cache lifetime
	$lifetime = ($lifetime === NULL) ? Fragment::$lifetime : (int) $lifetime;
	// Get the cache key name
	$cache_key = Fragment::_cache_key($name, $i18n);
	if ($fragment = BootPHP::cache($cache_key, NULL, $lifetime))
	{
		// Display the cached fragment now
		echo $fragment;
		return true;
	}
	else
	{
		// Start the output buffer
		ob_start();
		// Store the cache key by the buffer level
		Fragment::$_caches[ob_get_level()] = $cache_key;
		return false;
	}
}

public static save( ) (在 Fragment 中定义)

Saves the currently open fragment in the cache.

Fragment::save();

返回值

  • void

源代码

public static function save()
{
	// Get the buffer level
	$level = ob_get_level();
	if (isset(Fragment::$_caches[$level]))
	{
		// Get the cache key based on the level
		$cache_key = Fragment::$_caches[$level];
		// Delete the cache key, we don't need it anymore
		unset(Fragment::$_caches[$level]);
		// Get the output buffer && display it at the same time
		$fragment = ob_get_flush();
		// Cache the fragment
		BootPHP::cache($cache_key, $fragment);
	}
}

protected static _cache_key( string $name [, boolean $i18n = NULL ] ) (在 Fragment 中定义)

Generate the cache key name for a fragment.

$key = Fragment::_cache_key('footer', true);

参数

  • string $name required - Fragment name
  • boolean $i18n = NULL - Multilingual fragment support

Tags

返回值

  • string

源代码

protected static function _cache_key($name, $i18n = NULL)
{
	if ($i18n === NULL)
	{
		// Use the default setting
		$i18n = Fragment::$i18n;
	}
	// Language prefix for cache key
	$i18n = ($i18n === true) ? I18n::lang() : '';
	// Note: $i18n && $name need to be delimited to prevent naming collisions
	return 'Fragment::cache(' . $i18n . '+' . $name . ')';
}