1 | <?php |
||
25 | class FragmentCache extends Widget |
||
26 | { |
||
27 | /** |
||
28 | * @var Cache|array|string the cache object or the application component ID of the cache object. |
||
29 | * After the FragmentCache object is created, if you want to change this property, |
||
30 | * you should only assign it with a cache object. |
||
31 | * Starting from version 2.0.2, this can also be a configuration array for creating the object. |
||
32 | */ |
||
33 | public $cache = 'cache'; |
||
34 | /** |
||
35 | * @var int number of seconds that the data can remain valid in cache. |
||
36 | * Use 0 to indicate that the cached data will never expire. |
||
37 | */ |
||
38 | public $duration = 60; |
||
39 | /** |
||
40 | * @var array|Dependency the dependency that the cached content depends on. |
||
41 | * This can be either a [[Dependency]] object or a configuration array for creating the dependency object. |
||
42 | * For example, |
||
43 | * |
||
44 | * ```php |
||
45 | * [ |
||
46 | * 'class' => 'yii\caching\DbDependency', |
||
47 | * 'sql' => 'SELECT MAX(updated_at) FROM post', |
||
48 | * ] |
||
49 | * ``` |
||
50 | * |
||
51 | * would make the output cache depends on the last modified time of all posts. |
||
52 | * If any post has its modification time changed, the cached content would be invalidated. |
||
53 | */ |
||
54 | public $dependency; |
||
55 | /** |
||
56 | * @var array list of factors that would cause the variation of the content being cached. |
||
57 | * Each factor is a string representing a variation (e.g. the language, a GET parameter). |
||
58 | * The following variation setting will cause the content to be cached in different versions |
||
59 | * according to the current application language: |
||
60 | * |
||
61 | * ```php |
||
62 | * [ |
||
63 | * Yii::$app->language, |
||
64 | * ] |
||
65 | * ``` |
||
66 | */ |
||
67 | public $variations; |
||
68 | /** |
||
69 | * @var bool whether to enable the fragment cache. You may use this property to turn on and off |
||
70 | * the fragment cache according to specific setting (e.g. enable fragment cache only for GET requests). |
||
71 | */ |
||
72 | public $enabled = true; |
||
73 | /** |
||
74 | * @var array a list of placeholders for embedding dynamic contents. This property |
||
75 | * is used internally to implement the content caching feature. Do not modify it. |
||
76 | */ |
||
77 | public $dynamicPlaceholders; |
||
78 | |||
79 | |||
80 | /** |
||
81 | * Initializes the FragmentCache object. |
||
82 | */ |
||
83 | 3 | public function init() |
|
95 | |||
96 | /** |
||
97 | * Marks the end of content to be cached. |
||
98 | * Content displayed before this method call and after [[init()]] |
||
99 | * will be captured and saved in cache. |
||
100 | * This method does nothing if valid content is already found in cache. |
||
101 | */ |
||
102 | 3 | public function run() |
|
125 | |||
126 | /** |
||
127 | * @var string|bool the cached content. False if the content is not cached. |
||
128 | */ |
||
129 | private $_content; |
||
130 | |||
131 | /** |
||
132 | * Returns the cached content if available. |
||
133 | * @return string|false the cached content. False is returned if valid content is not found in the cache. |
||
134 | */ |
||
135 | 3 | public function getCachedContent() |
|
160 | |||
161 | /** |
||
162 | * Replaces placeholders in content by results of evaluated dynamic statements. |
||
163 | * |
||
164 | * @param string $content |
||
165 | * @param array $placeholders |
||
166 | * @return string final content |
||
167 | */ |
||
168 | protected function updateDynamicContent($content, $placeholders) |
||
176 | |||
177 | /** |
||
178 | * Generates a unique key used for storing the content in cache. |
||
179 | * The key generated depends on both [[id]] and [[variations]]. |
||
180 | * @return mixed a valid cache key |
||
181 | */ |
||
182 | 2 | protected function calculateKey() |
|
193 | } |
||
194 |