summaryrefslogtreecommitdiff
path: root/framework/Web/UI/WebControls/TOutputCache.php
blob: 9708c0331314b6b01cd85b852d1875fdd9d2627c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php

class TOutputCache extends TControl implements INamingContainer
{
	const CACHE_ID_PREFIX='prado:outputcache';
	private $_useCache=false;
	private $_cacheReady=false;
	private $_cacheChecked=false;
	private $_expiry=60;
	private $_cache=null;
	private $_contents;
	private $_state;
	private $_enableCaching=true;
	private $_actions=array();

	protected function initRecursive($namingContainer=null)
	{
		if($this->_cacheReady && !$this->_useCache)
		{
			$stack=$this->getPage()->getCachingStack();
			$stack->push($this);
			parent::initRecursive($namingContainer);
			$stack->pop();
		}
		else
			parent::initRecursive($namingContainer);
	}

	protected function loadRecursive()
	{
		if($this->_cacheReady && !$this->_useCache)
		{
			$stack=$this->getPage()->getCachingStack();
			$stack->push($this);
			parent::loadRecursive();
			$stack->pop();
		}
		else
		{
			if($this->_useCache)
			{
				$cs=$this->getPage()->getClientScript();
				foreach($this->_actions as $action)
				{
					if($action[0]==='registerRequiresPostData')
						$this->getPage()->registerRequiresPostData($action[1]);
					else
						call_user_func_array(array($cs,$action[0]),$action[1]);
				}
			}
			parent::loadRecursive();
		}
	}

	protected function preRenderRecursive()
	{
		if($this->_cacheReady && !$this->_useCache)
		{
			$stack=$this->getPage()->getCachingStack();
			$stack->push($this);
			parent::preRenderRecursive();
			$stack->pop();
		}
		else
			parent::preRenderRecursive();
	}

	public function registerAction($funcName,$funcParams)
	{
		$this->_actions[]=array($funcName,$funcParams);
	}

	public function getAllowChildControls()
	{
		if(!$this->_cacheChecked)
		{
			$this->_cacheChecked=true;
			if(!$this->getPage()->getIsPostBack() && ($this->_cache=$this->getApplication()->getCache())!==null && $this->getEnableCaching())
			{
				$this->_cacheReady=true;
				$data=$this->_cache->get($this->getCacheKey());
				if(($this->_useCache=($data!==false)))
					list($this->_contents,$this->_state,$this->_actions)=$data;
			}
		}
		return !$this->_useCache;
	}

	public function getEnableCaching()
	{
		return $this->_enableCaching;
	}

	public function setEnableCaching($value)
	{
		$this->_enableCaching=TPropertyValue::ensureBoolean($value);
	}

	protected function loadStateRecursive(&$state,$needViewState=true)
	{
		if($this->_useCache)
			parent::loadStateRecursive($this->_state,$needViewState);
		else
			parent::loadStateRecursive($state,$needViewState);
	}

	protected function &saveStateRecursive($needViewState=true)
	{
		if($this->_useCache)
			return $this->_state;
		else if($this->_cacheReady)
		{
			$this->_state=parent::saveStateRecursive($needViewState);
			return $this->_state;
		}
		else
			return parent::saveStateRecursive($needViewState);
	}

	protected function getCacheKey()
	{
		return self::CACHE_ID_PREFIX.$this->getUniqueID();
	}

	public function getExpiry()
	{
		return $this->_expiry;
	}

	public function setExpiry($value)
	{
		if(($value=TPropertyValue::ensureInteger($value))<0)
			throw new TInvalidDataValueException('outputcache_expiry_invalid');
		$this->_expiry=$value;
	}

	public function render($writer)
	{
		if($this->_useCache)
			$writer->write($this->_contents);
		else if($this->_cacheReady)
		{
			$textWriter=new TTextWriter;

			$stack=$this->getPage()->getCachingStack();
			$stack->push($this);
			parent::render(new THtmlWriter($textWriter));
			$stack->pop();

			$content=$textWriter->flush();
			$data=array($content,$this->_state,$this->_actions);
			$this->_cache->set($this->getCacheKey(),$data,$this->getExpiry());
			$writer->write($content);
		}
		else
			parent::render($writer);
	}
}

?>