1 <?php
defined('SYSPATH') or die('No direct script access.');
3 * View fragment caching. This is primarily used to cache small parts of a view
4 * that rarely change. For instance, you may want to cache the footer of your
5 * template because it has very little dynamic content. Or you could cache a
6 * user profile page and delete the fragment when the user updates.
8 * For obvious reasons, fragment caching should not be applied to any
9 * content that contains forms.
11 * [!!] Multiple language (I18n) support was added in v3.0.4.
16 * @copyright (c) 2009-2010 Kohana Team
17 * @license http://kohanaframework.org/license
20 class Kohana_Fragment
{
23 * @var integer default number of seconds to cache for
25 public static $lifetime = 30;
28 * @var boolean use multilingual fragment support?
30 public static $i18n = FALSE;
32 // List of buffer => cache key
33 protected static $_caches = array();
36 * Generate the cache key name for a fragment.
38 * $key = Fragment::_cache_key('footer', TRUE);
40 * @param string fragment name
41 * @param boolean multilingual fragment support
46 protected static function _cache_key($name, $i18n = NULL)
50 // Use the default setting
51 $i18n = Fragment
::$i18n;
54 // Language prefix for cache key
55 $i18n = ($i18n === TRUE) ? I18n
::lang() : '';
57 // Note: $i18n and $name need to be delimited to prevent naming collisions
58 return 'Fragment::cache('.$i18n.'+'.$name.')';
62 * Load a fragment from cache and display it. Multiple fragments can
63 * be nested with different life times.
65 * if ( ! Fragment::load('footer')) {
66 * // Anything that is echo'ed here will be saved
70 * @param string fragment name
71 * @param integer fragment cache lifetime
72 * @param boolean multilingual fragment support
75 public static function load($name, $lifetime = NULL, $i18n = NULL)
77 // Set the cache lifetime
78 $lifetime = ($lifetime === NULL) ? Fragment
::$lifetime : (int) $lifetime;
80 // Get the cache key name
81 $cache_key = Fragment
::_cache_key($name, $i18n);
83 if ($fragment = Kohana
::cache($cache_key, NULL, $lifetime))
85 // Display the cached fragment now
92 // Start the output buffer
95 // Store the cache key by the buffer level
96 Fragment
::$_caches[ob_get_level()] = $cache_key;
103 * Saves the currently open fragment in the cache.
109 public static function save()
111 // Get the buffer level
112 $level = ob_get_level();
114 if (isset(Fragment
::$_caches[$level]))
116 // Get the cache key based on the level
117 $cache_key = Fragment
::$_caches[$level];
119 // Delete the cache key, we don't need it anymore
120 unset(Fragment
::$_caches[$level]);
122 // Get the output buffer and display it at the same time
123 $fragment = ob_get_flush();
125 // Cache the fragment
126 Kohana
::cache($cache_key, $fragment);
131 * Delete a cached fragment.
133 * Fragment::delete($key);
135 * @param string fragment name
136 * @param boolean multilingual fragment support
139 public static function delete($name, $i18n = NULL)
142 Kohana
::cache(Fragment
::_cache_key($name, $i18n), NULL, -3600);